Skip to content

Commit

Permalink
Add user login and logout
Browse files Browse the repository at this point in the history
  • Loading branch information
HYXC committed Sep 12, 2017
1 parent 14c1bf7 commit 8b8efca
Show file tree
Hide file tree
Showing 11 changed files with 221 additions and 4 deletions.
5 changes: 5 additions & 0 deletions accounts/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,10 @@
from __future__ import unicode_literals

from django.contrib import admin
from .models import User

# Register your models here.
class UserAdmin(admin.ModelAdmin):
list_display = ('username','password','phone','mail','department','job')

admin.site.register(User,UserAdmin)
16 changes: 16 additions & 0 deletions accounts/decorators.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# -*- coding: utf-8 -*-
from __future__ import unicode_literals

from django.http import HttpResponseRedirect
import functools

# Create your decorators here.
def login_required(func):
@functools.wraps(func)
def wrapper(request,*args, **kw):
user = request.session.get('username')
if user is not None:
return func(request,*args, **kw)
else:
return HttpResponseRedirect('/accounts/login/')
return wrapper
9 changes: 9 additions & 0 deletions accounts/forms.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# -*- coding: utf-8 -*-
from __future__ import unicode_literals

from django import forms

# Create your forms here.
class UserForm(forms.Form):
username = forms.CharField(max_length=30)
password = forms.CharField(widget=forms.PasswordInput())
4 changes: 2 additions & 2 deletions accounts/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,14 @@
# Create your models here.
class User(models.Model):
username = models.CharField(max_length=30, primary_key=True)
password = models.CharField(max_length=60)
password = models.CharField(max_length=100)
phone = models.CharField(max_length=20,default=None)
mail = models.EmailField(max_length=50)
department = models.CharField(max_length=80)
job = models.CharField(max_length=50)

def save(self,*args,**kwargs):
self.password = hashlib.sha1(self.password+self.username+'ylhb').hexdigest()
self.password = hashlib.sha256(self.password+self.username+'ylhb').hexdigest()
super(User,self).save(*args,**kwargs)

def __unicode__(self):
Expand Down
7 changes: 7 additions & 0 deletions accounts/urls.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
from django.conf.urls import url
from . import views

urlpatterns = [
url(r'^login/$', views.login, name='login'),
url(r'^logout/$', views.logout, name='logout'),
]
30 changes: 30 additions & 0 deletions accounts/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,35 @@
from __future__ import unicode_literals

from django.shortcuts import render
from django.http import HttpResponseRedirect
from .forms import UserForm
from .models import User
from .decorators import login_required
import hashlib

# Create your views here.
def login(request):
if request.method == 'POST':
uspa = UserForm(request.POST)
if uspa.is_valid():
username = uspa.cleaned_data['username']
password = uspa.cleaned_data['password']
passwdhash = hashlib.sha256(password+username+'ylhb').hexdigest()
user = User.objects.filter(username__exact = username,password__exact = passwdhash)
if user:
request.session['username'] = username
return render(request, 'accounts/dashboard.html')
else:
return render(request, 'accounts/login.html')
else:
return render(request, 'accounts/login.html')
else:
uspa = UserForm()
return render(request, 'accounts/login.html')

