回想一下MTV模型,数据模型(这里先不讲),模板,视图函数
1.先创建路由
2.有了路由才有视图函数,写各种的逻辑
3.模板写前端的代码,并且通过视图函数拿到相应需要展示数据,他俩就是一个互相交互的过程
了解了大致的思路我们下面来看一下具体的操作
创建一个新路由
demo1/urls.py
from django.contrib import admin
from django.urls import path
from django.views import hello
urlpatterns = [
path('admin/', admin.site.urls),
path('hello/', hello),
]
创建视图函数
app01/views.py
from django.shortcuts import render
# Create your views here.
def hello(request):
name = "zhangsan"
return render(request,"hello.html",{"name":name})
创建模板
templates/hello.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>hello</title>
</head>
<body>
name
</body>
</html>
配置模板引擎
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [
BASE_DIR / "templates", # 路径拼接
],
'APP_DIRS': True,
'OPTIONS': {
'context_processors': [
'django.template.context_processors.debug',
'django.template.context_processors.request',
'django.contrib.auth.context_processors.auth',
'django.contrib.messages.context_processors.messages',
],
},
},
]
运行这个项目
python manage.py runserver 8090