我想重定向一個視圖,以便它顯示標題和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'),
]
有什麼建議? 感謝
什麼錯誤? – Selcuk 2015-02-06 12:47:17
這是我得到的錯誤:TypeError at/entities/12786692/ get()缺少1所需的位置參數:'args' – Torostar 2015-02-06 12:48:00