我正在尝试开发带有Django 2.1.3和python 3.7.1的网站,当我转到主页时出现此错误:
以下是有关我编写的代码的一些详细信息:
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
这只是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'
在这里,我只想通过用户身份验证即可在主页上看到数据库“ 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'
from django.urls import path from . import views urlpatterns = [ path('', views.HomeView, name='homepage'), ]
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():
path('', views.HomeView.as_view(), name='homepage'),
另外,使用login_required装饰器时,需要在CBV的分派方法上使用它:
login_required
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)