标签:截断 linu ace add ken join lis 参数 input
cvb 即class view 和funtion view 两种方式 发送get和post请求
前后端不分离的写法,定义一个html(form.html),在view中写逻辑(cvb&fvb)
备注:需要注意csrf的问题,如果多次提交重复的数据会报403,解决办法两种
1.在settings.py-MIDDLEWARE-将csrf(第四行)注释掉 (
‘django.middleware.csrf.CsrfViewMiddleware‘,
)
2.在form.html中写{% csrf_token%}
form.html的代码:
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> </head> <body> <form action="/add_article/" method="post"> {% csrf_token %} title:<input type="text" name="title"> desc:<input type="text" name="desc"> content:<input type="text" name="content"> category:<select name="category"> {% for c in categorys %} <option value="{{ c.id }}">{{ c.name }}</option> {% endfor %} </select> <input type="submit" value="提交"> </form> </body> </html>
views.py中的代码
#function fvb方式 def add_article(request): if request.method == ‘GET‘: categorys = Category.objects.all() return render(request,‘form.html‘,locals()) else: title = request.POST.get(‘title‘) desc = request.POST.get(‘desc‘) content = request.POST.get(‘content‘) category = request.POST.get(‘category‘) article = Article(title=title,desc=desc,category_id=category,content=content) article.save() return HttpResponseRedirect(‘/blog‘)#重定向 #class cvb方式 class ArticleView(View): def get(self,request): categorys = Category.objects.all() return render(request, ‘form.html‘, locals()) def post(self,request): title = request.POST.get(‘title‘) desc = request.POST.get(‘desc‘) content = request.POST.get(‘content‘) category = request.POST.get(‘category‘) article = Article(title=title, desc=desc, category_id=category, content=content) article.save() return HttpResponseRedirect(‘/blog‘) # 重定向
Django自带过滤器
1. 页面显示部分内容,超过长度内容使用.... 显示
2.list、字符串切片显示
3.给默认值
4.时间戳按照格式化显示
5.css、js注入
views.py代码
def demo(request): import datetime cur_time = datetime.datetime.now() age = 18 name = ‘haha‘ article_content = ‘人生如此美好应该学习python的django常用使用‘ comments = ‘<h1 style="font-size:500px">hello</h1>‘ navs = [‘linux‘, ‘python‘, ‘java‘, ‘c++‘, ‘jmeter‘] return render(request, ‘demo.html‘, locals()) #locals() 返回所有的参数
demo.html代码
<body> {{ age|add:‘1‘ }} <br> {{ name|upper }} {{ name|lower }} {{ name|add:‘先生‘|upper }} {# 大小写、字符串则自动拼接、支持多次拼接 #} <br> {{ article_content|truncatechars:"10"}} {# 截断字符串,多余的字符串用....显示#} <br> {{ navs|slice:"0:3" }} {# list切片,显示前3个元素#} <br> {{ navs|join:"==" }} <br> ----长度{{ navs|length}} <br> {{ author|default:"管理员" }} {# 默认值#} <br> {{ cur_time|date:‘Y-m-d H:i:s‘ }} {#时间格式化显示#} <br> {{ js_str|safe }} <br> {{ comments|safe }} {# 如果后面加safe,认为是安全的字符串,浏览器会按照标签解析;不加,浏览器按照字符串解析#} {# css、js注入#} </body>
自定义过滤器
views.py代码
def demo(request): import datetime cur_time = datetime.datetime.now() age = 18 name = ‘haha‘ article_content = ‘人生如此美好应该学习python的django常用使用‘ comments = ‘<h5 style="font-size:200px">你好呀</h5>‘ js_str = "<script>alert(‘哈哈哈‘)</script>" navs = [‘linux‘, ‘python‘, ‘java‘, ‘c++‘, ‘jmeter‘] article_content2 = ‘朝鲜一个金三胖,已经登录太阳,因为太热,选择晚上去月球‘ say = ‘今天吃饭的时候,碰见了一个没素质213的人,嘴里一直说脏话sb‘ return render(request, ‘demo.html‘, locals()) #locals() 返回所有的参数
demo.html代码
{% load my_tags %}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body> {{ article_content2|hanmin}} <br> {{ article_content2|hanmin2:"金丝胖" }} {#自定义过滤器#} <br> {% mingan3 say ‘213‘ ‘sb‘ %} ========== {# 需要替换多个参数,需要用simple_tag#} <br> </body>
my_tags.py自定义过滤器
from django.template import Library register = Library() #固定写法,必须叫register #自定义过滤器,单个参数 @register.filter(name=‘hanmin‘) 定义了名称,html文件中使用定义的name def mingan(value): ‘‘‘将金三胖替换为金正恩‘‘‘ if ‘金三胖‘ in value: value = value.replace(‘金三胖‘, ‘金正恩‘) return value #自定义过滤器,最多2个参数 @register.filter(name=‘hanmin2‘) def mingan2(value, s): ‘‘‘将金三胖替换为s‘‘‘ if ‘金三胖‘ in value: value = value.replace(‘金三胖‘, s) return value # ‘你是个sb123666‘, (sb,123,666) simple_tag,可以过滤多个参数 @register.simple_tag def mingan3(value, *args): ‘‘‘将敏感词替换为*‘‘‘ for line in args: if line in value: value = value.replace(line, ‘**‘) return value
标签:截断 linu ace add ken join lis 参数 input
原文地址:https://www.cnblogs.com/lhly/p/12236308.html