作者:christinezzy850 | 来源:互联网 | 2023-05-28 12:59
直接切入正题,手把手直接来写一个类似博客园的项目我们先来新建一个django项目红框的地方,填写你的项目名称,还有项目中的应用.create创建.进入了显示是在urls路由分发
直接切入正题,手把手直接来写一个类似博客园的项目
alt="" width="50px" height="50px">div>
<div class="text col-md-11">
本文中的提到GNU扩展时,表示该功能是GNU为sed提供的(即GNU版本的sed才有该功能),一般此时都会说明:如果要写具有可移植性的脚本,应尽量避免在脚本中使用该选项。
本文中的正则表达式几乎和grep中支持的一样。但还是有少数几个是sed自身才能解析的表达式。因此本译文中只对这些sed自身才支持 ...
div>
div>
<div class="row">
<div class="icon">
<span><a href="">Yuan a> 发布于span>
<span class="timer"> 2017-09-05 10:12span>
<span class="glyphicon glyphicon-comment">span> <a href="">评论12a>
<span class="glyphicon glyphicon-thumbs-up">span> <a href="">赞34a>
div>
div>
li>
ul>
div>
{# 文章区域#}
{# 右侧块区#}
<div class="col-md-3">
<div class="panel panel-default">
<div class="panel-heading">标题一div>
<div class="panel-body">
Panel content
div>
div>
<div class="panel panel-default">
<div class="panel-heading">
<h3 class="panel-title">标题二h3>
div>
<div class="panel-body">
Panel content
div>
div>
div>
{# 右侧块区#}
div>
div>
{#内容区域#}
<script src="{% static 'dist/js/jquery-3.2.1.js' %}">script>
<script src="{% static 'dist/js/bootstrap.js' %}">script>
<script>
script>
body>
html>
之后是index 的视图函数:
def index(request,**kwargs):
user=request.session['user']
type_choices = models.Article.type_choices
print('kwargs',kwargs)
current_type_choices_id=int(kwargs.get('article_type_id',0))
# 用户选择的ID
article_list = models.Article.objects.filter(**kwargs)
# 文章列表
return render(request,'index.html',{'type_choices':type_choices,'current_type_choices_id':current_type_choices_id,'article_list':article_list,'user':user})
之后是登录页面,也直接上代码:
{% load staticfiles %}
DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Titletitle>
<link href="{% static 'dist/css/bootstrap.css' %}" rel="stylesheet">
<link rel="stylesheet" href="{% static 'dist/css/my_style.css' %}">
head>
<style>
.container {
margin-top: 80px;
}
.valid_img {
width: 150px;
height: 40px;
}
.jiancha{
color: red;
}
style>
<body>
<div class="container">
<div class="row">
<div class="col-md-6 col-md-offset-3">
<div class="form-group">
<label for="user">USERlabel>
<input type="text" class="form-control" id="user" placeholder="user" name="user">
div>
<div class="form-group">
<label for="pwd">Passwordlabel>
<input type="password" class="form-control" id="pwd" placeholder="pwd" name="pwd">
div>
<div class="form-group">
<label for="v_code">验证码label>
<div class="row">
<div class="col-md-6">
<input type="text" class="form-control" id="v_code" placeholder="verification"
name="v_code">
div>
<div class="col-md-6">
<img class="valid_img" src="/valid_code/"
alt="" width="150px" height="50px">
div>
div>
div>
<button type="submit" class="btn btn-default">登录button>
<span class="hide jiancha" >请检查账户与验证码是否正确span>
div>
div>
div>
<script src="{% static 'dist/js/jquery-3.2.1.js' %}">script>
<script src="{% static 'dist/js/bootstrap.js' %}">script>
<script>
$('[name=pwd]').val();
$('button').click(function () {
$.ajaxSetup({
data: {csrfmiddlewaretoken: '{{ csrf_token }}'}
});
$.ajax({
url: '/login/',
type: 'post',
data: {user: $("[name='user']").val(), pwd: $("[name='pwd']").val(), v_code: $("[name='v_code']").val()},
success: function (data) {
data=JSON.parse(data);
console.log(data['statu']);
if (data['statu']==false){
$(".jiancha").removeClass('hide');
}
else {
location.href='/index/'
}
}
})
})
script>
body>
html>
给上login页面的函数:
def login(request):
data={'statu':False}
print(request.user)
if request.method=='POST':
print(request.POST)
print(request.session["valid_code"])
user=request.POST.get('user')
pwd=request.POST.get('pwd')
v_code=request.POST.get('v_code').upper()
if user=='egon' and pwd=='123' and v_code==request.session["valid_code"].upper():
data['statu']=True
request.session['user']='egon'
return HttpResponse(json.dumps(data))
return render(request,'login.html')
def valid_code(request):
from PIL import Image,ImageDraw,ImageFont
from io import BytesIO
import random
f=BytesIO()
img = Image.new(mode='RGB', size=(120, 30), color=(random.randint(0,255), random.randint(0,255),random.randint(0,255)))
draw = ImageDraw.Draw(img, mode='RGB')
font=ImageFont.truetype("test_demo/app01/static/dist/font/Lobster-Regular.ttf",28)
code_list = []
for i in range(5):
char = random.choice([chr(random.randint(65, 90)), str(random.randint(1, 9))])
code_list.append(char)
draw.text([i * 24, 0], char, (random.randint(0, 255), random.randint(0, 255), random.randint(0, 255)),
font=font)
img.save(f,"png")
valid_code=''.join(code_list)
request.session["valid_code"]=valid_code
return HttpResponse(f.getvalue())
顺便还有验证码的.
今天就暂时到这里,写的真是我自己都看不下去了,还是没有养成写文档的习惯啊