django:无法重定向到 django 中的另一个视图
django: unable to redirect to another view in django
我试图点击 http://127.0.0.1:8000/main/electronics/switch/
中的一个按钮来调用 getCommodityCommentDetail
做某事,然后重定向到另一个页面 commodityInfoPage
。
令我困惑的是,尽管 url 发生了变化,但该页面始终显示与初始页面相同的内容,例如至 http://127.0.0.1:8000/main/comments/1/
。
测试后发现views.py中的commodityInfoPage
没有被调用。我已经搜索了很长时间的解决方案,但都失败了。那我该如何解决呢?
urls.py:
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.py:
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'),
请注意,如果您重新排列图案,如果类别是 'comments',您将无法查看 commodityInfoPage
。
我试图点击 http://127.0.0.1:8000/main/electronics/switch/
中的一个按钮来调用 getCommodityCommentDetail
做某事,然后重定向到另一个页面 commodityInfoPage
。
令我困惑的是,尽管 url 发生了变化,但该页面始终显示与初始页面相同的内容,例如至 http://127.0.0.1:8000/main/comments/1/
。
测试后发现views.py中的commodityInfoPage
没有被调用。我已经搜索了很长时间的解决方案,但都失败了。那我该如何解决呢?
urls.py:
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.py:
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'),
请注意,如果您重新排列图案,如果类别是 'comments',您将无法查看 commodityInfoPage
。