第一步:在 urls.py 文件里修改
from django.contrib import admin
from django.urls import path
from app01 import views # 添加这一行urlpatterns = [#path('admin/', admin.site.urls),path('index/', views.index), # 添加这一行
]
第二步:在 settings.py 文件里修改
INSTALLED_APPS = ['django.contrib.admin','django.contrib.auth','django.contrib.contenttypes','django.contrib.sessions','django.contrib.messages','django.contrib.staticfiles','app01.apps.App01Config', # 添加这一行
]
第三步:在 views.py 文件里创建一个函数
from django.shortcuts import render
from django.shortcuts import HttpResponse# Create your views here.def index(request):data = "welcome"userList = ["aa", "bb", "cc"]dictionary = {"name": "ere","age": 34,"email": "erer@dfd.com"}context = {"v1": data, "v2": userList,"v3": dictionary}# 传参数给 index.html 文件return render(request, 'index.html', context)
第四步:在 templates 文件夹里创建 index.html 文件
<!DOCTYPE html>
<html><head></head><body><h1>Index页面</h1><div>{{ v1 }}</div><h2>循环展示</h2><ul>{% for item in v2 %}<li>{{ item }}</li>{% endfor %}</ul><div>{{ v2.0 }}{{ v2.1 }}{{ v2.2 }}</div><h3>字典展示</h3><div>{{ v3.name }}{{ v3.age }}{{ v3.email }}</div><ul>{% for data in v3.keys %}<li>{{ data }}</li>{% endfor %}</ul><ul>{% for data in v3.values %}<li>{{ data }}</li>{% endfor %}</ul><ul>{% for k,v in v3.items %}<li>{{ k }}: {{ v }}</li>{% endfor %}</ul></body>
</html>
第五步:运行下面命令
python manage.py runserver
点个赞呗~