码迷,mamicode.com
首页 > 其他好文 > 详细

django 测试7之教育

时间:2019-05-23 23:18:01      阅读:149      评论:0      收藏:0      [点我收藏+]

标签:NPU   include   UNC   密码   ase   har   ima   sheet   turn   

setting:


STATIC_URL = ‘/static/‘
# 上传图片的路径
UPLOAD_ROOT = os.path.join(BASE_DIR,‘upload‘)
STATICFILES_DIRS = [
os.path.join(BASE_DIR, ‘static‘),

]
 
 
 
 
主urls:
from django.contrib import admin
from django.urls import path,include

urlpatterns = [
path(‘admin/‘, admin.site.urls),
path(‘‘,include(‘jishi.urls‘))
]
 
 
 
 
 
 
副urls:
from django.contrib import admin
from django.urls import path,include,re_path
from jishi import views


from django.views.static import serve
from lianxi.settings import UPLOAD_ROOT

urlpatterns = [
# path(‘admin/‘, admin.site.urls),
path(‘register/‘,views.register.as_view()),
path(‘login/‘,views.login.as_view()),
path(‘add_class/‘,views.add_class.as_view()),
re_path(‘^upload/(?P<path>.*)$‘,serve,{‘document_root‘:UPLOAD_ROOT}),
path(‘‘,views.index.as_view()),
path(‘delete/‘,views.delete.as_view())

]
 
 
 
 
 
 
models:
from django.db import models

# Create your models here.


class User(models.Model):
name = models.CharField(max_length=20)
pwd = models.CharField(max_length=20)
permission = models.CharField(max_length=2)

class Meta:
db_table = ‘user‘
def __str__(self):
return self.name

class Class(models.Model):
name = models.CharField(max_length=20)
image = models.CharField(max_length=255)
num = models.CharField(max_length=50)
u_id = models.ForeignKey(User,on_delete=models.SET_NULL,null=True,blank=True)

class Meta:
db_table = ‘class‘
def __str__(self):
return self.name
 
 
 
 
 
 
views:
from django.shortcuts import render,redirect
from django.views import View
from jishi.models import User,Class
from django.http import HttpResponse

# Create your views here.

class register(View):
def get(self,request):
return render(request,‘register.html‘,locals())
def post(self,request):
mes = ""
name = request.POST.get(‘name‘)
pwd = request.POST.get(‘pwd‘)
permission = request.POST.get(‘permission‘)
if all([name,pwd,permission]):
User.objects.create(
name= name,
pwd= pwd,
permission= permission
)
mes = ‘添加成功‘
else:
mes = ‘数据不完整‘
return render(request, ‘register.html‘, locals())


class login(View):
def get(self,request):
return render(request,‘login.html‘,locals())
def post(self,request):
mes = ""
name = request.POST.get(‘name‘)
pwd = request.POST.get(‘pwd‘)
if all([name,pwd]):
user = User.objects.filter( name = name).first()
if user:
if user.pwd == pwd:
request.session[‘name‘]= user.name
request.session[‘id‘] = user.id
return redirect(‘/add_class/‘)
else:
mes = ‘密码不正确‘
else:
mes = ‘用户名不存在‘
else:
mes = ‘信息不全‘
return render(request, ‘login.html‘, locals())


from lianxi.settings import UPLOAD_ROOT
import os
def uploadfile(img):
f = open(os.path.join(UPLOAD_ROOT,‘‘,img.name),‘wb‘)
#写文件 遍历图片文件流
for chunk in img.chunks():
f.write(chunk)


class add_class(View):
def get(self, request):
id = request.session.get(‘id‘)
user = User.objects.filter(id =id).first()
return render(request,‘add_class.html‘,locals())
def post(self, request):

id =request.session.get(‘id‘)
name = request.POST.get(‘name‘)
img = request.FILES.get(‘image‘)
num = request.POST.get(‘num‘)
uploadfile(img)
c =Class.objects.create(
u_id_id= id,
name = name,
image= ‘/upload/‘ + img.name,
num=num
)
return redirect(‘/add_class/‘)

from django.core.paginator import Paginator
from utils.comm import PRECATECOUNT


class index(View):
def get(self,request):
id = request.session.get(‘id‘)
c = Class.objects.order_by(‘-num‘).all()
# courls = Courls.objects.all()
# 每一页可以展示多少条数据
page_count = PRECATECOUNT
# 默认为第一页
current_page = int(request.GET.get(‘p‘, 1))
g = Paginator(c, page_count)
# 每页显示的信息
clist = g.get_page(current_page)
# 一共显示多少页
total_page = g.num_pages
return render(request, "index.html", {"c": c, "clist": clist, "total_page": total_page,
"current_page": current_page, "id": id})


class delete(View):
def get(self,request):
# user_id = request.session.get("id")
# print(user_id)
id = request.GET.get("id")
print(id)
c = Class.objects.filter(id = id).delete()
return redirect(‘/‘)









 
html:
add_class:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>添加课程</title>


</head>
<body>
<form action="" method="post" enctype="multipart/form-data">

{% if user.permission == ‘0‘ %}
课程名称:<input type="text" name="name"><br>
课程图片:<input type="file" name="image"><br>
课程人数: <input type="text" name="num"><br>
{% else %}
<a href="/">首页</a>
{% endif %}
</form>
</body>
</html>
 
 
 
 
index:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<link rel="stylesheet" href="/static/admin/css/jquery.pagination.css">
  <script type="text/javascript" src="/static/admin/js/jquery-1.12.4.min.js"></script>
  <script type="text/javascript" src="/static/admin/js/jquery.pagination.min.js"></script>

<title>首页展示</title>
</head>
<body>
<table border="1">
<tr>
<td>课程名称</td>
<td>课程教师</td>
<td>课程图片</td>
<td>课程人数</td>
<td>是否删除</td>
</tr>
{% for i in clist %}
<tr>
<td>{{ i.name }}</td>
<td>{{ i.u_id.name }}</td>
<td><img src="{{ i.image }}" width="50px" height="50px"></td>
<td>{{ i.num }}</td>
{% if i.u_id_id == id %}
<td><a href="/delete/?id={{ i.id }}">删除</a> </td>
{% else %}
<td>无权限删除</td>
{% endif %}
</tr>


{% endfor %}
</table>

<div id="pagination"></div>

</body>
<script type="text/javascript">
$("#pagination").pagination({
          currentPage: {{current_page}},
          totalPage: {{total_page}},
          callback: function(current) {
          var keyword = $(".input_txt").val()
          window.location.href="/?p="+current
 }
});
</script>

</html>
 
 
 
 
login:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>登录</title>
</head>
<body>
<form action="" method="post">
用户名:<input type="text" name="name"><br>
密码 : <input type="password" name="pwd"><br>
<button type="submit">登录</button><br>

<a href="/register/">注册页面</a>

{{ mes }}
</form>
</body>
</html>
 
 
 
 
register:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>添加用户</title>
</head>
<body>
<form action="" method="post">
用户名:<input type="text" name="name"><br>
密码 : <input type="password" name="pwd"><br>
是否有权限:<input type="radio" name="permission" value="0">是
<input type="radio" name="permission" value="1">否
<button type="submit">注册</button>
<br>
<a href="/login/">登录页面</a>

{{ mes }}
</form>
</body>
</html>
 

django 测试7之教育

标签:NPU   include   UNC   密码   ase   har   ima   sheet   turn   

原文地址:https://www.cnblogs.com/lhrd/p/10915022.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!