app.urls
from django.urls import path,re_path
from book.views import index,detail,cookie
urlpatterns = [
#name就是给url起一个名字
##通过name找到路由
path('book/',index,name='index'),
path('cookie/',cookie,name='cookie'),
#http://127.0.0.1:8000/分类id/书籍id/
#http://127.0.0.1:8000/category_id/book_id/
#分组来获取正则中的数据
#根据位置来获取 url中的参数
#关键字参数--推荐大家使用关键字参数
###需要导入 re_path
# re_path('(\d+)/(\d+)/',detail)
##关键字参数 views中
re_path('(?P<category_id>\d+)/(?P<book_id>\d+)/',detail)
# path('<int:category_id>/<int:book_id>/',detail)
]
views
from django.http import HttpResponse
from django.shortcuts import render
from django.urls import reverse
import json
# Create your views here.
def index(request):
######登录层高需要跳转到首页
##viewname通过视图名字
# return redirect('/index/')
path = reverse('book:index')
# print(path)
####注册成功后需要跳转到首页
# return redirect('/index/')
# return reverse(path)
return HttpResponse('index')
def detail(request,book_id,category_id):
# print(category_id,book_id)
###########################GET 查询字符串#################################
"""
https://www.baidu.com/s?ie=utf-8&wd=itcast&rsv_pq=fdf543ed000f8688&rsv_t=8862Eb1lxc9858Ihke7VdJylicTyYs%2F3EuFyVPKcOBnv9wmTxLdhwlYL6%2F8&rqlang=cn&rsv_enter=1&rsv_sug3=5&rsv_sug1=4&rsv_sug7=100
以? 作为一个分隔
?前边 表示 路由
?后边 表示 get方式传递的参数 称之为 查询字符串
?key=value&key=value...
我们在登陆的时候会输入用户名和密码 理论上 用户名和密码都应该以POST方式进行传递
只是为了让大家好理解,我们接下来 用 get方式来传递用户名和密码
"""
##<QueryDict: {'username': ['test'], 'passwoed': ['test']}>
# #<QueryDict: {'username': ['itcast', 'itheima'], 'password': ['123']}>
# # QueryDict 以普通的字典形式来获取 一键多值的是时候 只能获取最后的那一个值
# # 我们想获取 一键一值的化 就需要使用 QueryDict 的get方法
# # 我们想获取 一键多值的化 就需要使用 QueryDict 的getlist方法
####接受请求
# query_params = request.GET
# # print(query_params)
# # username=query_params['username']
# username=query_params['username']
# users = query_params.getlist('username')
# password=query_params.get('password')
# # password=query_params['password']
# print(username)
# #test1
# print(users)
# ###['test', 'test1']
# return HttpResponse('detail')
###########################POST 表单数据#################################
# data=request.POST
# print(data)
#
# return HttpResponse('detail')
###########################POST json数据#################################
# # print(request.POST) ##无数据
# body = request.body
# # print(body)
# ##b'{\r\n "username":"test",\r\n "password":"test"\r\n}'
# ##byte数据转义
# body_str = body.decode() # JSON形式的字符串
# # print(type(body_str))
# ####str类型 字符串
# # """
# # json
# # json.dumps 将字典转换为 JSON形式的字符串
# # json.loads 将JSON形式的字符串 转换为字典
# # """
# data = json.loads(body_str)
# # print(data)
# print(request.META)
# print('我是有底线的')
# return HttpResponse('detail')
###########################跳转页面#################################
from django.shortcuts import redirect
# path = reverse('book:index')
# return redirect(path)
#
# return redirect('/index/')
# return redirect('https://www.sooele.com')
###########################请求头#################################
# #
# print(request.method)
# return HttpResponse('detail')
###########################JsonResponse#################################
# from django.http import JsonResponse
# data = {'name': 'itcast'}
#
# return JsonResponse(data)
###################HttpResponse#####################
# data={'name':'test'}
#HttpResponse
#content 传递字符串 不要传递 对象/字典数据
#status ValueError: HTTP status code must be an integer from 100 to 599. 规定的只能使用系统
# content_type 是一个MIME类型
# text/html text/css text/javascript
# application/json
# image/png image/gif image/jpeg
# return HttpResponse(data,status=400,content_type='')
# return HttpResponse(data,status=400)
"""
保存在客户端的数据叫做 cookie
0.概念
1.流程(原理)
第一次请求过程
① 我们的浏览器第一次请求服务器的时候,不会携带任何cookie信息
② 服务器接收到请求之后,发现 请求中没有任何cookie信息
③ 服务器设置一个cookie.这个cookie设置在相应中
④ 我们的浏览器接收到这个相应之后,发现相应中有cookie信息,浏览器会将cookie信息保存起来
第二次及其之后的过程
⑤ 当我们的浏览器第二次及其之后的请求都会携带cookie信息
⑥ 我们的服务器接收到请求之后,会发现请求中携带的cookie信息,这样的话就认识是谁的请求了
2.看效果
3.从http协议角度深入掌握cookie的流程(原理)
保存在服务器的数据叫做 session
"""
def cookie(request):
#1. 先判断有没有cookie信息
# 先假设就是没有
#2.获取用户名
username=request.GET.get('username')
#3. 因为我们假设没有cookie信息,我们服务器就要设置cookie信息
response = HttpResponse('cookie')
# key,value
response.set_cookie('username',username)
#4.返回相应
return response
urls
"""booktest URL Configuration
The `urlpatterns` list routes URLs to views. For more information please see:
https://docs.djangoproject.com/en/4.0/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,include
from book import urls as book_url
urlpatterns = [
path('admin/', admin.site.urls),
path('book/', include(('book.urls',"book"), namespace='book')),
# path('book/', include(book_url)),
]
setting
MIDDLEWARE = [
'django.middleware.security.SecurityMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
# 'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
]