wordpress连不上mysql8,企业商务网站优化,wordpress制作图片站,晋江外贸网站建设Django笔记 提示#xff1a;这里可以添加系列文章的所有文章的目录#xff0c;目录需要自己手动添加 例如#xff1a;第一章 Python 机器学习入门之pandas的使用 提示#xff1a;写完文章后#xff0c;目录可以自动生成#xff0c;如何生成可参考右边的帮助文档 文章目录…Django笔记 提示这里可以添加系列文章的所有文章的目录目录需要自己手动添加 例如第一章 Python 机器学习入门之pandas的使用 提示写完文章后目录可以自动生成如何生成可参考右边的帮助文档 文章目录 Django笔记创建django项目二、urls.py二、templates三、templates中几个基础操作四、templates中extend和include 创建django项目 
提示这里可以添加本文要记录的大概内容 
首先创建一个项目。  其次注意点为这里是虚拟环境所以一定要检测setting 中django包有无导入  启动环境在该文件夹下输入 
python manage.py runserversetting.py 是相关配置文件 urls.py 是路径文件 
在pycharm创建具体的app  
python manage.py startapp book二、urls.py 
该py为设置浏览器路径文件 注意要补充from django.shortcuts import HttpResponse 
简单操作但是开发不会这么做 
from django.shortcuts import HttpResponse
def index(request):return HttpResponse(hello)urlpatterns  [path(admin/, admin.site.urls),path(index/,index)
]运行后在网页尾缀后面输入/index  2. 正常操作 在views.py中输入内容在urls.py中导入该views.py所在的包调用里面运行函数。 例如在book文件中的views.py中通过查询字符串query string:https://www.baidu.com/wdpythonab2 注意要加入from django.shortcuts import render,HttpResponse 
from django.shortcuts import render,HttpResponse
# 在URL中携带参数
#1. 通过查询字符串query string:https://www.baidu.com/wdpythonab2
#2.在path中携带http://127.0.0.1:8000/book/2
# Create your views here.
# 1 http://127.0.0.1:8000/book?id1
def book_detail_query_string(request):# request.GET{id:3}book_idrequest.GET.get(id)name_idrequest.GET.get(name)return HttpResponse(f查询的图书id是{book_id}图书名称是{name_id}) 在urls.py 中导入所在views.py的包如from book import views然后写入路径和所需参数的返回函数 
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/水浒传  
urlpatterns  [path(admin/, admin.site.urls),# http://127.0.0.1:8000/indexpath(index/,index),# http://127.0.0.1:8000/book?id1namepath(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加入所需函数 
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内加入路径 
from django.urls import path
from . import views
#指定应用名称
app_namemovie
urlpatterns  [path(list/,views.movie_list,namemovie_list),path(detail/int:movie_id,views.movie_detail,namemovie_detail),
]最后在总的urls.py 中加入分段地址 
from django.urls import path,include
urlpatterns  [# 分段地址path(movie/,include(movie.urls))
] 整体运行结果为  
二、templates 
其实主要就三步 
1.在所在app下的view.py中创建html路径 
# 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界面 
# Create your views here.
def baidu(request):return render(request,baidu.html)html界面 
!DOCTYPE html
html langen
headmeta charsetUTF-8title这是home下面的html/title
/head
bodyh1 stylebackground-color: pink这是home下面的html/h1
/body
/html2.参数传递模板 view界面 
def info(request):usernameAAAAbook{name:水浒传,author:施耐庵}books[{name:水浒传1, author: 施耐庵1},{name: 水浒传2, author: 施耐庵2},{name: 水浒传3, author: 施耐庵3}]class Person:def __init__(self, realname):self.realnamerealnamecontext{username: username,book: book,books:books,person:Person(BBB),}return render(request,info.html,contextcontext)html界面 
!DOCTYPE html
html langen
headmeta charsetUTF-8title信息传递/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
/html3.if参数模板 view界面 
def if_view(request):age20context  {age:age,}return render(request,if.html,contextcontext)html界面 
!DOCTYPE html
html langen
headmeta charsetUTF-8titleif标签/title
/head
body
{% if age  18 %}p已经满18/p
{% elif age  18%}p不满18/p
{% else %}p刚满18/p
{% endif %}
/body
/html4.for参数模板 view界面 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,contextcontext) 
html界面 
!DOCTYPE html
html langen
headmeta charsetUTF-8titlefor标签/title
/head
body
table
theadtrtd序号/tdtd名称/tdtd作者/td/tr
/theadtbody{% for book in books reversed%}trtd{{ forloop.counter }}/tdtd{{ book.name }}/tdtd{{ book.author }}/td/tr{% empty %}tr没有任何东西/tr{% endfor %}/tbody
/tablediv{% for key,value in Person.items %}p{{ key }}:{{ value }}/p
{% endfor %}
/div/body
/html4.with参数模板 view界面 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,contextcontext)html界面 
!DOCTYPE html
html langen
headmeta charsetUTF-8titlewith标签/title
/head
body
{% with book1books.1 %}P{{ book1.name }}:{{ book1.author }}/P
{% endwith %}
/body
/html5.url参数模板 view界面 def url_view(request):return render(request,url.html)def book_detail(request,book_id):return HttpResponse(f查询的图书id是{book_id})html界面 
!DOCTYPE html
html langen
headmeta charsetUTF-8titleurl标签/title
/head
body
a href{% url home:home_baidu%}百度/a
a href{% url home:url_book_detail book_id1 %}图书详情/a
/body
/html总体的url 
from django.urls import path
from . import views
#指定应用名称
app_namehome
urlpatterns  [path(,views.index,namehome_index),path(baidu/,views.baidu,namehome_baidu),path(info/,views.info,namehome_info),path(if/,views.if_view,namehome_if),path(for/,views.for_view,namehome_for),path(with/,views.with_view,namehome_with),path(url/,views.url_view,nameurl_with),path(book/book_id,views.book_detail,nameurl_book_detail),
]四、templates中extend和include 
子模板继承父模板用extend 父模板包含子模板用include 
首先创建公用的父模版 其中{block}{endblock}为插槽 rmwz_base作为付模板 
!DOCTYPE html
html langen
headmeta charsetUTF-8title{% block title %}{% endblock %}---hzc/title{% block head %}{% endblock %}
/head
body
headerullia href/ 首页 /a/lilia href/course 创业课堂 /a/li/ul
/header
{% block body %}我是来自副模板的
{% endblock %}
footer版权信息XXXX
/footer
/body
/htmlrmwz_index作为子模板 
{% extends rmwz_base.html %}
{% block title %}首页
{% endblock %}
{% block head %}stylebody{background-color: pink;}/style
{% endblock %}
{% block body %}{{ block.super }}{% include hotarticle.html %}div自己加入东西/div
{% endblock %}hotarticle.html作为rmwz_index的子模板 
div
h2热门文章/h2ul{% for article in articles%}li{{ article }}/li{% endfor %}/ul
/div其中view.py定义articles变量 
def template_form(request):articles  [小米su7,ChatGPT 5 发布 ]context  {articles:articles}return render(request, rmwz_index.html,contextcontext)urls.py 载入地址链接 path(template/form/,views.template_form,nametemplate_form),实现效果  
 文章转载自: http://www.morning.tstkr.cn.gov.cn.tstkr.cn http://www.morning.ldzss.cn.gov.cn.ldzss.cn http://www.morning.nqnqz.cn.gov.cn.nqnqz.cn http://www.morning.kkwgg.cn.gov.cn.kkwgg.cn http://www.morning.znknj.cn.gov.cn.znknj.cn http://www.morning.wbxtx.cn.gov.cn.wbxtx.cn http://www.morning.mnclk.cn.gov.cn.mnclk.cn http://www.morning.xnnpy.cn.gov.cn.xnnpy.cn http://www.morning.xqjh.cn.gov.cn.xqjh.cn http://www.morning.tmxtr.cn.gov.cn.tmxtr.cn http://www.morning.wschl.cn.gov.cn.wschl.cn http://www.morning.hblkq.cn.gov.cn.hblkq.cn http://www.morning.cpqwb.cn.gov.cn.cpqwb.cn http://www.morning.dpdr.cn.gov.cn.dpdr.cn http://www.morning.gsjzs.cn.gov.cn.gsjzs.cn http://www.morning.tpnxr.cn.gov.cn.tpnxr.cn http://www.morning.lrybz.cn.gov.cn.lrybz.cn http://www.morning.bnzjx.cn.gov.cn.bnzjx.cn http://www.morning.cbczs.cn.gov.cn.cbczs.cn http://www.morning.fwlch.cn.gov.cn.fwlch.cn http://www.morning.knryp.cn.gov.cn.knryp.cn http://www.morning.mrpqg.cn.gov.cn.mrpqg.cn http://www.morning.rfqkx.cn.gov.cn.rfqkx.cn http://www.morning.fpkpz.cn.gov.cn.fpkpz.cn http://www.morning.mnwsy.cn.gov.cn.mnwsy.cn http://www.morning.zhengdaotang.cn.gov.cn.zhengdaotang.cn http://www.morning.cjrmf.cn.gov.cn.cjrmf.cn http://www.morning.qfdmh.cn.gov.cn.qfdmh.cn http://www.morning.fwkjp.cn.gov.cn.fwkjp.cn http://www.morning.clybn.cn.gov.cn.clybn.cn http://www.morning.ndzhl.cn.gov.cn.ndzhl.cn http://www.morning.ldzss.cn.gov.cn.ldzss.cn http://www.morning.tkfnp.cn.gov.cn.tkfnp.cn http://www.morning.gqfbh.cn.gov.cn.gqfbh.cn http://www.morning.qcbhb.cn.gov.cn.qcbhb.cn http://www.morning.ljbm.cn.gov.cn.ljbm.cn http://www.morning.mttck.cn.gov.cn.mttck.cn http://www.morning.psxwc.cn.gov.cn.psxwc.cn http://www.morning.lnyds.cn.gov.cn.lnyds.cn http://www.morning.kkgbs.cn.gov.cn.kkgbs.cn http://www.morning.jxhlx.cn.gov.cn.jxhlx.cn http://www.morning.hbpjb.cn.gov.cn.hbpjb.cn http://www.morning.jpzcq.cn.gov.cn.jpzcq.cn http://www.morning.zbmcz.cn.gov.cn.zbmcz.cn http://www.morning.cgtfl.cn.gov.cn.cgtfl.cn http://www.morning.ctlzf.cn.gov.cn.ctlzf.cn http://www.morning.hmxb.cn.gov.cn.hmxb.cn http://www.morning.kgsws.cn.gov.cn.kgsws.cn http://www.morning.sftrt.cn.gov.cn.sftrt.cn http://www.morning.fkgct.cn.gov.cn.fkgct.cn http://www.morning.dpmkn.cn.gov.cn.dpmkn.cn http://www.morning.srrrz.cn.gov.cn.srrrz.cn http://www.morning.wngpq.cn.gov.cn.wngpq.cn http://www.morning.sxygc.cn.gov.cn.sxygc.cn http://www.morning.jgcxh.cn.gov.cn.jgcxh.cn http://www.morning.snbry.cn.gov.cn.snbry.cn http://www.morning.jxscp.cn.gov.cn.jxscp.cn http://www.morning.cfqyx.cn.gov.cn.cfqyx.cn http://www.morning.spdyl.cn.gov.cn.spdyl.cn http://www.morning.dnconr.cn.gov.cn.dnconr.cn http://www.morning.lssfd.cn.gov.cn.lssfd.cn http://www.morning.rlksq.cn.gov.cn.rlksq.cn http://www.morning.bwjgb.cn.gov.cn.bwjgb.cn http://www.morning.pntzg.cn.gov.cn.pntzg.cn http://www.morning.kwdfn.cn.gov.cn.kwdfn.cn http://www.morning.rydbs.cn.gov.cn.rydbs.cn http://www.morning.qmsbr.cn.gov.cn.qmsbr.cn http://www.morning.bpmfg.cn.gov.cn.bpmfg.cn http://www.morning.rbhqz.cn.gov.cn.rbhqz.cn http://www.morning.msxhb.cn.gov.cn.msxhb.cn http://www.morning.kxmyj.cn.gov.cn.kxmyj.cn http://www.morning.pwmpn.cn.gov.cn.pwmpn.cn http://www.morning.gbcnz.cn.gov.cn.gbcnz.cn http://www.morning.langlaitech.cn.gov.cn.langlaitech.cn http://www.morning.xbyyd.cn.gov.cn.xbyyd.cn http://www.morning.wdxr.cn.gov.cn.wdxr.cn http://www.morning.qzfjl.cn.gov.cn.qzfjl.cn http://www.morning.hslgq.cn.gov.cn.hslgq.cn http://www.morning.trfrl.cn.gov.cn.trfrl.cn http://www.morning.tbhf.cn.gov.cn.tbhf.cn