当前位置:首页 > 开发 > 编程语言 > 编程 > 正文

Django自带的用户验证

发表于: 2014-01-23   作者:cywhoyi   来源:转载   浏览:
摘要: 用户自带验证的功能,其实挺不错的,首先在settings.py里面加入其应用 TEMPLATE_CONTEXT_PROCESSORS = ( "django.contrib.auth.context_processors.auth", "django.core.context_processors.debug", "

用户自带验证的功能,其实挺不错的,首先在settings.py里面加入其应用

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",
)

 

在homepage.py

from django import forms
from django.shortcuts import render
from django.http import HttpResponse, HttpResponseRedirect
from django.contrib.auth.models import User
from django.contrib.auth import authenticate, login as user_login, logout as user_logout
from django.template import loader, Context, Template
from django.shortcuts import render_to_response
import simplejson as json

def homepage(req):
    user = authenticate(username='linkedin', password='i123312')
    if user is not None:
            if user.is_active:
                print 'user is active'
            else:
                print 'user is not active'
    else:
        print 'user is not exit'
    t = loader.get_template('home.html')
    c = Context({})

    return HttpResponse(t.render(c))

 新增一用户:

newUser=User.objects.create_user(username='linkedin',email='cywhoyi@gmail.com',password='i123312')
newUser.save()

 

Django自带的用户验证

  • 0

    开心

    开心

  • 0

    板砖

    板砖

  • 0

    感动

    感动

  • 0

    有用

    有用

  • 0

    疑问

    疑问

  • 0

    难过

    难过

  • 0

    无聊

    无聊

  • 0

    震惊

    震惊

版权所有 IT知识库 CopyRight © 2009-2015 IT知识库 IT610.com , All Rights Reserved. 京ICP备09083238号