第1步:新建项目包含如下文档
在项目命令行进入虚拟环境:cd venv/Scripts/activate
E:\py7\newweb>cd venv
E:\py7\newweb\venv>cd scripts
E:\py7\newweb\venv\Scripts>activate
(venv) E:\py7\newweb\venv\Scripts>cd ../../
(venv) E:\py7\newweb>django-admin startproject myweb
(venv) E:\py7\newweb>cd myweb
(venv) E:\py7\newweb\myweb>django-admin startapp myapp
(venv) E:\py7\newweb\myweb>python.exe manage.py runserver #检测是否可以跑通
manage.py代码
import os
import sys
if __name__ == "__main__":
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "myweb.settings")
try:
from django.core.management import execute_from_command_line
except ImportError as exc:
raise ImportError(
"Couldn't import Django. Are you sure it's installed and "
"available on your PYTHONPATH environment variable? Did you "
"forget to activate a virtual environment?"
) from exc
execute_from_command_line(sys.argv)
在myweb列表里面添加mypp
在myapp里面新建templates python文件夹,并在里面新建HTML文件我再此命名为index
并输入标题helloworld
在views文档里面导入相关django模块并实现功能
from django.shortcuts import render, HttpResponse
# Create your views here.
def index(request):
dicts = {
"变量1":"值1",
"变量2":"值2",
}
return render(request,'index.html', dicts)
添加路由
from django.contrib import admin
from django.urls import path
from myapp.views import index
urlpatterns = [
path('admin/', admin.site.urls),
path('', index)
]
保存代码
实现效果