文章目录
1 Web框架
2 工程目录结构
只要关注settings.py和urls.py即可!
3 调试运行Web框架
这时,不对工程进行任何路由修改。
C:\Users\88304>cd mysite
C:\Users\88304\mysite>python manage.py runserver
Performing system checks...
System check identified no issues (0 silenced).
You have 14 unapplied migration(s). Your project may not work properly until you apply the migrations for app(s): admin, auth, contenttypes, sessions.
Run 'python manage.py migrate' to apply them.
March 05, 2020 - 20:39:10
Django version 2.0, using settings 'mysite.settings'
Starting development server at http://127.0.0.1:8000/
Quit the server with CTRL-BREAK.
[05/Mar/2020 20:39:24] "GET / HTTP/1.1" 200 16559
[05/Mar/2020 20:39:24] "GET /static/admin/css/fonts.css HTTP/1.1" 200 423
Not Found: /favicon.ico
[05/Mar/2020 20:39:24] "GET /static/admin/fonts/Roboto-Bold-webfont.woff HTTP/1.1" 200 82564
[05/Mar/2020 20:39:24] "GET /favicon.ico HTTP/1.1" 404 1972
[05/Mar/2020 20:39:24] "GET /static/admin/fonts/Roboto-Regular-webfont.woff HTTP/1.1" 200 80304
[05/Mar/2020 20:39:24] "GET /static/admin/fonts/Roboto-Light-webfont.woff HTTP/1.1" 200 81348
3.1 Django admin
3.2 manage.py
同为管理工具。
4 创建一个具体应用
C:\Users\88304\mysite>manage.py startapp helloapp
4.1 修改应用的view.py
from django.shortcuts import render
# Create your views here.
from django.http import HttpResponse
def hello(request):
return HttpResponse("hello")
4.2 修改URL路由
5 效果
6 MTV开发模式
6.1 在hello2app,配置本地路由文件
6.2 增加对本地路由的引用
"""mysite URL Configuration
The `urlpatterns` list routes URLs to views. For more information please see:
https://docs.djangoproject.com/en/2.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 path,include
from helloapp import views
urlpatterns = [
path('index2/',include('helloapp.urls')),
path('index/',views.hello),
path('admin/', admin.site.urls),
]