本篇博客记录了博主平时Django的一些学习笔记:
1.在Url.py中使用HttpResponse
urls.py
from django.http import HttpResponse
from django.urls import path, include
def index1(request):
return HttpResponse("这才是首页")
def book(request):
return HttpResponse("书籍首页")
urlpatterns = [
path("",index1),
path("book/",book),
]
开启debug模式可以在访问错误页面的时候发现运行中的错误,
当Debug设置为False时,必须设置能够访问本网站的主机地址
同时,在本页面出现错误时会返回Server Error (500)
在生产环境中一定要关闭Debug模式,否则可能造成信息泄露
2.project与app分开写视图函数
projetc/url.py
from book.views import book
urlpatterns = [
path("book/",book),
]
book/views.py
from django.http import HttpResponse
def book(request):
return HttpResponse("这是book首页")
3.URL传递参数给视图函数
一个参数views.py
def booklist(request,book_id):
#可以根据数据库中的数据提取book的信息
text = "您获取的图书的id是:%s" % book_id
return HttpResponse(text)
一个参数urls.py
from book import booklist
urlpatterns = [
path("book/<book_id>",booklist),
]
两个参数views.py
def booklist(request,book_id,book_name):
#可以根据数据库中的数据提取book的信息
text = "您获取的图书的id是:%s ,您的图书的名字是:%s" % (book_id,book_name)
return HttpResponse(text)
两个参数urls.py
from book import booklist
urlpatterns = [
path("book/<book_id>/<book_name>",booklist),
]
4.查询
views.py
def author(request):
author_id=request.Get.get("id")
text = "作者的id为%s " % author_id
return HttpResponse(text)
urls.py
path("author/",author)
查询时
http://192.168.168.141:8000/author/?id=2
因为查询字符串使用的是GET请求,所以我们使用request.GET来获取参数,且因为GET是一个类似于字典的数类型,所以获取值跟字典的方式都是一样的
5.uuid的使用
uuid根据实际情况也可以换成int,或者str
from book import booklist
urlpatterns = [
path("book/<uuid:book_id>",booklist),
]
使用uuid,只能访问uuid类型的字符串,其他的都是错误的
在Python环境中导入一个uuid
import uuid
print(uuid.uuid4())
from django.urls import converters
str不能加/,但是改为path就可以显示斜杠,具体使用看converters.py
6.url参数的转化器
str: 除了/以外的所有字符
int:阿拉伯数字
path:所有字符
uuid:uuid.uuid4()函数返回的格式
slug:英文字母、数字、横杠、下划线
7.url的模块化管理
project/urls.py
from django.urls import path, include
urlpatterns = [
path("book/>",include("book.urls")),
]
book/urls.py
from django.urls import path
from .views import booklist
urlpatterns = [
path("<book_id>/",booklist),
]
book/views.py
from django.http import HttpResponse
def booklist(request,book_id):
text = "图书的id为%s " % book_id
return HttpResponse(text)
8.url命名与反转
在app的urls.py中添加app_name(应用命名空间),防止每个app的url重复,造成url找不到的情况
front/views.py
from django.shortcuts import render,redirect,reverse
from django.http import HttpResponse
def index(request):
username=request.GET.get("username")
if username:
return HttpResponse("这是前台首页")
else:
return redirect(reverse("front:login"))
def login(request):
return HttpResponse("这是前台的登录页面")
front/urls.py
from django.urls import path
from .views import index,login
app_name="front"
urlpatterns=[
path("",index),
path("signin/",login,name="login"),
]
9.应用实例命名空间
不能只制定实例命名空间,没有应用命名空间
project/urls.py
path("cms1/",include("cms.urls",namespace="cms1")),
path("cms2/",include("cms.urls",namespace="cms2")),
app/urls.py
from django.shortcuts import render,reverse,redirect
from django.http import HttpResponse
def index(request):
username=request.GET.get("username")
if username:
return HttpResponse("这是后台首页")
else:
current_namespace=request.resolver_match.namespace
return redirect(reverse("%s:login" %current_namespace))
def login(request):
return HttpResponse("这是后台的登录页面")
10.re_path
urls.py
#P为参数的名字,<year>为类型,\d表示为日期型,{4}表示有4个数字
#w:0-9,a-z,A-Z
re_path(r"^list/(?P<year>\d{4}/)",article_list),
re_path(r"^list/(?P<year>\d{4})/(?P<month>\d{4})",article_list_month),
views.py
def article_list(request,year):
text = "您输入的是第%s年的文章" % year
return HttpResponse(text)
此时list后面只能是四位数字
11.跳转到指定的页面
return redirect(reverse("<article>/",kwargs={"article_id":1}))
12.添加多个标签
urls.py
re_path(r"list/(?P<catagries>\w+|(\w+\+\w+)+)/",article_list_lll),
views.py
def article_list_lll(request,catagries):
text = "您输入的是第%s的文章hhhh " % catagries
return HttpResponse(text)
13.自定义path
from django.urls import converters,register_converter
class CategoryConverter(object):
regex =r'\w+|(\w+\+\w+)+'
def to_python(self,value):
#python+django+flask
#['python','django','flask']
result=value.split("+")
return result
def to_url(self,value):
#['python','django','flask']
# python+django+flask
if isinstance(value,list):
result = "+".join(value)
return result
else:
raise RuntimeError("转换url的时候,分类参数必须为列表")
#应用命名空间
register_converter(CategoryConverter,'cate')
path("list/<cate:catagries>",article_list_lll),
views.py
def article_list_lll(request,catagries):
text = "您输入的是第%s的文章hhhh " % catagries
return HttpResponse(text)
如果converters.py是另外的,则需要在init文件中导入converters
init.py
from . import converters
14.自定义参数
urls.py
path("",movies),
path("page/<int:page>",movies),
views.py
book_list=[
"三国演义",
"红楼梦",
"西游记",
"水浒传",
]
# Create your views here.
# def movie(request,movie_id):
# text = "电影的id为:%s" % movie_id
# return HttpResponse(text)
def movies(request,page=0):
return HttpResponse(book_list[page])