def logout(request):
try:
del request.session['username']
except KeyError:
pass
return HttpResponseRedirect('/accounts/login/')
2 changes: 1 addition & 1 deletion blackjack/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [],
'DIRS': [os.path.join(BASE_DIR, 'templates')],
'APP_DIRS': True,
'OPTIONS': {
'context_processors': [
Expand Down
4 changes: 3 additions & 1 deletion blackjack/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,11 @@
1. Import the include() function: from django.conf.urls import url, include
2. Add a URL to urlpatterns: url(r'^blog/', include('blog.urls'))
"""
from django.conf.urls import url
from django.conf.urls import include, url
from django.contrib import admin
import accounts

urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^accounts/', include('accounts.urls')),
]
42 changes: 42 additions & 0 deletions templates/accounts/dashboard.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
{% extends 'base.html' %}
{% block title %}DashBoard{% endblock %}

{% block content %}
<nav class="navbar navbar-inverse navbar-static-top" role="navigation" style="margin-bottom: 0">
<div class="navbar-header">
<button type="button" class="navbar-toggle" data-toggle="collapse" data-target=".navbar-collapse">
<span class="sr-only">Toggle navigation</span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
<a class="navbar-brand" href="#">Blackjack</a>
</div>
<!-- /.navbar-header -->

<ul class="nav navbar-top-links navbar-right">

<!-- /.dropdown -->
<li class="dropdown">
<label style="color: #C2C2C2">欢迎你,{{ request.session.username }}!</label>
</li>
<li class="dropdown">
<a class="dropdown-toggle" data-toggle="dropdown" href="#">
<i class="fa fa-user fa-fw"></i> <i class="fa fa-caret-down"></i>
</a>
<ul class="dropdown-menu dropdown-user">
<li><a href="#"><i class="fa fa-user fa-fw"></i> User Profile</a>
</li>
<li><a href="#"><i class="fa fa-gear fa-fw"></i> Settings</a>
</li>
<li class="divider"></li>
<li><a href="{% url 'logout' %}"><i class="fa fa-sign-out fa-fw"></i> Logout</a>
</li>
</ul>
<!-- /.dropdown-user -->
</li>
<!-- /.dropdown -->
</ul>
<!-- /.navbar-top-links -->
</nav>
{% endblock %}
36 changes: 36 additions & 0 deletions templates/accounts/login.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
{% extends 'base.html' %}
{% block title %}BlackJack 平台{% endblock %}

{% block content %}
<div class="container">
<div class="row">
<div class="col-md-4 col-md-offset-4">
<div class="login-panel panel panel-default">
<div class="panel-heading">
<h3 class="panel-title">BlackJack用户登录</h3>
</div>
<div class="panel-body">
<form method='post' role="form">
<fieldset>
{% csrf_token %}
<div class="form-group">
<input class="form-control" name="username" type="text" autofocus>
</div>
<div class="form-group">
<input class="form-control" placeholder="Password" name="password" type="password" value="">
</div>
<div class="checkbox">
<label>
<input name="remember" type="checkbox" value="Remember Me">Remember Me
</label>
</div>
<!-- Change this to a button or input when using this as a form -->
<button type="submit" class="btn btn-lg btn-success btn-block">登录</button>
</fieldset>
</form>
</div>
</div>
</div>
</div>
</div>
{% endblock %}
70 changes: 70 additions & 0 deletions templates/base.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
<!DOCTYPE html>
<html lang="en">

<head>

<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta name="description" content="">
<meta name="author" content="">

<title>{% block title %}blackjack{% endblock %}</title>
{% load staticfiles %}

<!-- Bootstrap Core CSS -->
<link href="{% static "vendor/bootstrap/css/bootstrap.min.css" %}" rel="stylesheet">

<!-- MetisMenu CSS -->
<link href="{% static "vendor/metisMenu/metisMenu.min.css" %}" rel="stylesheet">

<!-- Custom CSS -->
<link href="{% static "dist/css/sb-admin-2.css" %}" rel="stylesheet">

<!-- Morris Charts CSS -->
<link href="{% static "vendor/morrisjs/morris.css" %}" rel="stylesheet">

<!-- Custom Fonts -->
<link href="{% static "vendor/font-awesome/css/font-awesome.min.css" %}" rel="stylesheet" type="text/css">

<!-- HTML5 Shim and Respond.js IE8 support of HTML5 elements and media queries -->
<!-- WARNING: Respond.js doesn't work if you view the page via file:// -->
<!--[if lt IE 9]>
<script src="https://oss.maxcdn.com/libs/html5shiv/3.7.0/html5shiv.js"></script>
<script src="https://oss.maxcdn.com/libs/respond.js/1.4.2/respond.min.js"></script>
<![endif]-->
{% block style %}
{% endblock %}

</head>

<body>

<div id="wrapper">
{% block content %}
{% endblock %}
</div>
<!-- /#wrapper -->

<!-- jQuery -->
<script src="{% static "vendor/jquery/jquery.min.js" %}"></script>

<!-- Bootstrap Core JavaScript -->
<script src="{% static "vendor/bootstrap/js/bootstrap.min.js" %}"></script>

<!-- Metis Menu Plugin JavaScript -->
<script src="{% static "vendor/metisMenu/metisMenu.min.js" %}"></script>

<!-- Morris Charts JavaScript -->
<script src="{% static "vendor/raphael/raphael.min.js" %}"></script>
<script src="{% static "vendor/morrisjs/morris.min.js" %}"></script>
<script src="{% static "data/morris-data.js" %}"></script>

<!-- Custom Theme JavaScript -->
<script src="{% static "dist/js/sb-admin-2.js" %}"></script>
{% block jquery %}
{% endblock %}

</body>

</html>

0 comments on commit 8b8efca

Please sign in to comment.