python账号怎么注销_python django 快速实现注册,登录,注销

临近年底,各种忙,好久没更新博客了,2017春节假期在即,距下班还有2小时,难得闲下来,来撸一手django简单的web注册,登录,注销。

环   境:centos 6.4 64bit

python :python 2.7.8

django :1.9.6

author:soul

date  :2017-01-24

1、创建一个项目project  名为sheying

#django-admin startproject sheying

2、创建一个项目应用app  名为danfan

#python manage.py startapp danfan

3、创建一个模板目录,存放html展示页面

在danfan目录下mkdir templeates

4、设置项目sheying 的settings参数

添加app应用以及模板路径,数据库链接

TEMPLATE_DIRS = (

'/usr/local/python27/sheying/danfan/templeates',

)

INSTALLED_APPS = (

#添加app

'danfan',

)

MIDDLEWARE_CLASSES中添加中文支持,注释安全检测

'django.middleware.locale.LocaleMiddleware',

#'django.middleware.csrf.CsrfViewMiddleware',

DATABASES = {

'default': {

'ENGINE': 'django.db.backends.mysql',           //这里用的是mysql

'NAME': 'sheying',    //数据库名

'USER': 'soul',       //用户名

'PASSWORD': 'password',//密码

'HOST': '127.0.0.1',

'PORT': '3306',

}

}

5、在danfan目录下定义数据模型models(User,Info)

User: 用于存在用户注册的用户数据

Info:用于存在图片的信息详细。

models.py

# -*-coding:utf-8 -*-

from django.db import models

class User(models.Model):

username = models.CharField('用户名',max_length=32)

password = models.CharField('密码',max_length=32)

email = models.EmailField('邮箱')

def __unicode__(self):

return self.username

class Info(models.Model):

BRAND_CHOICES = (

('Canon', 'Canon'),

('Nikon', 'Nikon'),

('Sony', 'Sony'),)

TYPE_CHOICES = (

('风景', '风景'),

('人像', '人像'),

('写真', '写真'),)

title = models.CharField('标题',max_length=64)

type = models.CharField('类型',choices=TYPE_CHOICES, max_length=16, default = '风景')

author = models.CharField('摄影师',max_length=32)

place = models.CharField('拍摄场所',max_length=64)

brand = models.CharField('品牌',choices=BRAND_CHOICES, max_length=16,default='Nikon')

model = models.CharField('型号',max_length=16)

scene = models.CharField('镜头',max_length=64)

description = models.TextField('描述',blank=True, null=True)

def __unicode__(self):

return self.title

定义表单 forms.py  (用于注册和登录信息框输入)

# -*-coding:utf-8 -*-

from django import forms

class UserForm(forms.Form):

username = forms.CharField(label='账号:',max_length=16, error_messages={'required': '请填写您的称呼','max_length': '称呼太长'})

password = forms.CharField(label='密码:',widget=forms.PasswordInput(),error_messages={'required': '请填写密码'})

email = forms.EmailField(label='邮箱:',error_messages={ 'required': '请输入你的邮箱','invalid': '邮箱格式不正确'})

class LoginForm(forms.Form):

username = forms.CharField(label='账号:',max_length=16, error_messages={'required': '请输入用户名','max_length':'用户名太长'})

password = forms.CharField(label='密码:',widget=forms.PasswordInput(),error_messages={'required': '请输入密码'})

6、同步数据,生成数据表和表单(1.7以上版本)

#python manage.py syncdb

# python manage.py makemigrations

# python manage.py migrate

(创建管理员账号过程略......)

7、创建视图(danfan目录下的views.py)

#coding=utf-8

from django.shortcuts import render

from forms import UserForm

from forms import LoginForm

from django.shortcuts import render_to_response

from django.http import HttpResponse,HttpResponseRedirect

from django.template import RequestContext

from .models import User,Info

#注册用户

def register(request):

if request.method == "POST":

uf = UserForm(request.POST)

if uf.is_valid():

username = uf.cleaned_data['username']

password = uf.cleaned_data['password']

email = uf.cleaned_data['email']

User.objects.create(username= username,password=password,email=email)

return render_to_response('success.html',{'username':username})

else:

uf = UserForm()

