网站建设在作用是什么原因,用爬虫做数据整合网站,网站选择理由描述,长沙待遇好的十大国企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),实现效果