我是Django的新手,我正试图按照Udemy的教程创建我的第一个项目,但我遇到了这个错误。
我的项目有以下文件夹结构:
-演示
- __init__.py
- admin.py
- apps.py
- 型号.py
- 测试.py
- urls.py-视图.py
-第一个
- __init__.py
- asgi.py
- 设置.py
- urls.py
- wsgi.py
视图.py:
from django.shortcuts import render
from django.http import HttpRequest
def first(request):
return HttpRequest('1st message from views')
演示/urls.py:
from django.urls import path
from . import views
urlpatterns = [
path('', views.first),
]
第一个/urls.py:
from django.contrib import admin
from django.urls import path, include
urlpatterns = [
path('demo/', include('demo.urls')),
path('admin/', admin.site.urls),
]
两个__init__.py文件都为空
错误:
TypeError at /demo/
__init__() takes 1 positional argument but 2 were given
Request Method: GET
Request URL: http://127.0.0.1:8000/demo/
Django Version: 3.2.16
Exception Type: TypeError
Exception Value:
__init__() takes 1 positional argument but 2 were given
Exception Location: D:PythonDJANGOfirst-projectdemoviews.py, line 6, in first
Python Executable: D:PythonDJANGOfirst-projectvenvScriptspython.exe
Python Version: 3.7.6
Python Path:
['D:\Python\DJANGO\first-project',
'D:\Python\DJANGO\first-project',
'C:\Users\biavu\AppData\Local\Programs\Python\Python37\python37.zip',
'C:\Users\biavu\AppData\Local\Programs\Python\Python37\DLLs',
'C:\Users\biavu\AppData\Local\Programs\Python\Python37\lib',
'C:\Users\biavu\AppData\Local\Programs\Python\Python37',
'D:\Python\DJANGO\first-project\venv',
'D:\Python\DJANGO\first-project\venv\lib\site-packages']
Server time: Thu, 06 Oct 2022 12:34:45 +0000
您需要返回一个HttpResponse
,而不是HttpRequest
。
请求是您作为输入获得的内容。
from django.http import HttpResponse
def first(request):
return HttpResponse('1st message from views')