2014-01-21 64 views
2
得到了一個意想不到的關鍵字參數

我已經覆蓋在我的視圖類的調度方法,因此它們需要用戶先登錄Django的調度()在Django

例:

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() 

這很好地工作直到我用這樣的URL模式:

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

然後,它給我的錯誤

dispatch() got an unexpected keyword argument 'pk' 

DetailView需要query_set函數的參數,但是dispatch捕獲它並抱怨它。這發生在我以前。我刪除了所有的登錄功能,不得不重新開始。我全部替換了它,即使使用pk arg,它也能正常工作。現在它再做一次,我在網上找不到任何理由。任何人都可以幫忙將張貼任何你認爲有必要的東西。

我知道你可以添加login_required到URL模式。我只是以這種錯誤的方式去做? Django文檔提供了我正在使用的方法。

報價/ 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 %} 

報價/ 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'), 
) 

回答

5

正確的方法調用的調度是:

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

所以你應該:

def dispatch(self, request, *args, **kwargs): 
    return super(IndexView, self).dispatch(request, *args, **kwargs) 
+0

即使有額外的參數也一樣錯誤 – lorless

+0

用urls.py更新你的問題,因爲你正在調用一個listview並且detailview被觸發,這意味着你要求在你的模板中顯示鏈接到一個detailview一個項目(一個典型的listview),你可以輸出你如何渲染鏈接到你的quotes/index.html模板中嗎? (檢查你的'{%url'detail'book.id%}') – petkostas

+0

IndexView不是我打電話的班級。這只是一個例子,我已經把我在下面調用的課程。 – lorless