【问题标题】:DJANGO: TemplateDoesNotExist: auth/user_confirm_delete.htmlDJANGO: TemplateDoesNotExist: auth/user_confirm_delete.html
【发布时间】:2018-06-02 08:23:01
【问题描述】:

我正在尝试在 Django 中创建“删除帐户”功能。我为此使用DeleteView

问题是在调用这个视图之后,Django raises:

Request Method: GET
Request URL:    http://127.0.0.1:8000/profiles/delete-account/
Django Version: 1.11.7
Exception Type: TemplateDoesNotExist
Exception Value:    
auth/user_confirm_delete.html

我的看法:

class DeleteAccount(LoginRequiredMixin,DeleteView):
    model = User

    def get_object(self, queryset=None):
        user = self.request.user
        userprofile = user.userprofile
        userprofile.prepare_to_delete_account()
        return user

为什么它会尝试渲染这个template,如果它被调用,为什么没有这样的模板?

你知道如何解决这个问题吗?

【问题讨论】:

    标签: django django-models django-views django-authentication


    【解决方案1】:

    视图呈现模板以确认您要删除对象。

    默认情况下,它会根据应用和模型名称生成模板名称auth/user_confirm_delete.html

    如果要使用其他模板,请设置template_name

    class DeleteAccount(LoginRequiredMixin,DeleteView):
        model = User
        template_name = 'delete_account.html'
    

    您必须创建模板,Django 不包含它。有一个示例 in the docs 可以扩展:

    <form action="" method="post">{% csrf_token %}
        <p>Are you sure you want to delete "{{ object }}"?</p>
        <input type="submit" value="Confirm" />
    </form>
    

    【讨论】:

    • 我明白了。为此,将 GET 更改为 POST 就足够了,谢谢。
    • 如果您这样做,如果用户或机器人直接使用 GET 请求访问 URL,您仍然可能会收到服务器错误。您可以设置http_method_names = ['POST'] 来避免这种情况。
    猜你喜欢
    • 2011-06-13
    • 2014-10-17
    • 1970-01-01
    • 2018-09-29
    • 1970-01-01
    • 2014-02-19
    • 2016-11-16
    • 2023-03-28
    • 2013-06-20
    相关资源
    最近更新 更多