0

I want to delete an object via DeleteView.

my views.py looks like this:

class HabitDelete(DeleteView):
model = Habit
success_url = reverse_lazy('renderOverview')
template_name = 'overview/habit_delete_confirm.html'

my urls.py looks like this:

    url(r'^details/(?P<pk>\w{0,50})/delete/$', overview_views.HabitDelete.as_view(), name='deleteHabit'),

my template:

    {% block content %}
  <div class="container custom-div-habit-detail">
    <h1 class="custom-title-label">{{habit.title}}</h1>
    <!-- <a href="{% url 'deleteHabit' habit.id %}">
      <span class="glyphicon glyphicon-trash custom-glyphicon-trash"></span>
    </a> -->
    <form action="{% url 'deleteHabit' pk=habit.id %}" method="POST" class="custom-form-trash">
      {% csrf_token %}
      <button type="submit" class="btn btn-default custom-btn-trash">
        <span class="glyphicon glyphicon-trash custom-glyphicon-trash"></span>
      </button>
    </form>
    <br>
    <br>
    <p class="custom-trigger-label custom-padding-top-30">{{habit.trigger}}...<p>
    <p class="custom-routinebehavior-label">{{habit.existingroutine}}<p><br>
    <p class="custom-trigger-label">I will...</p>
    <p class="custom-routinebehavior-label">{{habit.targetbehavior}}</p><br>
    <p> <img src="{{ habit.image_url }}" class="img-responsive custom-image-detail" width="100"></p>
    <br>
    <a class="btn btn-default custom-tab-item" href="/overview">
      <span class="glyphicon glyphicon-chevron-left"></span>
      <span class="tab-label">Go back</span>
    </a>

    <button class="btn btn-default custom-tab-item" id="myform">
      <span class="glyphicon glyphicon-ok"></span>
      <span class="tab-label">Let's go</span>
    </button>
  </div>
{% endblock %}

and the folder templates/overview contains habit_delete_confirm.html. My object 'habit' will be deleted successful, but I don't know why I do not get a confirmation template as usual.

Texas
  • 559
  • 2
  • 6
  • 18
  • Do you mean by 'as usual' like when you delete objects in the admin? Because the django admin does not use a `DeleteView`. – user2390182 May 27 '17 at 19:22
  • As usual means that I delete other objects on this way in my project. Acutally to do this, I use the url like this url(r'^delete/(?P\w{0,50})/$', targetbehavior_views.TargetBehaviorDelete.as_view(), name='deleteTargetbehavior,') but on this context, I visit detail of a habit first with url(r'^details/(?P\w{0,50})/$', overview_views.renderDetails, name='renderDetails'), and then I call the delete function if I click on a form (see template above) My console prints the msg: Not Found: /overview/details/17/None – Texas May 27 '17 at 19:29
  • Acutually I wanna use the url \w{0,50})/$'> to visit the details of an object (that works) and click on a button wrapped in a form, that use HabitDeleteForm.as_view() with this url \w{0,50})/delete/$', overview_views.HabitDelete.as_view(), name='deleteHabit')> to delete an object. – Texas May 27 '17 at 19:41

0 Answers0