1

I am writing a basic Events app which contains of two modules(apps) so far : users and events.

I am using Django 2.1 with Python 3.6 on Ubuntu 16.04

So far, I've been able to handle users, but on events, I can't use Update, Detail and Delete generic views. All of them return 404.

My views.py:

class EventListView(ListView):

    model = EventModel
    template_name = 'event_list.html'
    queryset = EventModel.objects.order_by('start_date_time')


class EventUpdateView(UpdateView):

    model = EventModel
    fields = ['event_type','start_date_time'
              ]
    template_name = 'event_update.html'


class EventDeleteView(DeleteView):

    model = EventModel
    template_name = 'event_delete.html'
    success_url = reverse_lazy('event_list')


class EventDetailView(DetailView):

    model = EventModel
    template_name = 'event_detail.html'

My urls.py (in project folder):

urlpatterns = [
path('', include('pages.urls')),
path('admin/', admin.site.urls),
path('users/', include('users.urls')),
path('users/', include('django.contrib.auth.urls')),
path('events/', include('events.urls')),
]

My urls.py (in events app):

urlpatterns = [
path('', views.EventListView.as_view(), name='event_list'),
path('<int:id>', views.EventDetailView.as_view(), name='event_detail'),
path('<int:id>/edit/', views.EventUpdateView.as_view(), name='event_update'),
path('<int:id>/delete/', views.EventDeleteView.as_view(), name='event_delete'),
]

What am I doing wrong? I've been searching the whole day and still have no idea how this might be wrong. Note that the first line works (EventListView) but the other lines don't. By the way, I am using the book Django for Beginners. Most of the code here is identical to the code in the book.

Update

I don't use namespace in this application, the rest of urls.py is only some basic imports :

from django.urls import path
from . import views

The urls.py for the Project is like above, except it has include and admin as well.

The examples of URLs giving 404 error:

http://127.0.0.1:8000/events/1/
http://127.0.0.1:8000/events/1/edit/

PS I thought edit and delete give me 404, but actually the error is :

ImproperlyConfigured at /events/1/edit/ EventUpdateView is missing a QuerySet. Define EventUpdateView.model, EventUpdateView.queryset, or override EventUpdateView.get_queryset().)

6
  • Did you define a app_namespace? Please post the full urls.py files, not the "limited" version. Commented Aug 19, 2018 at 11:58
  • 1
    How are you trying to use them? Please add examples of URLs giving you 404. Are you sure you are trying to make appropriate POST or DELETE and so on requests? not GET. Commented Aug 19, 2018 at 11:58
  • @IvanStarostin Using my browser, I am trying to access them. I haven't implemented any POST or GET explicitly. I am using generic views like above. Commented Aug 19, 2018 at 12:06
  • Well, at least it's not supposed to be models = EventModel it's model property. Not model>>S<<. Commented Aug 19, 2018 at 12:14
  • @IvanStarostin wow! fixed it, but still have the same error. The error on edit and delete is now changed to : Generic detail view EventUpdateView must be called with either an object pk or a slug in the URLconf. which I think I'm caling with pk, but don't know why django is not detecting it. Commented Aug 19, 2018 at 12:17

1 Answer 1

1

In short: you defined a models (with s) attribute, but it should be model (without s).

Well the error actually already explains the problem:

ImproperlyConfigured at /events/1/edit/ EventUpdateView is missing a QuerySet. 
Define EventUpdateView.model, EventUpdateView.queryset,
or override EventUpdateView.get_queryset().)

In your EventUpdateView you did not specify a model attribute, you wrote models, and for Django that is an entirely different attribute. So you should rename it to:

class EventListView(ListView):
    model = EventModel
    template_name = 'event_list.html'
    queryset = EventModel.objects.order_by('start_date_time')

class EventUpdateView(UpdateView):
    model = EventModel
    fields = ['event_type','start_date_time'
              ]
    template_name = 'event_update.html'

class EventDeleteView(DeleteView):
    model = EventModel
    template_name = 'event_delete.html'
    success_url = reverse_lazy('event_list')


class EventDetailView(DetailView):
    model = EventModel
    template_name = 'event_detail.html'

For the EventListView, that did not matter, since you also defined a queryset attribute, and so Django took that one, but I would update it anyway.

Furthermore in the urls.py, you need to specify a pk parameter by default:

urlpatterns = [
    path('', views.EventListView.as_view(), name='event_list'),
    path('<int:pk>', views.EventDetailView.as_view(), name='event_detail'),
    path('<int:pk>/edit/', views.EventUpdateView.as_view(), name='event_update'),
    path('<int:pk>/delete/', views.EventDeleteView.as_view(), name='event_delete'),
]

Finally in the template you wrote something like:

{% url 'event_update' event.id %}

But apparently there was no event identifier, as a result the event.id is the string_if_invalid (by default the empty string), which is not an integer (well at least not if you did not specify that), and hence it can not find a relevant URL. After some discussion, it turned out that the correct identifier was object, so the correct url is something like:

{% url 'event_update' pk=object.id %}

The same of course should happen with other {% url ... %} calls.

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

9 Comments

Thanks for the tip! actually Ivan mentioned that and I fixed it, but still I see 404 in URL/events/1/ link
@aLuViAn: yes that is because of a second problem: you named the parameter id, instead of pk. So either you update the get_object function, or you use pk.
Fixed that as well, thanks! Now my edit and delete work fine. But still I get 404 on my events/1/
@aLuViAn: are you sure there exists an Event with the given id?
Yes I checked the database. And my bad, the error has changed to NoReverseMatch...I think its coming from event_detail.html :: <p><a href="{% url 'event_update' event.id %}">Edit</a> | <a href="{% url 'event_delete' event.id %}">Delete</a></p> which should work fine, cos I have the exact same line in event_list.html and it works
|

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.