Django 2.1.3 错误:__init__() 采用 1 个位置参数,但给出了 2 个
Posted
技术标签:
【中文标题】Django 2.1.3 错误:__init__() 采用 1 个位置参数,但给出了 2 个【英文标题】:Django 2.1.3 Error: __init__() takes 1 positional argument but 2 were given 【发布时间】:2019-05-03 11:28:08 【问题描述】:我正在尝试使用 Django 2.1.3 和 python 3.7.1 开发一个网站 当我转到主页时,我收到此错误:
TypeError at / __init__() takes 1 positional argument but 2 were given
以下是我编写的代码的一些细节:
追溯
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
核心/models.py
这只是 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
这里我只想在用户通过身份验证的情况下在主页上看到数据库“Eventi”,我认为错误在这里,但我不知道在哪里:
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(项目)
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'))
]
【问题讨论】:
【参考方案1】:在 url 中声明时,您需要在基于类的视图末尾使用 as_view():
path('', views.HomeView.as_view(), name='homepage'),
另外,在使用login_required
装饰器时,需要在CBV的dispatch方法上使用:
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)
【讨论】:
我修改了这样的视图: class HomeView(ListView): @login_required def dispatch(self, Evento, pk): return super(HomeView, self).dispatch(Evento, pk=pk) 但现在错误是:“HomeView”对象没有属性“用户” 我一直忘记并回到这个答案。【参考方案2】:在 URL 中声明时,您需要在基于类的视图末尾使用 as_view()
。像这样:
path('', views.HomeView.as_view(), name='homepage'),
【讨论】:
【参考方案3】:在 `urls.py 中用这个路径代码替换:
path(' ', views.HomeView.as_view(), name='homepage'),
from django.urls import path
from . import views
urlpatterns = [
path(' ', views.HomeView.as_view(), name='homepage'),
]
【讨论】:
以上是关于Django 2.1.3 错误:__init__() 采用 1 个位置参数,但给出了 2 个的主要内容,如果未能解决你的问题,请参考以下文章
Django--bug--__init__() got an unexpected keyword argument 'qnique'
简单的留言簿 django:__init__() 接受 1 个位置参数,但给出了 2 个
Django __init__() 得到了一个意外的关键字参数“用户”