From 8b8efca2d3963cc2cdaa3e776340a26961397401 Mon Sep 17 00:00:00 2001 From: HYXC <2393470186@qq.com> Date: Tue, 12 Sep 2017 15:23:01 +0800 Subject: [PATCH] Add user login and logout --- accounts/admin.py | 5 +++ accounts/decorators.py | 16 +++++++ accounts/forms.py | 9 ++++ accounts/models.py | 4 +- accounts/urls.py | 7 ++++ accounts/views.py | 30 +++++++++++++ blackjack/settings.py | 2 +- blackjack/urls.py | 4 +- templates/accounts/dashboard.html | 42 +++++++++++++++++++ templates/accounts/login.html | 36 ++++++++++++++++ templates/base.html | 70 +++++++++++++++++++++++++++++++ 11 files changed, 221 insertions(+), 4 deletions(-) create mode 100644 accounts/decorators.py create mode 100644 accounts/forms.py create mode 100644 accounts/urls.py create mode 100644 templates/accounts/dashboard.html create mode 100644 templates/accounts/login.html create mode 100644 templates/base.html diff --git a/accounts/admin.py b/accounts/admin.py index 13be29d..3e9f2df 100644 --- a/accounts/admin.py +++ b/accounts/admin.py @@ -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) \ No newline at end of file diff --git a/accounts/decorators.py b/accounts/decorators.py new file mode 100644 index 0000000..d75a63c --- /dev/null +++ b/accounts/decorators.py @@ -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 \ No newline at end of file diff --git a/accounts/forms.py b/accounts/forms.py new file mode 100644 index 0000000..88cbb31 --- /dev/null +++ b/accounts/forms.py @@ -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()) diff --git a/accounts/models.py b/accounts/models.py index 17067fd..dd21bb3 100644 --- a/accounts/models.py +++ b/accounts/models.py @@ -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): diff --git a/accounts/urls.py b/accounts/urls.py new file mode 100644 index 0000000..2da1189 --- /dev/null +++ b/accounts/urls.py @@ -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'), +] diff --git a/accounts/views.py b/accounts/views.py index e784a0b..85b635c 100644 --- a/accounts/views.py +++ b/accounts/views.py @@ -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/') \ No newline at end of file diff --git a/blackjack/settings.py b/blackjack/settings.py index 710bf1a..0457137 100644 --- a/blackjack/settings.py +++ b/blackjack/settings.py @@ -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': [ diff --git a/blackjack/urls.py b/blackjack/urls.py index a4b7713..efc47b9 100644 --- a/blackjack/urls.py +++ b/blackjack/urls.py @@ -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')), ] diff --git a/templates/accounts/dashboard.html b/templates/accounts/dashboard.html new file mode 100644 index 0000000..8661c47 --- /dev/null +++ b/templates/accounts/dashboard.html @@ -0,0 +1,42 @@ +{% extends 'base.html' %} +{% block title %}DashBoard{% endblock %} + +{% block content %} + +{% endblock %} diff --git a/templates/accounts/login.html b/templates/accounts/login.html new file mode 100644 index 0000000..fbafdd4 --- /dev/null +++ b/templates/accounts/login.html @@ -0,0 +1,36 @@ +{% extends 'base.html' %} +{% block title %}BlackJack 平台{% endblock %} + +{% block content %} +
+
+
+ +
+
+
+{% endblock %} diff --git a/templates/base.html b/templates/base.html new file mode 100644 index 0000000..c12c7af --- /dev/null +++ b/templates/base.html @@ -0,0 +1,70 @@ + + + + + + + + + + + + {% block title %}blackjack{% endblock %} + {% load staticfiles %} + + + + + + + + + + + + + + + + + + + + {% block style %} + {% endblock %} + + + + + +
+ {% block content %} + {% endblock %} +
+ + + + + + + + + + + + + + + + + + + {% block jquery %} + {% endblock %} + + + +