用于添加新元素的 django 表单
我在views.py代码中为我的视图添加新文档到数据库:
def doc_creation(request):
# add_form = forms.DocAddForm(request.POST or None)
if request.method == 'POST':
add_form = forms.DocAddForm(data=request.POST)
if add_form.is_valid():
new_doc = add_form.save(commit=False)
rnd_nip=TestClass()
new_doc.doc_client_nip = rnd_nip.random_client_nip()
new_doc.added_by_2 = request.user
new_doc.save()
else:
add_form = forms.DocAddForm()
else:
add_form = forms.DocAddForm()
return render(request,'doc_new_2.html',{'add_form':add_form})
我的模型代码是:
class Doc(models.Model):
doc_client_nip = models.CharField(blank=False,null=False,max_length=100,validators=[MinLengthValidator(10)],primary_key=True)
doc_application_id = models.CharField(blank=False,null=True,max_length=100,validators=[MinLengthValidator(17)])
added_by_2 = models.ForeignKey(User, on_delete=models.CASCADE,null=True)
TestClass()及其方法random_client_nip()用于生成该字段的随机值
问题是每次我尝试时提交新记录时,数据库中不会添加任何内容 页面重新加载为空表单,但我没有收到任何错误。
到底是怎么回事?是added_by_2字段有问题吗?
I've got in views.py code for my view to add new docs to database:
def doc_creation(request):
# add_form = forms.DocAddForm(request.POST or None)
if request.method == 'POST':
add_form = forms.DocAddForm(data=request.POST)
if add_form.is_valid():
new_doc = add_form.save(commit=False)
rnd_nip=TestClass()
new_doc.doc_client_nip = rnd_nip.random_client_nip()
new_doc.added_by_2 = request.user
new_doc.save()
else:
add_form = forms.DocAddForm()
else:
add_form = forms.DocAddForm()
return render(request,'doc_new_2.html',{'add_form':add_form})
my model code is:
class Doc(models.Model):
doc_client_nip = models.CharField(blank=False,null=False,max_length=100,validators=[MinLengthValidator(10)],primary_key=True)
doc_application_id = models.CharField(blank=False,null=True,max_length=100,validators=[MinLengthValidator(17)])
added_by_2 = models.ForeignKey(User, on_delete=models.CASCADE,null=True)
TestClass() with its method random_client_nip() is used to generate random value of this field
The problem is every time when I try to submit new record nothing is added to database
page is reloaded to empty form but I don't receive any error.
What is going on? Is the field added_by_2 problem?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论