Django 2.1.3 错误:__init__() 需要 1 个位置参数,但给出了 2 个 [英] Django 2.1.3 Error: __init__() takes 1 positional argument but 2 were given
问题描述
我正在尝试使用 Django 2.1.3 和 python 3.7.1 开发一个网站当我转到主页时,出现此错误:
I am trying to develop a website with Django 2.1.3 and python 3.7.1 When I go to the homepage I get this error:
TypeError at / __init__() takes 1 positional argument but 2 were given
以下是我编写的代码的一些细节:
Here are some details about the code I write:
追溯
Environment:
Request Method: GET
Request URL: http://127.0.0.1:8000/
Django Version: 2.1.3
Python Version: 3.7.1
Installed Applications:
['django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'core',
'crispy_forms']
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.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware']
Traceback:
File "C:UsersandreAppDataLocalProgramsPythonPython37-32libsite-packagesdjangocorehandlersexception.py" in inner
34. response = get_response(request)
File "C:UsersandreAppDataLocalProgramsPythonPython37-32libsite-packagesdjangocorehandlersase.py" in _get_response
126. response = self.process_exception_by_middleware(e, request)
File "C:UsersandreAppDataLocalProgramsPythonPython37-32libsite-packagesdjangocorehandlersase.py" in _get_response
124. response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "C:UsersandreAppDataLocalProgramsPythonPython37-32libsite-packagesdjangocontribauthdecorators.py" in _wrapped_view
21. return view_func(request, *args, **kwargs)
Exception Type: TypeError at /
Exception Value: __init__() takes 1 positional argument but 2 were given
core/models.py
core/models.py
这只是数据库中的一张表:
This is just one table on DB:
from django.db import models
class Evento(models.Model):
titolo_evento = models.CharField(max_length=30)
data_inizio = models.DateTimeField()
data_fine = models.DateTimeField()
def __str__(self):
return self.titolo_evento
class Meta:
verbose_name = 'Evento'
verbose_name_plural = 'Eventi'
核心/views.py
core/views.py
在这里我想看到数据库Eventi"只有在用户通过身份验证时才在主页上,我认为错误在这里,但我不知道在哪里:
Here I want to see the DB "Eventi" on the homepage only if the user is authenticated, i think the mistake is here, but i don't know where:
from django.contrib.auth.decorators import login_required
from .models import Evento
from django.views.generic.list import ListView
@login_required
class HomeView(ListView):
queryset = Evento.objects.all()
template_name = 'core/homepage.html'
context_object_name = 'lista_eventi'
核心/urls.py
from django.urls import path
from . import views
urlpatterns = [
path('', views.HomeView, name='homepage'),
]
urls.py(项目)
urls.py(project)
from django.contrib import admin
from django.urls import path, include
urlpatterns = [
path('admin/', admin.site.urls),
path('', include('core.urls')),
path('accounts/', include('django.contrib.auth.urls'))
]
推荐答案
您需要使用 as_view() 在基于类的视图结束时在 url 中声明:
You need use as_view() at the end of class based views when declaring in the urls:
path('', views.HomeView.as_view(), name='homepage'),
另外,当使用login_required
装饰器时,你需要在CBV的dispatch方法上使用它:
Also, when using login_required
decorator, you need to use it on dispatch method of CBV:
from django.contrib.auth.decorators import login_required
from django.utils.decorators import method_decorator
class HomeView(ListView):
@method_decorator(login_required)
def dispatch(self, *args, **kwargs):
return super(HomeView, self).dispatch(*args, **kwargs)
这篇关于Django 2.1.3 错误:__init__() 需要 1 个位置参数,但给出了 2 个的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!