gpt4 book ai didi

python - get() 缺少 1 个必需的位置参数 : 'args' , Django slug RedirectView

转载 作者:行者123 更新时间:2023-12-01 04:51:28 26 4
gpt4 key购买 nike

我想重定向 View ,以便它显示标题和 id_number,但我在标题中收到错误。

型号:

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'),

]

有什么建议吗?谢谢

最佳答案

您错过了 get 函数签名中的 *:

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

关于python - get() 缺少 1 个必需的位置参数 : 'args' , Django slug RedirectView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28366065/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com