0

I have a ListView and from this I want to delete some objects using DeleteView. What I have so far:

views.py

class BlockListView(ListView):
    model= Classroom

    def get_context_data(self, **kwargs):
        context = super(BlockListView, self).get_context_data(**kwargs)
        classroom_blocks = Classroom.objects.all()
        context = {'classroom_blocks': classroom_blocks}
        return context

list_classroom_view = BlockListView.as_view()

class BlockDeleteView(DeleteView):
    model = Classroom
    success_url = reverse_lazy('classroom:blocklist')

delete_classroom_view = BlockDeleteView.as_view()

urls.py

urlpatterns = [
    path(r'^$', views.index, name='index'),
    path('submitted', views.submitted, name='submitted'),
    path('classup/', create_classroom_view, name='classroom'),
    path('block/', views.block, name='block'),
    path('blocklist/', list_classroom_view, name='blocklist'),
    path(r'^(?P<pk>\d+)/blockDelete/$', delete_classroom_view, name='blockDelete'),
]

template for listview:

{% for block in classroom_blocks %}
    <li>{{ block.get_course_block_display }}<a href ="{% url 'classroom:blockDelete' block.id %}" class="button" style="color:#999999">DELETE</a></li>
{% empty %}
    <li>No classes set up yet.</li>
{% endfor %}

template for confirm delete:

{% block body %}
<h1>Confirm Delete</h1>
<form action="." method="post">{% csrf_token %}
    <p>Are you sure you want to delete "{{ object.course_block }}"?</p>
    <input type="submit" value="Confirm" />
</form>
{% endblock %}

After I click the delete button from the listview, the url generated is http://127.0.0.1:8000/classroom/%5E(%3FP89%5Cd+)/blockDelete/$ and this directs to the confirm delete page. After confirming delete, I get a 404 error with Request URL: http://127.0.0.1:8000/classroom/%5E(%3FP89%5Cd+)/blockDelete/

1 Answer 1

1

You are using path(), so you shouldn't be using regexes. Change the first and last URL patterns to:

path('', views.index, name='index'),
...
path('<int:pk>/blockDelete/', delete_classroom_view, name='blockDelete'),

Now, when you click on the delete button from the list view, you should be taken to a url like /classroom/1/blockDelete/, and you shouldn't get a 404 when you submit the form.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.