[英]Data from POST request not passed from template/form to view in django
Actually, I'm not really sure what the problem exactly is, but I think data from the form/template is not being passed to the view in my Django app. 实际上,我不太确定问题到底出在哪里,但是我认为表单/模板中的数据没有传递到Django应用程序的视图中。
Running a test gives: 运行测试可以得出:
AttributeError: 'NoneType' object has no attribute 'id'
The form: 表格:
<form action="/{{ link.id }}/{{ node.id }}/" method="post" class="comment-form">
{% csrf_token %}
{% for field in form %}
{{ field.label_tag }} {{ field }}
{% if field.help_text %}
<p class="help">{{ field.help_text|safe }}</p>
{% endif %}
{% endfor %}
<input type="submit" value="submit">
</form>
The view: 风景:
def comments(request, link_id, comment_id=None):
link = get_object_or_404(Link, pk=link_id)
if comment_id is not None:
parent = get_object_or_404(Comment, pk=comment_id)
else:
parent = None
comments = link.comments.all()
if request.method == 'POST':
if request.user.is_authenticated():
form = CommentForm(data=request.POST,auto_id=True)
if form.is_valid():
form.full_clean()
new_comment = form.save(commit=False)
new_comment.link = link
new_comment.parent = parent
new_comment.save()
return reverse('comments', kwargs={'link_id':link.id,'comment_id':parent.id})
else:
return HttpResponseRedirect('/accounts/login/?next=/{0}/'.format(link.id))
else:
form = CommentForm(auto_id=True)
return render(request, 'posts/comments.html', {'nodes':comments, 'link': link, 'form': form})
The full relevant code is here , because it's maybe a bit much for one stackoverflow post. 完整的相关代码在这里 ,因为对于一个stackoverflow帖子可能要花很多。 (Is there a guideline for this?)
(是否有指导方针?)
This is the key part of the traceback. 这是回溯的关键部分。
File "/Users/robin/learning/python/parrot/posts/views.py", line 42, in comments
return reverse('comments', kwargs={'link_id':link.id,'comment_id':parent.id})
AttributeError: 'NoneType' object has no attribute 'id' AttributeError:“ NoneType”对象没有属性“ id”
Since link
cannot be None, that suggests that parent
is None
. 由于
link
不能为None,这表明parent
为None
。 Perhaps you need to use a different url if parent_id
is None. 如果
parent_id
为None,则可能需要使用其他URL。
The view should return an HttpResponse
, not a url. 该视图应返回
HttpResponse
而不是url。 I think you want: 我想你要:
return HttpResponseRedirect(reverse('comments', kwargs={'link_id':link.id,'comment_id':parent.id}))
or you can use the redirect
shortcut: 或者您可以使用
redirect
快捷方式:
from django.shortcuts import reverse
return redirect('comments', link_id=link.id, comment_id=parent.id)
You state in your view: 您认为:
if comment_id is not None:
parent = get_object_or_404(Comment, pk=comment_id)
else:
parent = None
but it seems you never pass comment_id
, just link_id
, therefore in this line: 但是似乎您永远不会传递
comment_id
,而只会传递link_id
,因此在这一行中:
return reverse('comments', kwargs={'link_id':link.id,'comment_id':parent.id})
parent
is always None. parent
永远是无。
It would help to see your url mappings as well as CommentForm. 这将有助于查看您的url映射以及CommentForm。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.