django 登陆

urlpatterns =( 
url(r'^admin/', admin.site.urls),
url(r'^blog/$',view.archive),
url(r'^articles/',include("news.urls")),
url(r'^upload/$',newview.upload,name='upload'),
url(r'^main/$', newview.main),
url(r'^$', newview.index),
url(r'^index/$', newview.index),
)




node2:/django/mysite/news#cat views.py
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
from django.shortcuts import render
from django.http import HttpResponse
# Create your views here.
from django.shortcuts import render,render_to_response,redirect
from .models import Article
from django.http import HttpResponse, HttpResponseNotFound
import datetime
from django.views.decorators.http import require_http_methods,require_GET
from django.http import HttpResponseRedirect
from django.shortcuts import render
import os
from django import forms
from django.forms import fields as Ffields
from django.forms import widgets as Fwidgets
from django.forms.extras.widgets import SelectDateWidget
from django.db import connection,transaction  
import os
os.environ['DJANGO_SETTINGS_MODULE'] = 'mysite.settings'
def check_user(username,password):
    cursor = connection.cursor()
    cursor.execute("SELECT count(*) FROM news_users WHERE username = %s and password=%s", [username,password])
    row = cursor.fetchone()
    return row[0]
class UserForm(forms.Form):
    #Username = forms.CharField(max_length=100)
    Username = forms.CharField()
    Password = forms.CharField()
    #comment = forms.CharField(widget=forms.Textarea)
def main(req):
          username = req.session.get('username',None) 
          print username
          try :
             req.session['username']
             return render_to_response('main.html')
          except:
             try:
                 a=req.POST['username']
                 if (check_user(req.POST['username'],req.POST['password'])== 1):
                    req.session['username']='aa'
                    return render_to_response('main.html')
                 else:
                  response = "用户名或密码错误"
                  return HttpResponse(response)
                  #return redirect('/index/')
             except:
               return redirect('/index/')
             
          
def index(req):
       try:
         req.session['username']
         return render_to_response('main.html')
       except:
         return render_to_response('index.html')



登陆页面:

node2:/django/mysite/news#cat templates/index.html
<html>
	<head>		
<title>Index</title>
		<link rel='stylesheet' type='text/css' href='/static/news/Css/Index/index.css'/>
	</head>
	<body>
		<h1>布丁运维管理平台</h1>
		<!--图片标签-->
		<img class="img_bk" src="/static/news/scan.jpg"/>
		<!--表单提交-->
		<form action="/main/" method="post" >
			<table cellspacing="0" cellpadding="0">
				<tr>
					<td class="td1">用户名:</td>
					<td><input type="text" name="username"/></td>
					<td class="td3"></td>
					<td class="td4"></td>
				<tr/>
				<tr>
					<td class="td1">密码:</td>
					<td><input type="password" name="password"/></td>
					<td class="td3"></td>
					<td class="td4"></td>
				<tr/>
				<!-- <tr> -->
					<!-- <td class="td1">验证码:</td> -->
					<!-- <td>   <input type='text' name='code' /></td> -->
					<!-- <td class="td3"><img src="__APP__/Public/code" οnclick='this.src=this.src+"?"+Math.random()'/></td> -->
					<!-- <td class="td4"></td> -->
				<!-- </tr> -->
				<tr>
					<td class="td1"></td>
					<td><input type="submit" value="" name="imgLogin" /></td>
					<td class="td3"></td>
					<td class="td4"></td>
				</tr>
			</table>
		</form>
	</body>
</html>

原文地址:https://www.cnblogs.com/hzcya1995/p/13349400.html