如何在没有视图模型的情况下呈现html页面?

时间:2018-06-17 04:30:02

标签: python django

如果页面只显示静态HTML,是否可以在Django中没有视图模型的情况下呈现HTML页面?

基本上,我想从网页中删除问题,然后在删除后显示“已成功删除”的静态HTML页面。

但是我得到了错误,任何人都可以帮忙吗?

/ project / 1 / issue / 14 / delete_issue /的NoReverseMatch 未找到'nice_delete.html'的反向。 'nice_delete.html'不是有效的视图函数或模式名称。

view.py

def delete_issue(request,project_id,issue_id):
    if not request.user.is_staff or not request.user.is_superuser:
        raise Http404
    issue = get_object_or_404(Issue,id=issue_id)
    issue.delete()
    return redirect(reverse('project:issue_tracker:nice_delete.html'))

urls.py

    urlpatterns =[
        path('',views.list_of_issue,name='list_of_issue'),
        path('<int:issue_id>/',views.issue_detail,name='issue_detail'),
        path('<int:issue_id>/comment',views.add_comment,name='add_comment'),
        path('new_issue/',views.new_issue,name='new_issue'),
        path('<int:issue_id>/edit_issue/',views.edit_issue,name='edit_issue'),


path('<int:issue_id>/delete_issue/',views.delete_issue,name='delete_issue'),
    ]

nice_delete.html

{% extends 'base.html' %}


{% block content %}
    <p>Successfully delete this issue</p>

{% endblock %}

1 个答案:

答案 0 :(得分:3)

您可以使用TemplateView。只需添加到您的urlpattern:

from django.views.generic import TemplateView

urlpatterns =[
        path('',views.list_of_issue,name='list_of_issue'),
        path('<int:issue_id>/',views.issue_detail,name='issue_detail'),
        path('<int:issue_id>/comment',views.add_comment,name='add_comment'),
        path('new_issue/',views.new_issue,name='new_issue'),
        path('<int:issue_id>/edit_issue/',views.edit_issue,name='edit_issue'),
        path('<int:issue_id>/delete_issue/',views.delete_issue,name='delete_issue'),
        path('deleted/', TemplateView.as_view(template_name="nice_delete.html"), name='success_deletion'),
    ]

并在success_deletion视图中使用delete_issue网址进行重定向:

def delete_issue(request,project_id,issue_id):
    if not request.user.is_staff or not request.user.is_superuser:
        raise Http404
    issue = get_object_or_404(Issue,id=issue_id)
    issue.delete()
    return redirect('success_deletion')