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
的网站,当我转到首页时出现此错误:
/ __init __()处的TypeError接受1个位置参数,但给出2个位置
这里有一些我写的代码的详细信息:
回退
环境:
请求方法:GET
请求URL:http://127.0.0.1:8000/
Django版本:2.1.3
Python版本:3.7.1
已安装的应用程序:
['django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'core',
'crispy_forms']
已安装中间件:
['django.middleware.security.SecurityMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware .csrf.CsrfViewMiddlewa re,
django.contrib.auth.middleware.AuthenticationMiddleware,
django.contrib.messages.middleware.MessageMiddleware,
django.middleware.clickjacking.XFrameOptionsMiddleware]
追溯:
文件 C:\Users\andre\AppData\Local\Programs\Python\Python37内部
中的-32\lib\site-packages\django\core\handlers\exception.py。response = get_response(request)
文件 C :_User_andre\AppData\Local\Programs\Python\Python37-32\lib\site-packagesdjango\core\handlers\base.py in _get_response
126。response = self.process_exception_by_middleware(e,request)
文件 C:\Users\andre\AppData\Local\Programs\Python\Python37-32 _lib_site_packagesbdjango\core\handlers\base.py,位于_get_response
124中。response = wrapd_callback(request,* callback_args,** callback_kwargs)
文件 C:\用户\andre\AppData\本地\程序\Python\Pyth on37-32\lib\site-packages\django\contrib\auth\decorators.py _wrapped_view
21. return view_func(request,* args,** kwargs)
异常类型:TypeError at /
异常值:__init __()接受1个位置参数,但给了2个
core / models.py
这只是数据库上的一个表:
<$ p从django.db导入模型中的$ p>
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
类Meta:
verbose_name ='Evento'
verbose_name_plural ='Eventi'
core / views.py
在这里,我只想在用户通过身份验证后才能在主页上看到数据库 Eventi,我认为错误在这里,但是我不要知道w其中:
从django.contrib.auth.decorators导入login_required
从.models导入Evento
从django.views.generic.list导入ListView
@login_required
类HomeView(ListView):
queryset = Evento.objects.all()
template_name ='core / homepage.html'
context_object_name ='lista_eventi'
core / django.urls的urls.py
导入路径。导入视图
urlpatterns = [
path('',views.HomeView,name ='homepage'),
]
urls.py(项目)
从django.contrib导入admin
从django.urls导入路径,包括
urlpatterns = [
path('admin /',admin.site.urls),
path('',include('core.urls')),
path('accounts /',include('django.contrib.auth.urls'))
]
您需要使用 as_view()在基于类的视图末尾在网址中声明时:
path('',views.HomeView.as_view(),name ='homepage'),
此外,当使用 login_required
装饰器时,您需要在CBV的分派方法上使用它:
from django.contrib.auth.decorators从django.utils.decorators导入login_required
import method_decorator
class HomeView(ListView):
@method_decorator(login_required)
def dispatch(self,* args,** kwargs):
return super(HomeView,self).dispatch(* args,** kwargs)
i am trying to develop a website whith 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 some details about the code i write:
Trceback
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:\Users\andre\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\core\handlers\exception.py" in inner
34. response = get_response(request)
File "C:\Users\andre\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\core\handlers\base.py" in _get_response
126. response = self.process_exception_by_middleware(e, request)
File "C:\Users\andre\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\core\handlers\base.py" in _get_response
124. response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "C:\Users\andre\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\contrib\auth\decorators.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
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'
core/views.py
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'
core/urls.py
from django.urls import path
from . import views
urlpatterns = [
path('', views.HomeView, name='homepage'),
]
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'))
]
You need use as_view() at the end of class based views when declaring in the urls:
path('', views.HomeView.as_view(), name='homepage'),
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屋!