mptt-urls
加注:
/ activities / test /
中的TypeError
__init__() takes exactly 1 argument (4 given)
这是我的网址格式:
url(r'^(?P<path>.*)/', mptt_urls.view(model='activities.models.Category',
view='activities.views.Category', slug_field='slug')),
我的观点:
class Category(TemplateView):
template_name = 'activities/articles_list.html'
def get_context_data(self, **kwargs):
c = super(Category, self).get_context_data(**kwargs)
c['articles'] = models.Article.objects.filter(Category=self)
return c
我做错了什么?
Environment:
Request Method: GET
Request URL: http://127.0.0.1:8000/activities/test/
Django Version: 1.9.7
Python Version: 2.7.11
Installed Applications:
['django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'taggit',
'mptt',
'ckeditor',
'easy_thumbnails',
'activities',
'common',
'images']
Installed Middleware:
['django.middleware.security.SecurityMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.auth.middleware.SessionAuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware']
Traceback:
File "/home/dukeimg/PycharmProjects/dorogi/env/local/lib/python2.7/site-packages/django/core/handlers/base.py" in get_response
149. response = self.process_exception_by_middleware(e, request)
File "/home/dukeimg/PycharmProjects/dorogi/env/local/lib/python2.7/site-packages/django/core/handlers/base.py" in get_response
147. response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/home/dukeimg/PycharmProjects/dorogi/env/local/lib/python2.7/site-packages/mptt_urls/__init__.py" in __call__
38. return self.view(*args, **kwargs)
Exception Type: TypeError at /activities/test/
Exception Value: __init__() takes exactly 1 argument (4 given)
答案 0 :(得分:2)
这可能对您有用:
def category(request, path, instance):
l = get_list_or_404(models.Category.objects.all(), slug=path)
return render(
request,
'activities/activity_list.html',
{
'articles': l,
'activities': models.Category.objects.filter(level__lte=0)
}
)
答案 1 :(得分:1)
我不知道你要做什么。
url(r'^(?P<path>.*)/', mptt_urls.view(model='activities.models.Category',
view='activities.views.Category', slug_field='slug')),
有两种方法可以呈现基于视图和函数的响应类视图。 但是您正在定义函数的URL,并且您正在使用基于类的视图,这就是您收到错误的原因。
如何解决此问题
基于类的视图 更改网址
url(r'^(?P<path>.*)/', Category.as_view(), slug_field='slug')),
观点会是。
class Category(TemplateView):
template_name = 'activities/articles_list.html'
def get_context_data(self, **kwargs):
c = super(Category, self).get_context_data(**kwargs)
c['articles'] = models.Article.objects.filter(Category=self)
return c
基于功能的视图 Urls.py
url(r'^(?P<path>.*)/','activities.views.Category', slug_field='slug')),
Views.py
def类别(请求,路径): l = get_list_or_404(models.Category.objects.all(),slug = path)
return render(
request,
'activities/activity_list.html',
{
'articles': l,
'activities': models.Category.objects.filter(level__lte=0)
}
)
希望这有帮助。