This commit is contained in:
Bob 2019-10-18 16:01:56 +08:00
commit 37619a0c61
6 changed files with 92 additions and 10 deletions

View File

@ -23,6 +23,7 @@
<div class="panel-body"> <div class="panel-body">
<form method="post" action="{% url 'login' %}"> <form method="post" action="{% url 'login' %}">
{% csrf_token %} {% csrf_token %}
<input type="hidden" name="next" value="{{ next }}">
<div class="form-group"> <div class="form-group">
<label for="username">用户名</label> <label for="username">用户名</label>
<input type="text" class="form-control" id="account" placeholder="请输入用户名" name="username"> <input type="text" class="form-control" id="account" placeholder="请输入用户名" name="username">
@ -32,6 +33,18 @@
<input type="password" class="form-control" id="password" placeholder="请输入密码" <input type="password" class="form-control" id="password" placeholder="请输入密码"
name="password"> name="password">
</div> </div>
<div class="form-group" style="position: relative">
<label for="password">验证码</label>
<input class="form-control" type="text" id="id_reg_captcha_1" name="captcha_1"
placeholder="验证码" style="width: 100px;">
<div style="position: absolute; right: 120px;top:24px;">
<a href="#" class="next-captcha">
<img src="{{ image_url }}" class="captcha" alt="captcha">
</a>
</div>
<input id="id_reg_captcha_0" name="captcha_0" type="hidden" value="{{ hash_key }}">
</div>
<button type="submit" class="btn btn-lg btn-primary btn-block">登录</button> <button type="submit" class="btn btn-lg btn-primary btn-block">登录</button>
</form> </form>
@ -40,3 +53,25 @@
</div> </div>
</div> </div>
{% endblock %} {% endblock %}
{% block add_script %}
<script type="text/javascript">
$(function () {
$(".next-captcha").click(function (e) {
e.preventDefault();
$.getJSON("{% url 'refresh-captcha' %}", function (json) {
// This should update your captcha image src and captcha hidden input
// debugger;
var status = json['status'];
var new_cptch_key = json['new_cptch_key'];
var new_cptch_image = json['new_cptch_image'];
id_captcha_0 = $("#id_reg_captcha_0");
img = $(".captcha");
id_captcha_0.attr("value", new_cptch_key);
img.attr("src", new_cptch_image);
});
});
})
</script>
{% endblock %}

View File