return render_to_response('register.html',{'uf': uf})

#登录账号

def login(request):

if request.method == "POST":

uf = LoginForm(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:

response = HttpResponseRedirect('/danfan/add')

response.set_cookie('username',username,3600)

return response

else:

return HttpResponseRedirect('/danfan/login')

else:

uf = LoginForm()

return render_to_response('login.html',{'uf': uf})

#添加图片分享

def add(request):

if request.method == 'GET':

return render(request,'add.html')

elif request.method == 'POST':

title = request.POST['title']

type = request.POST['type']

author = request.POST.get('author', '')

place = request.POST.get('place', '')

brand = request.POST['brand']

model = request.POST.get('model', '')

scene = request.POST.get('scene', '')

description = request.POST.get('description', '')

info = Info.objects.filter(title=title)

if info:

return HttpResponse('Title %s have been exist' % title)

info = Info(title=title, type=type, author=author, place=place,                             brand=brand, model=model, scene=scene, description=description)

info.save()

#return HttpResponse('Add title %s success' % title)

return HttpResponseRedirect('/danfan/list')

else:

return HttpResponse('Not support method %s' % request.method)

#list列表

def list(request):

#article = Article.objects.get(id=id)

#self_infos = Info.objects.filter(Info=id).order_by("-id").all()

self_infos = Info.objects.order_by("-id").all()

return render_to_response('index.html', {'self_infos': self_infos})

#注销

def logout(request):

#response = HttpResponse('logout')

#response.delete_cookie('username')

#return response

return HttpResponseRedirect('/danfan/login')

8、配置url路径

项目sheying 下urls.py

from django.conf.urls import patterns, include, url

from django.contrib import admin

admin.autodiscover()

urlpatterns = patterns('',

url(r'^admin/', include(admin.site.urls)),

url(r'^danfan/', include('danfan.urls')),    //将danfan目录路径include进来

)

应用danfan下的urls.py

from django.conf.urls import patterns, url

from danfan import views

urlpatterns = patterns('',

url(r'^$',views.register, name='register'),

url(r'^register/$',views.register,name = 'register'),

url(r'^login/$',views.login,name = 'login'),

url(r'^add/$',views.add,name = 'add'),

url(r'^list/$',views.list,name='list'),

url(r'^logout/$',views.logout,name = 'logout'),

register:注册页面url

login:  登录页面url

add:     添加页面url

list:   列表页面url

logout: 注销页面url

9、创建html模板(存放在templeates目录下)

register.html 注册页面

小蜜蜂

body{background:#efd;padding:100 5em;margin:10;

background-p_w_picpath="/usr/local/python27/cenxi/hunlianwang/templeates/p_w_picpaths/index.jpg";

background-size:cover;

}

h1{padding:2em 0.5em;background:#687}

h2{color:#bf8;border-top:1px dotted #fff;margin-top:2em}

p{margin:1em 0}

首页

相约小蜜蜂

美图分享

摄影之旅

拍摄技巧

关于我们

{% csrf_token %}

`uf`.`as_p`

登录

login.html 登录页面

(这里只简写body部分,head title等直接继承beas.html即可)

{% csrf_token %}

`uf`.`as_p`

添加图片信息页面add.html

(这里只简写body部分,head title等直接继承beas.html即可)

分享我的照片

{% csrf_token %}

标题:

类型:

风景

人像

写真

摄影:

场地:

品牌:

Canon

Nikon

Sony

型号:

镜头:

描述:

列表页面 list.html

(这里只简写body部分,head title等直接继承beas.html即可)

我的图片分享

标题
类型
摄影
场地
品牌
型号
镜头
描述
操作

{% for info in self_infos %}

`info`.`title`
`info`.`type`
`info`.`author`
`info`.`place`
`info`.`brand`
`info`.`model`
`info`.`scene`
`info`.`description`
详情
删除

{% endfor%}

退出

访问测试

不填写用户信息会有一个判断

注册一个小茗同学 的账户

注册成功,添加分享的图片信息

保存信息内容

先写到这里了,还有图片上传、详情、评论、删除等功能需要优化。

春节放假咯,回去继续啃书.......在技术控的路上渐行渐远.大家新年快乐!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值