“我的视图”函数在返回渲染时添加到url。我怎样才能避免这种情况?

2024-10-01 00:15:44 发布

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

当用户未填写所有信息时,应将其发送回http://127.0.0.1:8000/content/readerpage/40,但将其发送到http://127.0.0.1:8000/content/readerpage/40/add_review

我怎样才能避免这种情况


def add_review(request, content_id):
    content = get_object_or_404(Content, pk=content_id)
    if request.POST['readability'] and request.POST['readability_rating'] and request.POST['actionability'] and request.POST['actionability_rating'] and request.POST['general_comments']:
        review = Review()
        review.readability = request.POST['readability']
        review.readability_rating = request.POST['readability_rating']
        review.actionability = request.POST['actionability']
        review.actionability_rating = request.POST['actionability_rating']
        review.general_comments = request.POST['general_comments']
        review.save()
        return redirect('home')
    else:
        return render(request, 'content/readerpage.html', {'error': 'You need to fill in all information'})

urlpatterns = [
    path('', views.home, name='home'),
    path('add/', views.add, name='add'),
    path('<int:content_id>', views.details, name='details'),
    path('link/<int:content_id>', views.link, name='link'),
    path('readerpage/<int:content_id>', views.readerpage, name='readerpage'),
    path('readerpage/<int:content_id>/add_review',
         views.add_review, name='add_review'),
]

谢谢你阅读这篇文章


Tags: andpathnameaddidrequestcontentpost