Search code examples
pythondjangodjango-modelsdjango-viewsdjango-generic-views

DeleteView doesn't delete and just refreshes the delete page


When I click on my delete project link it takes me to my delete page with a button to click which should delete the model data of the project and then take me to the profile page. However when I click the delete button, the page just refreshes and no data gets deleted?! What am I doing wrong here? Any help would be much appreciated :-)

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 template

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

Urls

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 template which has the delete link on

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

Solution

  • You must use DeleteView not UpdateView. See here.