给路由分子路由,并设置命名空间

This commit is contained in:
mxjdi 2025-04-26 23:36:03 +08:00
parent 3f1a01d942
commit 1500fb5dd5
18 changed files with 103 additions and 4 deletions

View File

@ -35,7 +35,9 @@ INSTALLED_APPS = [
'django.contrib.sessions', 'django.contrib.sessions',
'django.contrib.messages', 'django.contrib.messages',
'django.contrib.staticfiles', 'django.contrib.staticfiles',
"helloWorld.apps.HelloworldConfig" "helloWorld.apps.HelloworldConfig",
"user.apps.UserConfig",
"order.apps.OrderConfig"
] ]
MIDDLEWARE = [ MIDDLEWARE = [

View File

@ -16,7 +16,7 @@ Including another URLconf
""" """
from django.conf import settings from django.conf import settings
from django.contrib import admin from django.contrib import admin
from django.urls import path, re_path from django.urls import path, re_path, include
from django.views.generic import RedirectView from django.views.generic import RedirectView
from django.views.static import serve from django.views.static import serve
@ -46,8 +46,11 @@ urlpatterns = [
# 媒体路由 # 媒体路由
re_path('media/(?P<path>.*)', serve, {'document_root': settings.MEDIA_ROOT}, name='media'), re_path('media/(?P<path>.*)', serve, {'document_root': settings.MEDIA_ROOT}, name='media'),
# 路由重定向用RedirectVIew实现 # 路由重定向用RedirectVIew实现
# 访问http://localhost:8000/redirectTo直接跳转到http://localhost:8000/index/ # 访问http://localhost:8000/redirectTo直接跳转到http://localhost:8000/index/
path('redirectTo',RedirectView.as_view(url="index/")), path('redirectTo', RedirectView.as_view(url="index/")),
# 给路由分路由出来并命名
path('user/', include(('user.urls','user'), namespace='user')),
path('order/', include(('order.urls','order'), namespace='order')),
] ]

0
order/__init__.py Normal file
View File

3
order/admin.py Normal file
View File

@ -0,0 +1,3 @@
from django.contrib import admin
# Register your models here.

6
order/apps.py Normal file
View File

@ -0,0 +1,6 @@
from django.apps import AppConfig
class OrderConfig(AppConfig):
default_auto_field = 'django.db.models.BigAutoField'
name = 'order'

View File

3
order/models.py Normal file
View File

@ -0,0 +1,3 @@
from django.db import models
# Create your models here.

3
order/tests.py Normal file
View File

@ -0,0 +1,3 @@
from django.test import TestCase
# Create your tests here.

25
order/urls.py Normal file
View File

@ -0,0 +1,25 @@
"""
URL configuration for Django5 project.
The `urlpatterns` list routes URLs to views. For more information please see:
https://docs.djangoproject.com/en/5.2/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 path
import order.views
urlpatterns = [
path('admin/', admin.site.urls),
path('index/', order.views.index),
]

5
order/views.py Normal file
View File

@ -0,0 +1,5 @@
from django.http import HttpResponse
# Create your views here.
def index(request):
return HttpResponse("订单信息")

0
user/__init__.py Normal file
View File

3
user/admin.py Normal file
View File

@ -0,0 +1,3 @@
from django.contrib import admin
# Register your models here.

6
user/apps.py Normal file
View File

@ -0,0 +1,6 @@
from django.apps import AppConfig
class UserConfig(AppConfig):
default_auto_field = 'django.db.models.BigAutoField'
name = 'user'

View File

3
user/models.py Normal file
View File

@ -0,0 +1,3 @@
from django.db import models
# Create your models here.

3
user/tests.py Normal file
View File

@ -0,0 +1,3 @@
from django.test import TestCase
# Create your tests here.

26
user/urls.py Normal file
View File

@ -0,0 +1,26 @@
"""
URL configuration for Django5 project.
The `urlpatterns` list routes URLs to views. For more information please see:
https://docs.djangoproject.com/en/5.2/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 path
import user.views
urlpatterns = [
path('admin/', admin.site.urls),
path('index/', user.views.index),
path('list/', user.views.list),
]

8
user/views.py Normal file
View File

@ -0,0 +1,8 @@
from django.http import HttpResponse
# Create your views here.
def index(request):
return HttpResponse("用户信息")
def list(request):
return HttpResponse("用户列表")