渲染期间的 NoReverseMatch
NoReverseMatch during render
我收到此错误:
NoReverseMatch at /comments_page/1/post_comment/
Reverse for 'post_comment' with arguments '('',)' not found. 1 pattern(s) tried: ['comments_page/(?P[0-9]+)/post_comment/$']
我的views.py
def post_comment(request, product_id):
host_product = Product.objects.get(pk=product_id)
comment = Comment()
comment.product = host_product
comment.author = request.POST["author"]
comment.comment_text = request.POST["comment"]
comment.save()
return render(request, 'comments_page/detail.html', {"host_product": host_product})
我的comments_page\urls.py
from django.conf.urls import url
from . import views
app_name = "comments_page"
urlpatterns = [
# /comments_page/
url(r'^$', views.index, name="index"),
# /comments_page/1/
url(r'^(?P<product_id>[0-9]+)/$', views.detail, name="detail"),
# /comments_page/1/post_comment/
url(r'^(?P<product_id>[0-9]+)/post_comment/$', views.post_comment, name='post_comment'),]
我的detail.html
<form action="{% url 'comments_page:post_comment' product.id %}" method="post">
{% csrf_token %}
Name: <input type="text" id="author" name="author">
Comment:
<textarea id="comment" name="comment"></textarea><br>
<input type="submit" value="Post Comment">
我想我已经确定问题出在product.id这里
{% url 'comments_page:post_comment' product.id %}
在 html 页面中。我试过用几种不同的方式格式化它,但我没有任何运气。请注意,评论正在通过,表单正在更新数据库并加载页面上的条目,但页面没有被重定向。我必须手动重新加载它。任何帮助将不胜感激。
错误消息显示您传递给 {% url %}
标记的参数不存在并解析为空字符串。您的视图确实没有传入 product
变量,仅传入 host_product
变量。您需要相应地更改标签:
{% url 'comments_page:post_comment' host_product.id %}
对于那些可能想知道的人,解决方法是将 views.py 中的 return 函数更改为此
return render(request, 'comments_page/detail.html', {"product": host_product})
我不明白为什么会这样,但确实如此。任何关于如何清理我的 post_comment 函数的建议都将不胜感激。我觉得使用 host_product
过于复杂
我收到此错误:
NoReverseMatch at /comments_page/1/post_comment/
Reverse for 'post_comment' with arguments '('',)' not found. 1 pattern(s) tried: ['comments_page/(?P[0-9]+)/post_comment/$']
我的views.py
def post_comment(request, product_id):
host_product = Product.objects.get(pk=product_id)
comment = Comment()
comment.product = host_product
comment.author = request.POST["author"]
comment.comment_text = request.POST["comment"]
comment.save()
return render(request, 'comments_page/detail.html', {"host_product": host_product})
我的comments_page\urls.py
from django.conf.urls import url
from . import views
app_name = "comments_page"
urlpatterns = [
# /comments_page/
url(r'^$', views.index, name="index"),
# /comments_page/1/
url(r'^(?P<product_id>[0-9]+)/$', views.detail, name="detail"),
# /comments_page/1/post_comment/
url(r'^(?P<product_id>[0-9]+)/post_comment/$', views.post_comment, name='post_comment'),]
我的detail.html
<form action="{% url 'comments_page:post_comment' product.id %}" method="post">
{% csrf_token %}
Name: <input type="text" id="author" name="author">
Comment:
<textarea id="comment" name="comment"></textarea><br>
<input type="submit" value="Post Comment">
我想我已经确定问题出在product.id这里
{% url 'comments_page:post_comment' product.id %}
在 html 页面中。我试过用几种不同的方式格式化它,但我没有任何运气。请注意,评论正在通过,表单正在更新数据库并加载页面上的条目,但页面没有被重定向。我必须手动重新加载它。任何帮助将不胜感激。
错误消息显示您传递给 {% url %}
标记的参数不存在并解析为空字符串。您的视图确实没有传入 product
变量,仅传入 host_product
变量。您需要相应地更改标签:
{% url 'comments_page:post_comment' host_product.id %}
对于那些可能想知道的人,解决方法是将 views.py 中的 return 函数更改为此
return render(request, 'comments_page/detail.html', {"product": host_product})
我不明白为什么会这样,但确实如此。任何关于如何清理我的 post_comment 函数的建议都将不胜感激。我觉得使用 host_product
过于复杂