一. 安装django
pip install django
二. 在pycharm创建项目
1. 打开python依次选择:文件 新建项目 Django 在框内填写安装地址
2. 删除自带的templates文件夹,然后新建新建一个
3. 在settings.py中删除os.path.join(BASE_DIR,'templates
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
# os.path.join(BASE_DIR, 'templates') 这是【】里面的暂时不要把它拿出来删除,后期要用再放进去
'DIRS': []
,
'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',
],
},
},
]
三.创建app项目
在pycharm的终端输入:python manage.py startapp app01
四.注册app
在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', # 把app01项目写入
]
五. 编写URL和视图函数的对应关系(我这里写了5个对应关系由浅入深)
1.找到urls.py文件导入form app01 import views
"""
URL configuration for myslte project.
The `urlpatterns` list routes URLs to views. For more information please see:
https://docs.djangoproject.com/en/4.2/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
from app01 import views # 导入app01
urlpatterns = [
# path('admin/', admin.site.urls), # 注释掉系统自带的
path('index/', views.index), # 网址与视图函数的对应关系
path('user/list', views.user_list), # 路径(网址,views文件里找到 user_list函数)
path('user/add/', views.user_add),
path('tpl/', views.tpl), # 路径(网址,views文件里找到 user_list函数)
path('news/', views.news),
]
六. 编写视图函数
找到views.py文件编写函数(写了5个函数,包括渲染的模板语法在内)
# from django.shortcuts import render
from django.shortcuts import render, HttpResponse, redirect
# Create your views here.
def index(request): # 1.直接把字符串返回到网页
return HttpResponse("欢迎使用")
def user_list(request): # 2.把HTML文件返回到网页
return render(request,"user_list.html")
def user_add(request):
return render(request, 'user_add.html') # 返回HTML文件
def tpl(request): # 3.模板语法把数据渲染到页面再返回到网页
name = "韩超发方法" # 1. 字符串数据类型
roles = ["管理员", "CEO", "保安"] # 2. 列表数据类型
user_info = {"name": "郭智", "salary": 100000, 'role': "CTO"} # 3. 字典数据类型
data_list = [
{"name": "郭智", "salary": 100000, 'role': "CTO"}, # 4. 列表套字典数据类型
{"name": "卢慧", "salary": 100000, 'role': "CTO"},
{"name": "赵建先", "salary": 100000, 'role': "CTO"},
]
# 返回 提供(请求,页面{ni属性=name,......}
return render(request, 'tpl.html', {"n1": name, "n2": roles, 'n3': user_info, "n4": data_list})
def news(req): # 4.新闻案例:把数据库或网络上的数据渲染到页面再返回到网页
# 1.定义一些新闻(字典或者列表) 或 去数据库 或 去网上爬数据
# 第三方模块:requests (安装:pip install requests)
import requests # 注意这个第三方模块和 request 不一样
# 2.向地址:https://weibo.com/ajax/statuses/news 发送请求
res = requests.get("https://weibo.com/ajax/statuses/news") # 爬取网页的数据
data_list = res.json() # 把爬取网页的数据转化成json的格式
print(data_list)
# 3.把数据渲染到页面再返回到网页
return render(req, 'news.html', {"news_list": data_list})
七. 编写HTML文件(我只写了:3.模板语法 4.新闻案例 5.静态文件的引入)
3. 模板语法
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<h1>模板语法的学习</h1>
<!-- 1.字符串 -->
<div>{{ n1 }}</div> <!-- 把后台的字符串渲染到页面 -->
<!-- 2.列表 -->
<div>{{ n2 }}</div> <!-- 把后台的列表渲染到页面 -->
<div>{{ n2.0 }}</div> <!-- 把后台的列表的第0个值渲染到页面 -->
<div>{{ n2.1 }}</div> <!-- 把后台的列表的第1个值渲染到页面 -->
<div>{{ n2.2 }}</div> <!-- 把后台的列表的第2个值渲染到页面 -->
<div> <!-- 用循环把后台的列表的值渲染到页面 -->
{% for item in n2 %} <!-- 遍历列表 -->
<span>{{ item }}</span> <!-- 取出每个值 -->
{% endfor %} <!-- 循环结束 -->
</div>
<!-- 3.字典 -->
<hr/>
{{ n3 }} <!-- 把后台的字典渲染到页面 -->
{{ n3.name }} <!-- 把后台的字典里name的值渲染到页面 -->
{{ n3.salary }} <!-- 把后台的字典里salary的值渲染到页面 -->
{{ n3.role }} <!-- 把后台的字典里role的值渲染到页面 -->
<ul> <!-- 列表标签 -->
{% for k,v in n3.items %} <!-- 遍历字典 -->
<li>{{ k }} = {{ v }} </li> <!-- 取出键,值 放入li标签-->
{% endfor %} <!-- 循环结束 -->
</ul>
<hr/>
<!-- 4.列表套字典 -->
{{ n4 }} <!-- 把后台的列表渲染到页面 -->
{{ n4.1 }} <!-- 把后台的列表的第1个字典渲染到页面 -->
{{ n4.1.name }} <!-- 把后台的列表的第1个字典name的值渲染到页面 -->
{{ n4.1.role }} <!-- 把后台的列表的第1个字典role的值渲染到页面 -->
{% for item in n4 %} <!-- 用循环把后台的列表的值渲染到页面 -->
<div>{{ item.name }} {{ item.salary }}</div> <!-- 把字典name的值 salary的值 -->
{% endfor %} <!-- 循环结束 -->
<hr/>
<!-- if语句 -->
{% if n1 == "韩超" %} <!-- 如果n1==韩超 -->
<h1>哒哒哒哒哒</h1> <!-- 就把哒哒哒哒哒 写入标签 -->
{% else %} <!-- 反之 -->
<h1>嘟嘟嘟嘟</h1> <!-- 就把嘟嘟嘟嘟 写入标签 -->
{% endif %} <!-- if语句结束 -->
{% if n1 == "韩超" %} <!-- 如果n1==韩超 -->
<h1>哒哒哒哒哒</h1> <!-- 就把哒哒哒哒哒 写入标签 -->
<h1>哒哒哒哒哒</h1>
{% elif n1 == "xxx" %} <!-- 如果n1==xxx -->
<h1>哔哔哔</h1> <!-- 就把哔哔哔 写入标签 -->
{% else %} <!-- 反之 -->
<h1>嘟嘟嘟嘟</h1> <!-- 就把嘟嘟嘟嘟 写入标签 -->
{% endif %} <!-- if语句结束 -->
</body>
</html>
4. 新闻案例
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<h1>联通新闻中心</h1>
<ul>
{% for item in news_list %} <!-- 遍历列表套字典 -->
<li>{{ item.news_title }} 时间:{{ item.post_time }}</li> <!-- 取标题项 取时间项-->
{% endfor %} <!-- 循环结束 -->
</ul>
</body>
</html>
5. 静态文件的引入
{% load static %} <!-- django静态文件引用方式 -->
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<link rel="stylesheet" href="{% static 'plugins/bootstrap-3.4.1/css/bootstrap.css' %}"> <!-- 引入bootstrap -->
</head>
<body>
<h1>用户列表</h1>
<input type="text" class="btn btn-primary" value="新建"/>
<img src="{% static 'img/1.png' %}" alt=""> <!-- 引入图片 -->
<script src="{% static 'js/jquery-3.6.0.min.js' %}"></script> <!-- 引入jquery模块 -->
<script src="{% static 'plugins/bootstrap-3.4.1/js/bootstrap.js' %}"></script> <!--引入bootstrap中js样式-->
</body>
</html>
七. 启动django项目
1. 终端启动【python manage.py runserver】
2. Pycharm中启动【直接点运行的小三角】