Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1371137
  • 博文数量: 185
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 2664
  • 用 户 组: 普通用户
  • 注册时间: 2016-07-01 13:46
个人简介

鸟在笼中,恨关羽不能张飞;Survival of the fittest

文章分类

全部博文(185)

分类: Web开发

2015-07-27 16:19:09

简单的了解django原理后,我们就开始创建登录界面吧。

首先我们编辑一下项目中的account下的views.py,其内容如下

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

from django.shortcuts import render_to_response

from django.http import HttpResponseRedirect

from django.template import RequestContext

from django.contrib import auth

def index(request):

    return render_to_response('account/login.html')

def login(request):

    username = request.POST.get('username')

    password = request.POST.get('password')

    user = auth.authenticate(username=username,password=password)

    if request.POST:

        if user is not None and user.is_active:

            auth.login(request,user)

            return HttpResponseRedirect('/idc/dashboard/')

       else:

           return render_to_response('account/login.html',{'login_err': "Wrong username or password"}, context_instance=RequestContext(request))

    else:

        return render_to_response('account/login.html',context_instance=RequestContext(request))

def logout(request):

    if not request.user.is_authenticated():

        return render_to_response('account/login.html')

    user=request.user

    auth.logout(request)

    return render_to_response('account/login.html',{})

def register(request):

    errors= []

    account=None

    password=None

    password2=None

    email=None

    CompareFlag=False

    if request.method == 'POST':

        if not request.POST.get('account'):

            errors.append('Please Enter account')

        else:

            account = request.POST.get('account')

        if not request.POST.get('password'):

            errors.append('Please Enter password')

        else:

            password= request.POST.get('password')

        if not request.POST.get('password2'):

            errors.append('Please Enter password2')

        else:

            password2= request.POST.get('password2')

        if not request.POST.get('email'):

            errors.append('Please Enter email')

        else:

            email= request.POST.get('email')

        if password is not None and password2 is not None:

            if password == password2:

                CompareFlag = True

            else :

                errors.append('password2 is diff password ')

        if account is not None and password is not None and password2 is not None and email is not None and CompareFlag :

            user=User.objects.create_user(account,email,password)

            user.is_active=True

            user.save

            return HttpResponseRedirect('account/login')

    return render_to_response('account/register.html', {'errors': errors})

项目account下的urls.py内容如下

# -*- coding: UTF-8 -*-

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

urlpatterns = patterns('account.views',

    (r'^$','login'),

    (r'^index/$','login'),

    (r'^login/$','login'),

    (r'^logout/$','logout'),

    (r'^account/register/$','register'),

)

接下来我们再看看登录页的“面子”,如果你的英文并不是很好的话,你可以在bootstrap中文网来找找相关的内容。这里找到你想要的“面子”,个人觉得这个还可以,就使用了这个,比较简洁嘛。另外如果你想要更炫丽的可以学下bootstrap来修改。我这里先选择这个来做为我的登录界面前端。

你可以直接下载boostrap的源码包,当然这个包里就包括那些例子,你可以直接使用当中的Html文件。比如刚才那个登录界面的文件路径为/文件所有位置/bootstrap-3.3.5/docs/examples/signin/index.html.

首先你可以打开这个登录界面的index.html文件来查看它的源码。你可以看到源文件里主要css文件路径都为../../,也就是你下载源码包的根路径,所以各css文件的路径都要替换成你工程的路径。结果前面说过的django的settings.py配置文件,这里的css文件路径应该为/static/css/。至于其它什么的你可以根据需要改改,多改动下练习下可以,也没什么坏处。

更新这个登录界面源码后,把这个文件名改为login.html并放到工程下的templates/account/目前里即可。

把需要用的css文件放到工程下的static/css/目录即可,如下图。以后所需要的一些静态文件都是放到static目录下的,如js等。 


到这里我们就完成了account项目的创建、登录视图的编写、登录前端页面的显示、项目内部的url的设置,接下来我们完成最后一步工程总url的编写,就可以显示登录页面了,如下图。 

接下来回到工程的根目录下,启动自带的浏览器即可。如python manage.py runserver 0.0.0.0:8000。打开本地浏览器,输入127.0.0.1:8000即可显示登录界面了。 

关于注册的界面我这里没有去详细的找“面子”,你可以根据自己的喜好来选择。用户退出的views也给出了,只是暂时还没用到,等到编写dashboard时会用到。


阅读(8653) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~