我试图点击http://127.0.0.1:8000/main/electronics/switch/
中的按钮来调用getCommodityCommentDetail
做某事,然后重定向到另一页commodityInfoPage
。
让我感到困惑的是,页面总是在初始页面中显示相同的内容,尽管网址已经改变,例如到http://127.0.0.1:8000/main/comments/1/
。
经过测试,我发现views.py中的commodityInfoPage
没有被调用。我已经搜索了很长时间的解决方案,但所有这些都失败了。那么我该如何解决呢?
URLs.朋友:
app_name = 'main'
urlpatterns = [
# eg:127.0.0.1:8000/main/
path('', views.index, name = 'index'),
path('getCommodityInfo/', views.getCommodityInfo, name = 'getCommodityInfo'),
path('getCommodityCommentDetail/', views.getCommodityCommentDetail, name="getCommodityCommentDetail"),
path('<str:category>/<str:searchKey>/',views.commodityInfoPage, name = 'commodityInfoPage'),
path('comments/<str:commodityId>/', views.commodityCommentPage,name = 'commodityCommentPage'),
]
view.朋友:
def getCommodityCommentDetail(request):
if request.method=="POST":
commodityId = request.POST.get("commodityId")
# scrapy module is waiting implementation
#
return HttpResponseRedirect(reverse('main:commodityInfoPage',args=(commodityId)))
def commodityCommentPage(request, commodityId):
print("enter commodityCommentPage")
commentList = JDCommentDetail.objects.all()
context = {'commentList':commentList}
return render(request,'main/commodityCommentPage.html',context)
模板:
<form action="{% url 'main:getCommodityCommentDetail'%}" method="POST">
{% csrf_token %}
<input class="hidden" value="{{commodity.id}}" name="commodityId">
<button type="submit" class="btn btn-default" >review</button>
</form>
问题是comments/1/
与commodityInfoPage
URL模式匹配。
path('<str:category>/<str:searchKey>/',views.commodityInfoPage, name='commodityInfoPage'),
您可以通过更改URL模式以便它们不会发生冲突来解决此问题,或者通过将commodityCommentPage
URL模式移动到commodityInfoPage
上方来解决此问题。
path('comments/<str:commodityId>/', views.commodityCommentPage, name='commodityCommentPage'),
path('<str:category>/<str:searchKey>/', views.commodityInfoPage, name='commodityInfoPage'),
请注意,如果您重新排序模式,如果类别为“评论”,您将无法查看commodityInfoPage
。