@ -3,6 +3,7 @@ from . import views
urlpatterns = [ urlpatterns = [
url(r'^login$', views.user_login, name='login'), url(r'^login$', views.user_login, name='login'),
url(r'^refresh_captcha/', views.refresh_captcha, name='refresh-captcha'),
url(r'^register$', views.user_register, name='register'), url(r'^register$', views.user_register, name='register'),
url(r'^logout', views.user_logout, name='logout'), url(r'^logout', views.user_logout, name='logout'),
url(r'^cart', views.cart, name='my_cart'), url(r'^cart', views.cart, name='my_cart'),

View File

@ -1,11 +1,17 @@
# coding=utf-8 # coding=utf-8
from django.contrib.admin.views.decorators import staff_member_required import datetime
from django.core.paginator import Paginator, PageNotAnInteger, EmptyPage
from django.shortcuts import render, HttpResponseRedirect, get_object_or_404, HttpResponse from captcha.helpers import captcha_image_url
from django.contrib.auth.models import User from captcha.models import CaptchaStore
from metadata.models import LineItem, Order, Favorite, Cart
from django.contrib import messages from django.contrib import messages
from django.contrib.admin.views.decorators import staff_member_required
from django.contrib.auth import authenticate, login, logout from django.contrib.auth import authenticate, login, logout
from django.contrib.auth.models import User
from django.core.paginator import Paginator, PageNotAnInteger, EmptyPage
from django.http import JsonResponse
from django.shortcuts import render, HttpResponseRedirect, get_object_or_404
from metadata.models import LineItem, Order, Favorite, Cart
# Create your views here. # Create your views here.
@ -14,7 +20,19 @@ from django.contrib.auth import authenticate, login, logout
def user_login(request): def user_login(request):
username = None username = None
password = None password = None
hash_key = CaptchaStore.generate_key()
image_url = captcha_image_url(hash_key)
n = request.GET.get('next', '')
if request.method == 'POST': if request.method == 'POST':
next = request.POST.get('next')
captcha_input = request.POST.get('captcha_1')
captcha_hashkey = request.POST.get('captcha_0')
try:
CaptchaStore.objects.get(response=captcha_input.lower(), hashkey=captcha_hashkey,
expiration__gt=datetime.datetime.now()).delete()
except CaptchaStore.DoesNotExist:
messages.error(request, '验证码错误')
return HttpResponseRedirect('/account/login')
if not request.POST.get('username'): if not request.POST.get('username'):
messages.error(request, u'请输入用户名') messages.error(request, u'请输入用户名')
else: else:
@ -35,12 +53,22 @@ def user_login(request):
cart.save() cart.save()
count = LineItem.objects.filter(cart=user.cart).count() count = LineItem.objects.filter(cart=user.cart).count()
request.session['count'] = count request.session['count'] = count
if next and len(next) > 0:
return HttpResponseRedirect(next)
return HttpResponseRedirect('/') return HttpResponseRedirect('/')
else: else:
messages.error(request, 'disabled username') messages.error(request, 'disabled username')
else: else:
messages.error(request, 'invalid user') messages.error(request, 'invalid user')
return render(request, 'account/login.html') return render(request, 'account/login.html', {"next": n, 'image_url': image_url, 'hash_key': hash_key})
def refresh_captcha(request):
to_json_response = dict()
to_json_response['status'] = 1
to_json_response['new_cptch_key'] = CaptchaStore.generate_key()
to_json_response['new_cptch_image'] = captcha_image_url(to_json_response['new_cptch_key'])
return JsonResponse(to_json_response)
def user_register(request): def user_register(request):

View File

@ -24,7 +24,7 @@ BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
SECRET_KEY = 'yufk*u+7u9l@3og_m_x1_=bexcxs+k41wz^-$g+6wdcp2f@i91' SECRET_KEY = 'yufk*u+7u9l@3og_m_x1_=bexcxs+k41wz^-$g+6wdcp2f@i91'
# SECURITY WARNING: don't run with debug turned on in production! # SECURITY WARNING: don't run with debug turned on in production!
DEBUG = True DEBUG = False
ALLOWED_HOSTS = ['221.207.32.75'] ALLOWED_HOSTS = ['221.207.32.75']
@ -48,7 +48,7 @@ INSTALLED_APPS = [
'graphics', 'graphics',
'results', 'results',
'account', 'account',
'captcha',
] ]
MIDDLEWARE = [ MIDDLEWARE = [
@ -175,6 +175,11 @@ MEDIA_ROOT = '/var/www/g214/shared/static/upload/'
MEDIA_URL = '/static/upload/' MEDIA_URL = '/static/upload/'
RECAPTCHA_PUBLIC_KEY = 'your-public-key'
RECAPTCHA_PRIVATE_KEY = 'your-private-key'
# If I'm not a Robot is required add:
NOCAPTCHA = False
try: try:
from local_settings import * from local_settings import *
except ImportError as e: except ImportError as e:

View File

@ -15,7 +15,9 @@ Including another URLconf
""" """
from django.conf.urls import url, include from django.conf.urls import url, include
from django.contrib import admin from django.contrib import admin
from account.views import user_login
admin.site.login = user_login
urlpatterns = [ urlpatterns = [
url(r'^\Z', include('portal.urls')), url(r'^\Z', include('portal.urls')),
@ -28,4 +30,5 @@ urlpatterns = [
url(r'^page/', include('page.urls')), url(r'^page/', include('page.urls')),
url(r'^results/', include('results.urls')), url(r'^results/', include('results.urls')),
url(r'^account/', include('account.urls')), url(r'^account/', include('account.urls')),
url(r'^captcha/', include('captcha.urls')),
] ]

View File

@ -0,0 +1,10 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<h1>Login Form</h1>
</body>
</html>