Data is not storing in django database.

huangapple go评论79阅读模式
英文:

Data is not storing in django database

问题

这是在 views.py 中的代码。

    def thanks(request):
        if request.method == 'POST':
            name = request.POST['name']
            email = request.POST['email']
            phone = request.POST['phone']
            doctor = request.POST['doctor']
            msg = request.POST['reason']
            print(name, email, phone, msg)
            appointment_db = appointment(email=email, phone=phone, doctor=doctor, message=msg)
            appointment_db.save()
        return render(request, "thanks.html", {"name": name})

这是错误:

            appointment_db = appointment(email=email, phone=phone, doctor=doctor, message=msg)
    TypeError: appointment() got an unexpected keyword argument 'name'

这是模型:

from django.db import models

class appointment(models.Model):
    count = models.AutoField(primary_key=True)
    name = models.CharField(max_length=100)
    email = models.EmailField(max_length=1000)
    phone = models.IntegerField()
    message = models.TextField(max_length=5000)
    doctor = models.CharField(max_length=100)

    def __str__(self):
        return self.name + " | " + "Doctor : " + self.doctor

模型也已注册,并显示在管理面板中。然而,当我尝试通过表单将数据存储到其中时,它会抛出错误。

英文:

This is the code in views.py.

def thanks(request):
    if request.method == 'POST':
        name = request.POST['name']
        email = request.POST['email']
        phone= request.POST['phone']
        doctor = request.POST['doctor']
        msg = request.POST['reason']
        print(name, email, phone, msg)
        appointment_db = appointment(name=name, email=email, phone=phone, doctor=doctor, message=msg)
        appointment_db.save()
    return render(request, "thanks.html", {"name": name})

rest of the code is working. the only error is in the second-last and third-last line.

 appointment_db = appointment(name=name, email=email, phone=phone, doctor=doctor, message=msg)
        appointment_db.save()

this is the error:

                 appointment_db = appointment(email=email, phone=phone, doctor=doctor, message=msg)
TypeError: appointment() got an unexpected keyword argument 'name'

this is the model:

from django.db import models

# Create your models here.
class appointment(models.Model):
    count=models.AutoField(primary_key=True)
    name= models.CharField(max_length=100)
    email= models.EmailField(max_length=1000)
    phone = models.IntegerField()
    message =models.TextField(max_length=5000)
    doctor = models.CharField(max_length=100)

    def __str__(self):
        return self.name + " | "+ "Doctor : "+ self.doctor

The model is also registered and it is showing in the admin panel. However, when I try to store data in it via forms, it throws an error.

答案1

得分: 0

可能您有一个同名的视图函数。这意味着,如果您调用<code>appointment(&hellip;)</code>,它会触发视图而不是函数。

这就是为什么类通常以PascalCase编写的原因之一,所以使用Appointment而不是<s>appointment</s>:

<pre><code>class <b>Appointment</b>(models.Model):
count = models.AutoField(primary_key=True)
# &hellip;</code></pre>

然后视图将会是这样的:

<pre><code>from django.views.decorators.http import require_POST

@require_POST
def thanks(request):
name = request.POST['name']
email = request.POST['email']
phone = request.POST['phone']
doctor = request.POST['doctor']
msg = request.POST['reason']
print(name, email, phone, msg)
<b>Appointment</b>.objects.create(
name=name, email=email, phone=phone, doctor=doctor, message=msg
)
return render(request, 'thanks.html', {'name': name})</code></pre>


> 注意: 最好使用Form&nbsp;<sup>[Django-doc]</sup>,而不是手动验证和清理数据。Form不仅会简化在HTML中渲染表单,而且还会更方便地验证输入并清理数据为更方便的类型。


> 注意: 在成功的POST请求的情况下,应进行redirect<sup>[Django-doc]</sup>以实现Post/Redirect/Get模式&nbsp;<sup>[wiki]</sup>。这可以避免在用户刷新浏览器时再次发出相同的POST请求。

英文:

Likely you have a view function with the same name. This thus means that if you call <code>appointment(&hellip;)</code>, it triggers the view instead.

This is one of the reasons why classes are normally written in PascalCase, so Appointment instead of <s>appointment</s>:

<pre><code>class <b>Appointment</b>(models.Model):
count = models.AutoField(primary_key=True)
# &hellip;</code></pre>

then the view thus works with:

<pre><code>from django.views.decorators.http import require_POST

@require_POST
def thanks(request):
name = request.POST['name']
email = request.POST['email']
phone = request.POST['phone']
doctor = request.POST['doctor']
msg = request.POST['reason']
print(name, email, phone, msg)
<b>Appointment</b>.objects.create(
name=name, email=email, phone=phone, doctor=doctor, message=msg
)
return render(request, 'thanks.html', {'name': name})</code></pre>


> Note: It is better to use a Form&nbsp;<sup>[Django-doc]</sup>
> than to perform manual validation and cleaning of the data. A Form will not
> only simplify rendering a form in HTML, but it also makes it more convenient
> to validate the input, and clean the data to a more convenient type.


> Note: In case of a successful POST request, you should make a redirect
> <sup>[Django-doc]</sup>

> to implement the Post/Redirect/Get pattern&nbsp;<sup>[wiki]</sup>.
> This avoids that you make the same POST request when the user refreshes the
> browser.

huangapple
  • 本文由 发表于 2023年6月25日 18:09:43
  • 转载请务必保留本文链接:https://go.coder-hub.com/76549876.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定