Django使用用户系统
来源:互联网 发布:java 开源界面框架 编辑:程序博客网 时间:2024/05/11 04:32
最近在用django做一个小网站,在做用户登录的这个模块,一开始想自己实现的,后面发现真的很复杂。
就拿了django自带的用户系统来使用了。在这中间出了很多的问题,现在记录下
网站的基础模板包含一个导航条,和大部分网站一样,就是没有登录显示登录。登录后就显示用户名
在前端页面使用了的话,可以拿user对象来使用
<div class="userinfo">{% if user.is_authenticated %}欢迎您:{{user.username}}{% else %}<a href="/account/login">登录</a><a href="/account/register">注册</a>{% endif %}<a href="/account/logout">注销</a></div>
要在页面引入这个user对象的话,就必须在django的settings里面加入一下的代码(默认是引入好的):
TEMPLATE_CONTEXT_PROCESSORS = ( "django.contrib.auth.context_processors.auth", "django.core.context_processors.debug", "django.core.context_processors.i18n", "django.core.context_processors.media", "django.core.context_processors.request",)
其中 django.contrib.auth.context_processors.auth就包含user对象
其实就是一些上下文处理器,这些处理器都返回一个字典。例如。我们看auth的代码。
def auth(request): def get_user(): if hasattr(request, 'user'): return request.user else: from django.contrib.auth.models import AnonymousUser return AnonymousUser() return { 'user': SimpleLazyObject(get_user), 'messages': messages.get_messages(request), 'perms': lazy(lambda: PermWrapper(get_user()), PermWrapper)(), }
可以看到user对象就是这样被返回回来供我们使用的,取得user对象是调用一个方法从request里面取。如果没有取到的话就是匿名用户了。这样在页面的验证就会被判断会匿名
一个登录和登出的代码
# -*- coding:utf-8 -*-# Create your views here.from django.shortcuts import render_to_response,render,get_object_or_404from CodeShare.account.forms import UserRegisterFormfrom django.http import HttpResponse, HttpResponseRedirectfrom django.contrib.auth.models import Userfrom django.contrib.auth import authenticate, login as user_login, logout as user_logoutdef index(request): #用户的个人页面 return render(request,'account/index.html')def register(request): #注册提交 if request.method == 'POST': form = UserRegisterForm(data=request.POST) if form.is_valid(): new_user = User.objects.create_user(request.POST['username'], request.POST['email'], request.POST['password']) new_user.save() return render(request,'account/index.html') else: return render(request,'account/register.html', {'form':form}) #超链接点击过来的注册 else: return render(request,'account/register.html')# return render_to_response('user/register.html', context_instance=RequestContext(request)) def login(request): #表单提交过来的数据 if request.user.is_authenticated(): return HttpResponse('你已经登录') if request.method == 'POST': username = request.POST['username'] password = request.POST['password'] user = authenticate(username=username, password=password) if user is not None: if user.is_active: user_login(request, user) return HttpResponseRedirect('/account/%d' % user.id) else: return HttpResponse('用户没有启用!') else: return HttpResponse('用户名或者密码错误!') else: return render_to_response('account/login.html') def logout(request): user_logout(request) return render_to_response('index.html')
这里的登录和登出都是使用自带的方法,只是import进来避免同名取了个别名
当用户验证通过后
使用user_login(request, user) 就会把用户存到session里面。
而删除也就是从session中销毁
这里有一个很重要的问题
如果要在页面使用user,那么页面必须要有request对象(为什么?因为user就是从request里面取的)
所以在渲染页面的时候。如果要使用这个验证,就必须要把request对象也渲染进去
我之前渲染页面就是用return render_to_response('index.html')
这样的话,只是单纯渲染页面。是不会把request传进去的。必须显式的指定
加入context_instanse=RequestContext(request)才有办法在页面使用
但是每次render都这样写是不是很烦?
django1.3新增了一个shortcut
render
其实就是一默认使用RequestContext的render_to_response
这个方法除了有一个必须的第一个参数 request
外,和render_to_response都一样
使用传统的页面重定向,是默认会带入request的,如使用HttpResponseRedirect
ps:记一个pydev的问题
在pydev里面写django代码。如果想引入django的settings文件
要设置好django项目的2个变量。不然会import出错 提示找不到
(其实默认是设置好的)
右键项目 找到PYTHON-PYTHONPATH
在里面的String subsitution Variables里面
必须有2个变量
DJANGO_MENAGE_LOCATION 你项目的manage,py的地址
DJANGO_SETTINGS_MODULE settings文件的路径(注意这里的路径是python的路径。也就是包名形式的,如果是路径形式的话会出错)
原文网址:http://qiaoqinqie.iteye.com/blog/1038332
- Django使用用户系统
- 使用django-userena搭建用户登录系统
- Django使用自己的用户系统
- Django使用自己的用户系统,自定义用户系统
- django 用户注册系统
- Django用户认证系统
- django 用户认证系统
- Django实践:自定义用户系统
- Django用户认证系统 authentication system
- Django自定义用户认证系统Customizing authentication
- django 用户认证系统 相关资料
- Django 用户系统 注册 校验 登陆
- Django 介绍、安装配置、基本使用、Django 用户注册例子
- 使用django的用户帐号登录 openfire
- 使用django的用户帐号登录openfire
- [Django模板系统]如何使用Django模板系统
- Django用户认证系统(三)组与权限
- Redis+Django(Session,Cookie、Cache)的用户系统
- php小结
- 数学符号读法大全
- linux 原子操作
- vim指令大全
- Linux各个驱动在内核源码包位置
- Django使用用户系统
- vb6.0 中 事务处理的一个问题
- 关于TreeCtrl SetItemData 释放问题
- x$表2
- Lua的协同程序
- Wireless tools for Linux 的一些资料收集
- Android中处理崩溃异常
- 中小企业存在的具体问题
- JavaScriptSerializer引用