2

I have overwritten the dispatch methods on my view classes so they require the user to be logged in.

Example:

class IndexView(generic.ListView):
    template_name='quotes/index.html'
    context_object_name = 'book_list'

    @method_decorator(login_required)
    def dispatch(self, args):
        return super(IndexView, self).dispatch(args)
    def get_queryset(self):
        """Return all the book objects"""
        return Book.objects.all()

class DetailView(generic.DetailView):
   model = Book
   template_name = 'quotes/detail.html'

   @method_decorator(login_required)
   def dispatch(self, request, args, kwargs):
        return super(DetailView, self).dispatch(request, args, kwargs)
   def get_queryset(self):
       """
       Excludes any polls that aren't yet published
       """
       return Book.objects.all()

This works nicely until I use a URL pattern like this:

     url(r'^book/(?P<pk>\d+)/$', views.DetailView.as_view(), name='detail'),

Then it gives me the error

    dispatch() got an unexpected keyword argument 'pk'

DetailView needs that argument for the query_set function yet the dispatch is catching it and complaining about it. This has happened to me before. I removed all the login functionality and had to start again. I replaced it all and it worked fine even with the pk arg. Now its doing it again and I can't find any reason online. Can anyone help. Will post whatever else you think is necessary.

I know you can add login_required to the URL patterns. Am I just going about this the wrong way? The Django documentation suggests the very method that I am using.

quotes/index.html

    {% extends "base.html" %}

{% block content %}
{% load staticfiles %}

<h2> Books </h2>
{% if book_list %}
    <ul>
    {% for book in book_list %}     
        <li><a href="{% url 'quotes:detail' book.id %}">{{ book.title }}</a></li>
    {% endfor %}
    </ul>
{% else %}
    <p>No books are available.<p>
{% endif %}
<a href="{% url 'quotes:newQuote' %}"><button class="btn btn-primary">Create a New Quote</button></a>
<a href="{% url 'quotes:newBook' %}"><button class="btn btn-primary">Create a New Book</button></a>


{% endblock %}

quotes/urls.py

    from django.conf.urls import patterns, url
from django.contrib.auth.decorators import login_required

from quotes import views

urlpatterns = patterns('',
    #no extra route sp (quotes/)
    url(r'^$', views.IndexView.as_view(), name='index'),
    #looks for id value next to /quotes/ and activates the detail view
    #this regex capture the id value in <pk> probably lt and mt characters define this
    #all these are 'quotes/andthensomething/'
    url(r'^book/(?P<pk>\d+)/$', views.DetailView.as_view(), name='detail'),
    url(r'^quote/(?P<pk>\d+)/$', views.QuoteDetailView.as_view(), name='quoteDetail'),
    url(r'^quote/(?P<quoteID>\d+)/edit/$', views.QuoteEditView2 , name='editQuote'),
    url(r'^quotes/', views.ListQuotesView.as_view(), name='listQuotes'),
    url(r'^newQuote/', views.newQuote, name='newQuote'),
    url(r'^newBook/', views.newBook, name='newBook'),
    url(r'^createQuote/', views.createQuote, name='createQuote'),
    url(r'^createBook/', views.createBook, name='createBook'),
    url(r'^book/(?P<bookID>\d+)/delete/$', views.deleteBook, name='deleteBook'),
    url(r'^quote/(?P<quoteID>\d+)/delete/$', views.deleteQuote, name='deleteQuote'),
    url(r'^book/(?P<pk>\d+)/edit/$', views.BookEditView.as_view(), name='editBook'),
    url(r'^quote/(?P<quoteID>\d+)/update/$', views.updateQuote, name='updateQuote'),
    url(r'^book/(?P<bookID>\d+)/update/$', views.updateBook, name='updateBook'),
    url(r'^logout/', views.logout_view, name="logout"),
    #url(r'^accounts/login/', views.loginProper, name='loginProper'),
)

1 Answer 1

5

The correct method call for the dispatch is:

def dispatch(self, request, *args, **kwargs):

So you should:

def dispatch(self, request, *args, **kwargs):
    return super(IndexView, self).dispatch(request, *args, **kwargs)
4
  • Update your question with the urls.py, also since you are calling a listview and the detailview is triggered this means that you are asking in your template to render the link to a detailview of an item (typical for a listview), can you output how you render the links in your quotes/index.html template? (check your {% url 'detail' book.id %} )
    – petkostas
    Jan 21, 2014 at 16:50
  • The IndexView is not the class i was calling. It was just an example, i have put the class i was calling underneath it.
    – lorless
    Jan 21, 2014 at 17:05
  • 1
    @dlampard you need to be sure you have the single and double asterisks exactly as petkostas shows. Can you please check, and update your question. Jan 21, 2014 at 17:09
  • That fixed it, in the documentation the asterisks are used as pointers to footnotes. I removed them as I'd never seen that sort of syntax before and thought they just meant footnotes.
    – lorless
    Jan 21, 2014 at 17:15

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.