DeleteView不会删除,只刷新删除页面

时间:2015-11-25 19:42:23

标签: python django django-models django-views django-generic-views

当我点击我的删除项目链接时,它会带我到我的删除页面,点击一个按钮,删除项目的模型数据,然后带我到个人资料页面。但是,当我单击删除按钮时,页面只刷新并且没有数据被删除?! 我在这做错了什么?任何帮助将不胜感激: - )

浏览

class DeleteProject(UpdateView):
    model = UserProject
    template_name = 'howdidu/delete_project.html'

    def get_object(self, queryset=None):
        obj = super(DeleteProject, self).get_object()
        if not obj.user == self.request.user:
            raise Http404
        return obj

    def get_success_url(self):
        project_username = self.request.user.username
        #project_slug = self.object.slug
        return reverse('user_profile', kwargs={'username':project_username})

delete_project.html模板

{% extends 'howdidu/base.html' %}

{% load staticfiles %}

{% block title %}Delete project{% endblock %}

{% block body_block %}

        <h1>Delete project</h1>

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

{% endblock %}

网址

urlpatterns = patterns('',
        url(r'^$', views.index, name='index'),
        url(r'^register_profile/$', views.register_profile, name='register_profile'),
        url(r'^update_profile/$', views.update_profile, name='update_profile'),
        url(r'^create_project/$', login_required(views.CreateProject.as_view()), name='create_project'),
        url(r'^(?P<username>\w+)/(?P<slug>[-\w]+)/update_project/$', login_required(views.UpdateProject.as_view()), name='update_project'),
        url(r'^(?P<username>\w+)/(?P<slug>[-\w]+)/delete_project/$', login_required(views.DeleteProject.as_view()), name='delete_project'),
        url(r'^(?P<username>\w+)/$', views.profile_page, name='user_profile'),
        url(r'^(?P<username>\w+)/(?P<slug>[-\w]+)/$', views.project_page, name='user_project'),

        )

Project.html模板,其中包含删除链接

{% extends 'howdidu/base.html' %}

{% load staticfiles %}

{% block title %}Project{% endblock %}

{% block body_block %}

        {% if project %}

        <h1>{{ project.title }}</h1>
        <img src="{{ project.project_picture.url }}" width = "300" height = "300"  />
        <h3>{{ project.project_overview }}</h3>
        {% if user.is_authenticated %}
        {% if project_user.username == user.username %}
        <p><a href="{% url 'update_project' project_user.username project.slug %}">Edit project</a></p>
        <p><a href="{% url 'delete_project' project_user.username project.slug %}">Delete project</a></p>
        {% endif %}
        {% endif %}


        {% else %}
            The specified project {{ project.title }} does not exist!
        {% endif %}

{% endblock %}

1 个答案:

答案 0 :(得分:0)

您必须使用DeleteView而不是UpdateView。 See here.