1.安装命令:pip install -U Django(-U:安装最新的版本)
2.验证命令:python -m django --version
或者:
import django
django.get_version()
输出:3.0
3.创建Django命令:django-admin startproject mysite
4.切换至 mysite 文件夹
运行服务命令:python3 manage.py runserver
在浏览器输入:http://127.0.0.1:8000/
4.创建APP命令:python manage.py startapp polls
<1>打开 polls/views.py
from django.http import HttpResponse
def index(request):
return HttpResponse("Hello, world. You're at the polls index.")
<2>打开 polls/urls.py(新建PY文件)
from django.urls import path
from . import views(从当前模块引入views)
urlpatterns = [
path('', views.index, name='index'),
]
<3>在 mysite/urls.py 文件的 urlpatterns 列表里插入一个 include()
from django.contrib import admin
from django.urls import include, path
urlpatterns = [
path('polls/', include('polls.urls')),
path('admin/', admin.site.urls),
]
网友评论