2015-02-06 97 views
1

我想重定向一個視圖,以便它顯示標題和id_number,但是我在標題中出現錯誤。get()缺少1個必需的位置參數:'args',Django slug RedirectView

型號:

from django.db import models 
from django.core.urlresolvers import reverse 
from autoslug import AutoSlugField 


class EntitiesNew(models.Model): 
    id = models.CharField(max_length=20, blank=True) 
    description = models.CharField(max_length=500, blank=True) 
    title = models.CharField(max_length=500, blank=True) 
    id_number = models.IntegerField(primary_key=True) 
    slug = AutoSlugField(populate_from='title', max_length=500) 

    class Meta: 
     managed = True 
     db_table = 'entities_new' 

    def get_absolute_url(self): 
     return reverse('entity-detail', kwargs={'slug': self.slug}) 

    def __unicode(self): 
     return self.title 

瀏覽:

from entities.models import EntitiesNew 
from django.views import generic 
from django.views.generic import TemplateView, RedirectView, ListView, DetailView 

class EntityNewView(generic.ListView): 
    template_name = 'entities/index.html' 
    context_object_name = 'entities_list' 
    def get_queryset(self): 
     return EntitiesNew.objects.order_by('-id_number')[:50] 


class EntityDetailView(TemplateView): 
    template_name = 'entities/detail.html' 
    def get_context_data(self, **kwargs): 
     context=super(EntityDetailView, self).get_context_data(**kwargs) 
     context['entity']=EntitiesNew.objects.get(pk=self.kwargs.get('id_number', None)) 
     return context 

class EntityRedirectDetailView(RedirectView): 

    def get(self, request, args, **kwargs): 
     id_number = self.kwargs.get('id_number', None) 
     entity = EntitiesNew.objects.get(pk=id_number) 
     self.url = '/entities/%s-%s' % (entity.id, entity.slug) 
     return super(EntityRedirectDetailView, self).get(request, args, **kwargs) 

網址:

from django.conf.urls import patterns,url 
from entities import views 

urlpatterns = [ 
    url(r'^$', views.EntityNewView.as_view(), name='entities'), 
    url(r'^(?P<id_number>\d+)/$', views.EntityRedirectDetailView.as_view(), name='entity-redirect'), 
    url(r'^(?P<id_number>\d+)-(?P<slug>\w+)/$', views.EntityDetailView.as_view(), name='entity-detail'), 

] 

有什麼建議? 感謝

+0

什麼錯誤? – Selcuk 2015-02-06 12:47:17

+0

這是我得到的錯誤:TypeError at/entities/12786692/ get()缺少1所需的位置參數:'args' – Torostar 2015-02-06 12:48:00

回答

2

你已經錯過了在get函數的簽名*

def get(self, request, *args, **kwargs): 
+0

感謝Daniel,我的愚蠢的錯誤...但是現在,如果我點擊顯示的實體之一I 'm重定向到例如:entities/Samuel%20Nalot- – Torostar 2015-02-06 12:58:14

+0

我不太清楚你在說什麼:該代碼會將你重定向到/ entities/ - /。 – 2015-02-06 13:13:20

+0

是的,這就是我想要做的......但是如果我點擊EntityNewView的get_queryset顯示的實體,它會重定向到url實體/ 12786445-跳過部分......並找不到頁面( 404)顯示 – Torostar 2015-02-06 13:24:15

相關問題