I have been following a wonderful four hour crash course on another platform, and things went swimmingly. At time 3:37hr 3:42hr of 3:45hr, I ran into a fundamental breakdown of my understanding.
A “raw” delete class based view, is deleting a record in the “Courses” dataset. After delete, the view named ‘courses-list’ is displayed listing all the records currently in the database… except, it is still showing the old record as if it was still there. Log into admin, and the record is definitely deleted.
SUPER DUPER CLUE and exactly why I know this is a lack of my understanding fundamentally. If refresh the page, old record still there. Go to somewhere else in the website and return to “/courses/”, deleted record still there. Clear browser cache and reload page, record still there. BUT if I stop the Django server and restart, hit refresh on the page, the deleted record is gone.
Please help, and let me know what else you need from me.
EDIT - Python 3.6.9 and Django 2.0.7 on Ubuntu 18.04
**courses.views.py**
class CourseView(View):
template_name = "courses/course_detail.html" # DetailView
def get(self, request, id=None, *args, **kwargs):
# GET method
context = {}
if id is not None:
obj = get_object_or_404(Course, id=id)
context['object'] = obj
return render(request, self.template_name, context)
class CourseListView(View):
template_name = "courses/course_list.html"
queryset = Course.objects.all()
def get_queryset(self):
return self.queryset
def get(self, request, *args, **kwargs):
context = {"object_list": self.get_queryset()}
return render(request, self.template_name, context)
class CourseDeleteView(View):
template_name = "courses/course_delete.html"
def get_object(self):
id = self.kwargs.get('id')
obj = None
if id is not None:
obj = get_object_or_404(Course, id=id)
return obj
def get(self, request, id=None, *args, **kwargs):
# GET method
context = {}
obj = self.get_object()
if obj is not None:
context['object'] = obj
return render(request, self.template_name, context)
def post(self, request, id=None, *args, **kwargs):
# POST method
context = {}
obj = self.get_object()
if obj is not None:
obj.delete()
context['object'] = None
return redirect('/courses/')
return render(request, self.template_name, context)
**courses.urls.py**
from django.urls import path
from .views import (
CourseView,
CourseListView,
CourseCreateView,
CourseUpdateView,
CourseDeleteView
)
app_name = 'courses'
urlpatterns = [
path('', CourseListView.as_view(), name='courses-list'),
path('create/', CourseCreateView.as_view(), name='courses-create'),
path('<int:id>/', CourseView.as_view(), name='courses-detail'), # keyword arg of pk or a slug default
path('<int:id>/update/', CourseUpdateView.as_view(), name='courses-update'),
path('<int:id>/delete/', CourseDeleteView.as_view(), name='courses-delete')
]
**courses_delete.html**
{% extends 'base.html' %}
{% block content %}
<form action='.' method='POST'>{% csrf_token %}
<h1>Do you want to delete the course "{{ object.title }}"?</h1>
<p><input type='submit' value='Yes' /> <a href='../'>Cancel</a></p>
</form>
{% endblock %}
**course_list.html**
{% extends 'base.html' %}
{% block content %}
<!-- {{ object_list }} -->
{% for instance in object_list %}
<p>{{ instance.id }} - {{ instance.title }}</p>
{% endfor %}
{% endblock %}