首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > 编程 >

Django运用用户系统

2012-12-22 
Django使用用户系统最近在用django做一个小网站,在做用户登录的这个模块,一开始想自己实现的,后面发现真的

Django使用用户系统

最近在用django做一个小网站,在做用户登录的这个模块,一开始想自己实现的,后面发现真的很复杂。

就拿了django自带的用户系统来使用了。在这中间出了很多的问题,现在记录下

网站的基础模板包含一个导航条,和大部分网站一样,就是没有登录显示登录。登录后就显示用户名

在前端页面使用了的话,可以拿user对象来使用

?

# -*- 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的路径。也就是包名形式的,如果是路径形式的话会出错)

?

?

?

热点排行