Django笔记
提示:这里可以添加系列文章的所有文章的目录,目录需要自己手动添加
例如:第一章 Python 机器学习入门之pandas的使用
提示:写完文章后,目录可以自动生成,如何生成可参考右边的帮助文档
文章目录
创建django项目
提示:这里可以添加本文要记录的大概内容:
首先创建一个项目。
其次注意点为:这里是虚拟环境所以,一定要检测setting 中django包有无导入
启动环境在该文件夹下输入
python
python manage.py runserver
setting.py 是相关配置文件
urls.py 是路径文件
在pycharm创建具体的app,
python
python manage.py startapp book
二、urls.py
该py为设置浏览器路径文件
注意要补充from django.shortcuts import HttpResponse
- 简单操作,但是开发不会这么做
python
from django.shortcuts import HttpResponse
def index(request):
return HttpResponse("hello")
urlpatterns = [
path('admin/', admin.site.urls),
path('index/',index)
]
运行后在网页尾缀后面输入/index
- 正常操作
在views.py中输入内容,在urls.py中导入该views.py所在的包调用里面运行函数。
例如 在book文件中的views.py中通过查询字符串 (query string):https://www.baidu.com/wd=python\&a=\&b=2
注意要加入from django.shortcuts import render,HttpResponse
python
from django.shortcuts import render,HttpResponse
# 在URL中携带参数
#1. 通过查询字符串(query string):https://www.baidu.com/wd=python&a=&b=2
#2.在path中携带:http://127.0.0.1:8000/book/2
# Create your views here.
# 1 http://127.0.0.1:8000/book?id=1
def book_detail_query_string(request):
# request.GET={"id":3}
book_id=request.GET.get('id')
name_id=request.GET.get('name')
return HttpResponse(f"查询的图书id是:{book_id},图书名称是:{name_id}")
在urls.py 中导入所在views.py的包如from book import views,然后写入路径和所需参数的返回函数
python
from book import views
urlpatterns = [
path('admin/', admin.site.urls),
path('index/',index),
path('book/',views.book_detail_query_string)
]
最终形成的样式为:
#2 # http://127.0.0.1:8000/book/2/水浒传
python
urlpatterns = [
path('admin/', admin.site.urls),
# http://127.0.0.1:8000/index
path('index/',index),
# http://127.0.0.1:8000/book?id=1&name=''
path('book/',views.book_detail_query_string),
# http://127.0.0.1:8000/book/1
#在book_id前指定参数类型有两点好处:
#1、以后在浏览器中,如果bobk_id输入的是一个非整形、那么会出现404错误:/book/abc
#2、在视图函数中,得到的book_id就是一个整形,否则就是str类型
path('book/<int:book_id>/<str:name_id>/',views.book_detail_path),
]
3.分模块化
新建一个app movie
在movie中的view加入所需函数
python
from django.shortcuts import render,HttpResponse
# Create your views here.
def movie_list(request):
return HttpResponse("电影列表")
def movie_detail(request,movie_id):
return HttpResponse(f"查询的电影id是:{movie_id}")
随后在movie中的urls内加入路径
python
from django.urls import path
from . import views
#指定应用名称
app_name="movie"
urlpatterns = [
path('list/',views.movie_list,name="movie_list"),
path('detail/<int:movie_id>',views.movie_detail,name="movie_detail"),
]
最后在总的urls.py 中加入分段地址
python
from django.urls import path,include
urlpatterns = [
# 分段地址
path('movie/',include("movie.urls"))
]
整体运行结果为
二、templates
其实主要就三步
1.在所在app下的view.py中创建html路径
python
# Create your views here.
def index(request):
return render(request,"index.html")
2.在所在app下创建templates文件夹,修改整体的setting.py,加入一个所在app名字,最后再在改templates文件夹中创建html
3.在该app名下的urls.py中创建连接views.py的路径,导入view所在的函数和需要的地址。最后再在整体urls.py中做个读取入口
三、templates中几个基础操作
1.app下的模板
view界面
python
# Create your views here.
def baidu(request):
return render(request,"baidu.html")
html界面
html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>这是home下面的html</title>
</head>
<body>
<h1 style="background-color: pink">这是home下面的html</h1>
</body>
</html>
2.参数传递模板
view界面
python
def info(request):
username="AAAA"
book={"name":"水浒传","author":"施耐庵"}
books=[
{"name":"水浒传1", "author": "施耐庵1"},
{"name": "水浒传2", "author": "施耐庵2"},
{"name": "水浒传3", "author": "施耐庵3"}
]
class Person:
def __init__(self, realname):
self.realname=realname
context={
'username': username,
'book': book,
'books':books,
'person':Person("BBB"),
}
return render(request,"info.html",context=context)
html界面
html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>信息传递</title>
</head>
<body>
<p>{{ username }}</p>
<p>图书名称{{ book.name }}</p>
<p>第一本书图书名称: {{ books.0.name }}</p>
<p>姓名为:{{ person.realname }}</p>
{% for book in books %}
<p>{{ book.name }}</p>
{% endfor %}
</body>
</html>
3.if参数模板
view界面
python
def if_view(request):
age=20
context = {
'age':age,
}
return render(request,"if.html",context=context)
html界面
html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>if标签</title>
</head>
<body>
{% if age > 18 %}
<p>已经满18</p>
{% elif age < 18%}
<p>不满18</p>
{% else %}
<p>刚满18</p>
{% endif %}
</body>
</html>
4.for参数模板
view界面
python
def for_view(request):
books = [
{"name": "水浒传1", "author": "施耐庵1"},
{"name": "水浒传2", "author": "施耐庵2"},
{"name": "水浒传3", "author": "施耐庵3"}
]
Person = {
"name": "施耐庵",
"age": 18,
"height":180
}
context = {
'books':books,
'Person':Person,
}
return render(request,"for.html",context=context)
html界面
html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>for标签</title>
</head>
<body>
<table>
<thead>
<tr>
<td>序号</td>
<td>名称</td>
<td>作者</td>
</tr>
</thead>
<tbody>
{% for book in books reversed%}
<tr>
<td>{{ forloop.counter }}</td>
<td>{{ book.name }}</td>
<td>{{ book.author }}</td>
</tr>
{% empty %}
<tr>没有任何东西</tr>
{% endfor %}
</tbody>
</table>
<div>
{% for key,value in Person.items %}
<p>{{ key }}:{{ value }}</p>
{% endfor %}
</div>
</body>
</html>
4.with参数模板
view界面
python
def with_view(request):
books = [
{"name": "水浒传1", "author": "施耐庵1"},
{"name": "水浒传2", "author": "施耐庵2"},
{"name": "水浒传3", "author": "施耐庵3"}
]
context = {
"books":books
}
return render(request,"with.html",context=context)
html界面
html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>with标签</title>
</head>
<body>
{% with book1=books.1 %}
<P>{{ book1.name }}:{{ book1.author }}</P>
{% endwith %}
</body>
</html>
5.url参数模板
view界面
python
def url_view(request):
return render(request,"url.html")
def book_detail(request,book_id):
return HttpResponse(f"查询的图书id是:{book_id}")
html界面
html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>url标签</title>
</head>
<body>
<a href="{% url 'home:home_baidu'%}">百度</a>
<a href="{% url 'home:url_book_detail' book_id=1 %}">图书详情</a>
</body>
</html>
总体的url
python
from django.urls import path
from . import views
#指定应用名称
app_name="home"
urlpatterns = [
path("",views.index,name="home_index"),
path('baidu/',views.baidu,name="home_baidu"),
path('info/',views.info,name="home_info"),
path('if/',views.if_view,name="home_if"),
path('for/',views.for_view,name="home_for"),
path('with/',views.with_view,name="home_with"),
path('url/',views.url_view,name="url_with"),
path('book/<book_id>',views.book_detail,name="url_book_detail"),
]
四、templates中extend和include
子模板继承父模板用extend
父模板包含子模板用include
首先创建公用的父模版
其中{block}{endblock}为插槽
rmwz_base作为付模板
html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>{% block title %}{% endblock %}---hzc</title>
{% block head %}{% endblock %}
</head>
<body>
<header>
<ul>
<li><a href="/"> 首页 </a></li>
<li><a href="/course"> 创业课堂 </a></li>
</ul>
</header>
{% block body %}
我是来自副模板的
{% endblock %}
<footer>
版权信息XXXX
</footer>
</body>
</html>
rmwz_index作为子模板
html
{% extends 'rmwz_base.html' %}
{% block title %}
首页
{% endblock %}
{% block head %}
<style>
body{
background-color: pink;
}
</style>
{% endblock %}
{% block body %}
{{ block.super }}
{% include "hotarticle.html" %}
<div>
自己加入东西
</div>
{% endblock %}
hotarticle.html作为rmwz_index的子模板
html
<div>
<h2>热门文章</h2>
<ul>
{% for article in articles%}
<li>{{ article }}</li>
{% endfor %}
</ul>
</div>
其中view.py定义articles变量
python
def template_form(request):
articles = ['小米su7','ChatGPT 5 发布' ]
context = {
"articles":articles
}
return render(request, "rmwz_index.html",context=context)
urls.py 载入地址链接
python
path('template/form/',views.template_form,name="template_form"),
实现效果