Python Web开发:多级路由配置和访问2
来自CloudWiki
目录
人才招聘:contactApp
urls.py
contactApp/urls.py:
from django.urls import path from . import views app_name = 'contactApp' urlpatterns = [ path('contact/', views.contact, name='contact'), # 欢迎咨询 path('recruit/', views.recruit, name='recruit'), # 加入恒达 ]
views.py
contactApp/views.py:
from django.shortcuts import render from django.shortcuts import HttpResponse # Create your views here. def contact(request): html = '<html><body>欢迎咨询</body></html>' return HttpResponse(html) def recruit(request): html = '<html><body>加入恒达</body></html>' return HttpResponse(html)
访问
http://127.0.0.1:8000/contactApp/contact/
http://127.0.0.1:8000/contactApp/recruit/
新闻动态:newsApp
urls.py
from django.urls import path from . import views app_name = 'newsApp' urlpatterns = [ path('company/', views.company, name='company'), # 公司要闻 path('industry/', views.industry, name='industry'), # 行业新闻 path('notice/', views.notice, name='notice'), # 通知公告 ]
views.py
from django.shortcuts import render from django.shortcuts import HttpResponse # Create your views here. def company(request): html = '<html><body>公司要闻</body></html>' return HttpResponse(html) def industry(request): html = '<html><body>行业新闻</body></html>' return HttpResponse(html) def notice(request): html = '<html><body>通知公告</body></html>' return HttpResponse(html)
访问
http://127.0.0.1:8000/newsApp/company/
http://127.0.0.1:8000/newsApp/industry/
http://127.0.0.1:8000/newsApp/notice/
产品中心:productsApp
urls.py
from django.urls import path from . import views app_name = 'productsApp' urlpatterns = [ path('robot/', views.robot, name='robot'), # 家用机器人 path('monitoring/', views.monitoring, name='monitoring'), # 智能监控 path('face/', views.face, name='face'), # 人脸识别解决方案 ]
views.py
from django.shortcuts import render from django.shortcuts import HttpResponse # Create your views here. def robot(request): html = '<html><body>家用机器人</body></html>' return HttpResponse(html) def monitoring(request): html = '<html><body>智能监控</body></html>' return HttpResponse(html) def face(request): html = '<html><body>人脸识别解决方案</body></html>' return HttpResponse(html)
访问
http://127.0.0.1:8000/productsApp/robot/
http://127.0.0.1:8000/productsApp/monitoring/
http://127.0.0.1:8000/productsApp/face/
服务支持:serviceApp
urls.py
from django.urls import path from . import views app_name = 'serviceApp' urlpatterns = [ path('download/', views.download, name='download'), # 资料下载 path('platform/', views.platform, name='platform'), # 人脸识别开放平台 ]
views.py
from django.shortcuts import render from django.shortcuts import HttpResponse # Create your views here. def download(request): html = '<html><body>资料下载</body></html>' return HttpResponse(html) def platform(request): html = '<html><body>人脸识别开放平台</body></html>' return HttpResponse(html)
访问
http://127.0.0.1:8000/serviceApp/platform/
http://127.0.0.1:8000/serviceApp/download/
科研基地:scienceApp
urls.py
from django.urls import path from . import views app_name = 'scienceApp' urlpatterns = [ path('science/', views.science, name='science'), # 科研基地 ]
views.py
from django.shortcuts import render from django.shortcuts import HttpResponse # Create your views here. def science(request): html = '<html><body>科研基地</body></html>' return HttpResponse(html)