我正在尝试创建一个从url捕获参数的formset,以预填充其中一个字段。表单正确显示并在正确的地址,但点击“提交”后,页面重定向到“/ correction /”而不是目标/更正/ A07686 + dwdID19,表单不保存。可能是什么问题?
在models.py中:
class correction(models.Model):
corrected_word = models.ForeignKey(item)
correction_author = models.ForeignKey(User)
correction_made = models.IntegerField(u'correction_made', choices=CORRECTION_CHOICES)
correction_word = models.CharField(u'correction_word', max_length=200, blank=True, null=True)
time = models.DateTimeField(auto_now_add=True)
approved = models.IntegerField(u'approved', choices=APPROVAL_CHOICES, blank=True, null=True)
def __unicode__(self):
return str(self.time)
在views.py中:
def submit_corr(request, bdword):
if hasattr(request, 'user') and request.user.is_authenticated():
word = item.objects.filter(file_position=bdword)[0]
CorrFormSet = inlineformset_factory(item, correction, fields=['correction_made', 'correction_word','correction_author'], can_delete=False, extra=1)
form = CorrFormSet(request.POST, request.FILES, instance=word, initial=[{'correction_author': request.user,}])
if request.method == 'POST':
if form.is_valid():
for entry in form:
entry.save()
else:
form = CorrFormSet(instance=word, initial=[{'correction_author': request.user,}])
return render(request, "correctionform.html", {"form": form,"word": word})
在网址中:
url(r'^correction/(?P<bdword>.*)$', 'english.views.submit_corr'),
在模板中:
<a href="../correction/{{word.file_position}}">Submit correction</a></th>
提前致谢!
答案 0 :(得分:0)
<a href="../correction/{{word.file_position}}">Submit correction</a>
没有提交表单,因为它是链接,当您按下链接时,会向服务器发送带有GET方法的请求,因此请勿输入if request.method == 'POST':
请尝试类似
的内容<form action="/correction/{{word.file_position}}" method="POST">
{# your inputs fields#}
<button type="submit">Submit correction</button>
</form>
我希望有用。此致