英文:
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(…)</code>,它会触发视图而不是函数。
这就是为什么类通常以PascalCase编写的原因之一,所以使用Appointment
而不是<s>appointment
</s>:
<pre><code>class <b>Appointment</b>(models.Model):
count = models.AutoField(primary_key=True)
# …</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
<sup>[Django-doc]</sup>,而不是手动验证和清理数据。Form
不仅会简化在HTML中渲染表单,而且还会更方便地验证输入并清理数据为更方便的类型。
> 注意: 在成功的POST请求的情况下,应进行redirect
<sup>[Django-doc]</sup>以实现Post/Redirect/Get模式 <sup>[wiki]</sup>。这可以避免在用户刷新浏览器时再次发出相同的POST请求。
英文:
Likely you have a view function with the same name. This thus means that if you call <code>appointment(…)</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)
# …</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
<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 <sup>[wiki]</sup>.
> This avoids that you make the same POST request when the user refreshes the
> browser.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论