这一篇,将会展示用Python语言实现用户信息的增删改查
首先,先搭建项目框架,这个在上一节里有提到,就不再写
然后 ,搭好项目,就开始编码了,首先,先分析,我们要实现用户信息的管理,就先写好界面,根据界面来写数据库,这里只是实现最简单的用户信息管理,一般有用户名,密码,头像。实现增删改,就是需要三个botton。页面代码展示
登录页面代码`
后台登录
font:14px center;
line-height:50px;
}
.in{
height:30px;
}
margin-top:20px;
width:90px;
height:30px;
font-size:14px;
这是用户信息列表页面
用户信息
font:14px center;
line-height:50px;
}
.in{
height:30px;
}
.ad{
margin-top:15px;
width:40px;
height:35px;
background-color:#ccc;
ID | 头 像 | 用户名 | 密 码 | 操 作 |
---|---|---|---|---|
{ {res.id}} | |
{
{res.username}}
{
{res.password}}
下载
{%endfor%}
`这是添加用户信息页面
<html>
<head>
<meta content="text/html; charset=UTF-8" http-equiv="Content-Type">
<meta charset="utf-8">
<title>添加用户title>
<style type="text/css">
*{
margin:0;
padding:0;
}
#u{
font:14px center;
line-height:50px;
}
.in{
height:30px;
}
#log{
margin-top:20px;
width:90px;
height:30px;
font-size:14px;
style>
head>
<body>
<center>
<form action="/addinform/" method="post">
{% csrf_token %}
<span id="u">用户名:span>
<input class="in" type="text" name="username"/><br/>
<span id="p">密 码:span>
<input class="in" type="text" name="password"/><br/>
<input type="submit" value="保 存" id="sa"/>
form>
center>
body>
html>
这是修改页面
<html>
<head>
<meta content="text/html; charset=UTF-8" http-equiv="Content-Type">
<meta charset="utf-8">
<title>添加用户title>
<style type="text/css">
*{
margin:0;
padding:0;
}
#u{
font:14px center;
line-height:50px;
}
.in{
height:30px;
}
#log{
margin-top:20px;
width:90px;
height:30px;
font-size:14px;
style>
head>
<body>
<center>
<form action="/domodify/{
{
us.id}}/" method="post">
{% csrf_token %}
<span id="u">用户名:span>
<input class="in" type="text" name="username" value="{
{
us.username}}" /><br/>
<span id="p">密 码:span>
<input class="in" type="text" name="password" value="{
{
us.password}}"/><br/>
<input type="submit" value="修 改" id="do"/>
form>
center>
body>
html>
文件,图片上传界面
<html>
<head>
<meta content="text/html; charset=UTF-8" http-equiv="Content-Type">
<meta charset="utf-8">
<title>上传title>
<style type="text/css">
*{
margin:0;
padding:0;
}
#u{
font:14px center;
line-height:50px;
}
.in{
height:30px;
}
#log{
margin-top:20px;
width:90px;
height:30px;
font-size:14px;
style>
head>
<body>
<center>
<form enctype="multipart/form-data" action="/upload/?id={
{
us}}/" method="post">
{% csrf_token %}
<input type = "hidden" name = "id" value = "{
{
us}}"/>
<input class="in" type="file" name="file" value="{
{
uf.img}}"/><br />
<input type="submit" value="上 传" id="log"/>
form>
center>
body>
html>
以上是界面,下面我们在models.py里建表
from __future__ import unicode_literals
from django.db import models
# Create your models here.
class User(models.Model):
username = models.CharField(max_length = 50)
password = models.CharField(max_length = 50)
img = models.CharField(max_length = 100)
这里文件上传,有些地方直接用filefield也是可以的
如果上传图片,也可直接写,但是需要安装pillow插件才能不报错,如:
img = models.ImageField(upload_to = './upload/',null=True)
写完数据库,在cmd里执行初始化,便生成了数据库,可以安装mysql的视图软件,直接可以看到数据表
下一步,在views.py里实现功能
#encoding:utf-8
from django.shortcuts import render
from django.shortcuts import render_to_response
from django.http import HttpResponse,HttpResponseRedirect
from models import User
from django.http import StreamingHttpResponse
from django import forms
import StringIO
import re
import os
import xlwt
#encoding:utf-8
# Create your views here.
# coding=
class userForm(forms.Form):
username = forms.CharField()
password = forms.CharField()
img = forms.FileField(required=False)
def Login(request):
return render(request,'login.html')
def dologin(request):
if request.method == 'POST':
uf = userForm(request.POST)
if uf.is_valid():
username = uf.cleaned_data['username']
password = uf.cleaned_data['password']
user = User.objects.filter(username__exact = username,
password__exact = password )
if user:
users = User.objects.all()
con = {
'us':users}
return render(request,'inform.html',con)
else:
return HttpResponseRedirect('/login/')
else:
return render_to_response('login.html')
def add(request):
return render(request,'add.html')
def addinform(request):
if request.method == 'POST':
uf = userForm(request.POST)
if uf.is_valid():
username = uf.cleaned_data['username']
password = uf.cleaned_data['password']
user = User(username=username,password=password)
user.save()
users = User.objects.all()
con = {
'us':users}
return render(request,'inform.html',con)
else:
return render(request,'add.html')
def deletett(request,p):
User.objects.get(id=p).delete()
users = User.objects.all()
con = {
'us':users}
return render(request,'inform.html',con)
def modify(request,p):
u = User.objects.get(id=p)
con = {
'us':u}
return render(request,'modify.html',con)
def domodify(request,p):
if request.method == 'POST':
uf = userForm(request.POST)
if uf.is_valid():
username = uf.cleaned_data['username']
password = uf.cleaned_data['password']
u = User.objects.get(id=p)
u.username = username
u.password = password
u.save()
users = User.objects.all()
con = {
'us':users}
return render(request,'inform.html',con)
def img(request):
u = request.GET.get('id')
return render(request,'img.html',{
"us":u})
#上传
def upload(request):
if request.method == 'POST':
uf = userForm(request.POST, request.FILES)
print "aaaaaaaaaaaaaa"
#if uf.is_valid():
ids = request.POST["id"]
print 'eeeeeeee',ids
img = request.FILES.get('file')
file_name = str(img.name)
print type(file_name)
handle_upload_file(request.FILES['file'])
u = User.objects.get(id = ids)
u.img = file_name
print "aaaaaasssfdfdg"
u.save()
print "qqwwee"
users = User.objects.all()
print "uuuuuuu"
con = {
'uf':users}
print "rrrrrrrrre"
return HttpResponseRedirect("/login/")
print "ddddddddddd"
else:
return HttpResponseRedirect("/img/")
#下载
def download(request):
print "wwww"
def file_iterator(file_name, chunk_size = 512): #32位系统
with open(file_name) as f:
while True:
c = f.read(chunk_size) #迭代器,适用于大小文件
if c:
yield c
else:
break
print "aaaa"
img = r'E:\pywork\mysite\products\static\\'+request.GET.get('img')
print "mmm",type(img),img
#StreamingHttpResponse用于将文件流发送给浏览器,优于HttpResponse
response = StreamingHttpResponse(file_iterator(img))
#让文件流写入硬盘,防止乱码
response["Content-Type"] = "application/octet-stream"
response["Content-Disposition"] = "attachment;filename={0}".format(img)
print "fff"
return response
在views.py里实现了功能之后,需要你在urls.py里配置方法的路径,这里用到了正则表达式,不懂得童鞋,下次再详细说
下面是在urls.py里
from django.conf.urls import patterns, include, url
from django.contrib import admin
from products import views
from django.conf import settings
import os
urlpatterns = patterns('',
# Examples:
# url(r'^$', 'mysite.views.home', name='home'),
# url(r'^blog/', include('blog.urls')),
url(r'^admin/', include(admin.site.urls)),
url(r'^login/', views.Login),
url(r'^dologin/', views.dologin),
url(r'^add/', views.add),
url(r'^addinform/', views.addinform),
url(r'^del/(.+)/$', views.deletett),
url(r'^modify/(.+)/$', views.modify),
url(r'^domodify/(.+)/$', views.domodify),
url(r'^img/', views.img),
url(r'^upload/', views.upload),
url(r'^download/', views.download),
url(r'^static/(?P.*)$' ,'django.views.static.serve',{
'document.root':settings.STATICFILES_DIRS}),
url(r'^output/', views.output),
)
以上,就是简单实现用户信息管理的代码实现,当然,代码有很多可以改进的地方,只做参考。