我事先用 Ruby 編程,現在正在轉向 Django。我正在嘗試按照這里的文章。https://docs.djangoproject.com/en/4.0/intro/tutorial01/
#polls/url.py
from django.urls import path
from . import views
urlpatterns = [
path('', views.index, name='index'),
]
# mysite /urls.py
"""mysite URL Configuration
The `urlpatterns` list routes URLs to views. For more information please see:
https://docs.djangoproject.com/en/4.0/topics/http/urls/
Examples:
Function views
1. Add an import: from my_app import views
2. Add a URL to urlpatterns: path('', views.home, name='home')
Class-based views
1. Add an import: from other_app.views import Home
2. Add a URL to urlpatterns: path('', Home.as_view(), name='home')
Including another URLconf
1. Import the include() function: from django.urls import include, path
2. Add a URL to urlpatterns: path('blog/', include('blog.urls'))
"""
from django.contrib import admin
from django.urls import include, path
urlpatterns = [
path('polls/', include('polls.urls')),
path('admin/', admin.site.urls),
]
-
這是我的樹: polls/ --> init .py -> admin.py -> apps.py -> migrations/ --> init .py -> models.py -> tests.py -> urls.py ->視圖.py->
當然,我django-admin startproject mysite
在所有 tihs 之前運行,版本的輸出是:
└──? $python -m django --version
4.0.4
我嘗試啟動服務器:
─? $python manage.py runserver
Watching for file changes with StatReloader
Performing system checks...
System check identified no issues (0 silenced).
May 24, 2022 - 13:47:13
Django version 4.0.4, using settings 'mysite.settings'
Starting development server at http://127.0.0.1:8000/
Quit the server with CONTROL-C.
...但是當我單擊鏈接時,我收到此錯誤:
Page not found (404)
Request Method: GET
Request URL: http://127.0.0.1:8000/
Using the URLconf defined in mysite.urls, Django tried these URL patterns, in this order:
polls/
admin/
The empty path didn’t match any of these.
You’re seeing this error because you have DEBUG = True in your Django settings file. Change that to False, and Django will display a standard 404 page.
其中是常見的 404 錯誤。為什么會這樣?我按照指示運行了所有內容。也許sqlite服務器有問題?我很迷茫,任何幫助將不勝感激,
uj5u.com熱心網友回復:
您沒有任何根路由。所以添加
urlpatterns = [
path('', **something**),
path('polls/', include('polls.urls')),
path('admin/', admin.site.urls),
]
應該幫助或嘗試打開http://127.0.0.1:8000/polls/或http://127.0.0.1:8000/admin/
uj5u.com熱心網友回復:
您polls/url.py
基于路徑 'polls/'(定義于mysite/urls.py
)
實際上你只定義了兩條路徑:
如果你添加
polls/url.py
path('index', views.index, name='index')
您還將polls/index
定義路徑
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/485342.html
標籤:Python django linux sqlite 模型视图控制器