简体   繁体   中英

How to render a html page without view model?

Is it possible to render an HTML page without having a view model in Django if a page is going to display only static HTML?

Basically, I want to delete an issue from a webpage and then show a 'successfully deleted' static HTML page after deleting.

But I got blew error, anyone could help?

NoReverseMatch at /project/1/issue/14/delete_issue/ Reverse for 'nice_delete.html' not found. 'nice_delete.html' is not a valid view function or pattern name.

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 %}

You can use TemplateView for this. Just add to your 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'),
    ]

And use success_deletion url in delete_issue view for redirection:

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')

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM