目录
一、自己封装response
二、在响应头中放数据
HttpResponse
redirect
编辑
render
JsonResponse
三、函数和方法区别 ----》绑定方法区别
四、上传图片和开启media访问
五、页面静态化(解决访问率高的问题)
from django.shortcuts import HttpResponse
import json
class MyResponse(HttpResponse):
def __init__(self, data):
res = json.dumps(data, ensure_ascii=False) # 进行序列化
return super().__init__(res) # 将res传进来
from django.shortcuts import render
# Create your views here.
from .response import MyResponse # 相对导入
def index(request):
return MyResponse({'code': 100, 'msg': '请求成功'})
from django.contrib import admin
from django.urls import path
from app01.views import index
urlpatterns = [
path('admin/', admin.site.urls),
path('', index), # 访问根路径
]
class MyResponse(HttpResponse):
def __init__(self, data):
if isinstance(data, dict) or isinstance(data, list):
res = json.dumps(data, ensure_ascii=False) # 进行序列化
return super().__init__(res) # 将res传入
def index(request):
return HttpResponse('ok', headers={'xxx': 'xxx'})
def index(request):
# return HttpResponse('ok', headers={'xxx': 'xxx'})
obj = HttpResponse('ok')
obj['yyy'] = 'yyy' # 像字典一样放入,最终会放在http的响应头中
return obj
return redirect('http://www.baidu.com')
from django.shortcuts import render,redirect,HttpResponse,resolve_url
from django.http import JsonResponse
# Create your views here.
from .response import MyResponse
def index(request):
# return redirect('http://www.baidu.com')
# res = resolve_url('login') # 反向解析,通过名字拿到真正的字符串的地址
# return redirect('/login', headers={'xxx': 'ssss'}) # 不生效
obj = redirect('/login')
obj['xxx'] = 'xxx'
return obj
def login(request):
return HttpResponse('login')
from django.contrib import admin
from django.urls import path
from app01.views import index, login
urlpatterns = [
path('admin/', admin.site.urls),
path('', index), # 访问根路径
path('login/', login, name='login'), # 访问login
]
# return render(request,'index.html',headers={'xxx': 'ssss'}) # 不行
obj = render(request, 'index.html')
obj['xxx'] = 'xxx'
return obj
return JsonResponse({'name': 'lqz'}, headers={'xxx': 'ssss'})
obj = JsonResponse({'name': 'lqz'})
obj['yyy'] = 'yyy'
return obj
class Person:
# 对象绑定方法 ---》写在类中,没有任何装饰器
def run(self):
# print(self.name)
print('人走路')
### 对象绑定方法 ###
# p = Person()
# p.run()
Person.run(1)
Person.run(Person())
### 绑定给类的方法 ###
# Person.xx() # 正统 类来调用
# Person().xx()
p = Person()
p.xx() # 对象来调用,类的绑定方法,会自动把当前对象的类拿到,传入进去
class Person:
# 对象绑定方法---》写在类中,没有任何装饰器
def run(self):
# print(self.name)
print('人走路')
@classmethod
def xx(cls):
# 把类传入了,类可以实例化得到对象
p = cls() # 直接类实例化得到对象,要不要传参数,取决于Person类有没有写__init__
print(p)
print('类的绑定方法,xxx')
# 静态方法 --->本质就是个函数
@staticmethod
def yy():
print('staticmethod')
Person.yy() # 类来调用
Person().yy() # 对象来调用
总结: 函数和方法
查看一个 '函数' 到底是函数还是方法
from types import FunctionType, MethodType
def add():
pass
print(isinstance(add,FunctionType)) # True
print(isinstance(add,MethodType)) # Fals
print(isinstance(Person.xx,FunctionType)) # False # 类来调用是个方法
print(isinstance(Person.xx,MethodType)) # True
print(isinstance(Person().xx,FunctionType)) # False # 类来调用是个方法
print(isinstance(Person().xx,MethodType)) # True
print(isinstance(Person.yy,FunctionType)) # True 静态方法,自动传值了吗? 没有,就是函数
print(isinstance(Person.yy,MethodType)) # False
print(isinstance(Person().run,FunctionType)) # false
print(isinstance(Person().run,MethodType)) # True
print(isinstance(Person.run,FunctionType)) # True 不能自动传值---》就是函数
print(isinstance(Person.run,MethodType)) # False
from django.shortcuts import render, redirect, HttpResponse
from django.conf import settings
def index(request):
obj = render(request, 'index.html')
obj['xxx'] = 'xxx'
return obj
def login(request):
return HttpResponse('login')
# from django_demo04 import settings
# django 有两套配置文件--》一套是项目自己的,一套内置的
def upload_img(request):
myfile = request.FILES.get('myfile')
print(settings.MEDIA_ROOT)
# with open(settings.MEDIA_ROOT + '/%s' % myfile.name, 'wb') as f:
with open('media/%s' % myfile.name, 'wb') as f:
for line in myfile:
f.write(line)
return HttpResponse('图片上传成功')
from django.contrib import admin
from django.urls import path
from app01.views import upload_img
urlpatterns = [
path('admin/', admin.site.urls),
path('', index), # 访问根路径
path('login/', login, name='login'),
path('uplowd_img', upload_img),
]
MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
启动之后发现没有开启这个路径
1 static文件夹,配置文件写好了,会自动开启
所以在static文件夹下不要放重要内容,因为客户端可以直接下载访问!
STATIC_URL = '/static/'
STATICFILES_DIRS = [os.path.join(BASE_DIR, 'static')]
MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
2 我们想让media这个文件夹像static文件夹一样,能被用户浏览器访问
访问的路径是:http://127.0.0.1:8000/ media/default.png
正则方法 re_path
re_path('^media/(?P.*)', serve, kwargs={'document_root': settings.MEDIA_ROOT}),
转换器 path
path('media/', serve, kwargs={'document_root': settings.MEDIA_ROOT})
即
from django.contrib import admin
from django.urls import path, re_path
from app01.views import upload_img, index, login
from django.views.static import serve
from django.conf import settings
urlpatterns = [
path('admin/', admin.site.urls),
path('', index), # 访问根路径
path('login/', login, name='login'),
path('uplowd_img', upload_img),
path('media/', serve, kwargs={'document_root': settings.MEDIA_ROOT})
]
html
Title
看帅哥
总结:
以后想开启media的访问
1 在项目根路径创建media文件
2 在配置文件中配置
MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
3 路由中配置:
path('media/', serve, kwargs={'document_root': settings.MEDIA_ROOT})
结果如下
models.py
from django.db import models
# Create your models here.
class Book(models.Model):
name = models.CharField(max_length=32)
price = models.IntegerField()
publish = models.CharField(max_length=64)
views.py
from .models import Book
from django.conf import settings
from django.template import Template, Context
import os
def books_view(request):
# 做静态化
if os.path.exists(os.path.join(settings.BASE_DIR, 'cache', 'books.html')):
print('不走数据库')
with open('cache/books.html', 'rt', encoding='utf-8') as f:
res_str = f.read()
return HttpResponse(res_str)
else:
books = Book.objects.all()
with open('templates/books.html', 'rt', encoding='utf-8') as f:
res = f.read()
t = Template(res)
c = Context({'books': books})
html = t.render(c)
# 保存起来
with open('cache/books.html', 'wt', encoding='utf-8') as f:
f.write(html)
return HttpResponse(html)
urls.py
from app01.views import upload_img, index, login,books_view
from django.views.static import serve
from django.conf import settings
urlpatterns = [
path('admin/', admin.site.urls),
path('', index),
path('books/', books_view),
books.html
Title
id号
图书名
图书价格
出版社
{% for book in books %}
{{ book.id }}
{{ book.name }}
{{ book.price }}
{{ book.publish }}
{% endfor %}
插入一些数据
import os
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'django01.settings')
import django
django.setup()
import random
from app01.models import Book
for i in range(100):
Book.objects.create(name='图书_%s'%i,price=random.randint(1,100),publish='%s出版社'%i)