Django:无法重定向到djang中的其他视图

2024-09-24 06:32:57 发布

您现在位置:Python中文网/ 问答频道 /正文

我试着点击http://127.0.0.1:8000/main/electronics/switch/中的一个按钮来调用getCommodityCommentDetail来做一些事情,然后重定向到另一个页面commodityInfoPage。你知道吗

让我困惑的是,页面总是在初始页面中显示相同的内容,尽管url已经更改为http://127.0.0.1:8000/main/comments/1/。你知道吗

经过测试,我发现commodityInfoPage在视图.py不打电话。我花了很长时间寻找解决办法,但都失败了。那我怎么修呢?你知道吗

你知道吗网址.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'),
]

你知道吗视图.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>

Tags: pathnamepymainrequest页面postviews
1条回答
网友
1楼 · 发布于 2024-09-24 06:32:57

问题是comments/1/commodityInfoPageURL模式匹配。你知道吗

path('<str:category>/<str:searchKey>/',views.commodityInfoPage, name='commodityInfoPage'),

您可以通过更改URL模式使其不发生冲突,或者将commodityCommentPageURL模式移到commodityInfoPage模式上方来解决此问题。你知道吗

path('comments/<str:commodityId>/', views.commodityCommentPage, name='commodityCommentPage'),
path('<str:category>/<str:searchKey>/', views.commodityInfoPage, name='commodityInfoPage'),

请注意,如果您对模式重新排序,那么如果类别为“comments”,您将无法查看commodityInfoPage。你知道吗

相关问题 更多 >