This commit is contained in:
baoliang 2019-08-12 13:52:43 +08:00
parent ad58e618a6
commit 1eacf31c56
6 changed files with 92 additions and 10 deletions

View File

@ -23,6 +23,7 @@
<div class="panel-body">
<form method="post" action="{% url 'login' %}">
{% csrf_token %}
<input type="hidden" name="next" value="{{ next }}">
<div class="form-group">
<label for="username">用户名</label>
<input type="text" class="form-control" id="account" placeholder="请输入用户名" name="username">
@ -32,6 +33,18 @@
<input type="password" class="form-control" id="password" placeholder="请输入密码"
name="password">
</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>
</form>
@ -40,3 +53,25 @@
</div>
</div>
{% 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 = [
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'^logout', views.user_logout, name='logout'),
url(r'^cart', views.cart, name='my_cart'),

View File

@ -1,11 +1,17 @@
# coding=utf-8
from django.contrib.admin.views.decorators import staff_member_required
from django.core.paginator import Paginator, PageNotAnInteger, EmptyPage
from django.shortcuts import render, HttpResponseRedirect, get_object_or_404, HttpResponse
from django.contrib.auth.models import User
from metadata.models import LineItem, Order, Favorite, Cart
import datetime
from captcha.helpers import captcha_image_url
from captcha.models import CaptchaStore
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.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.
@ -14,7 +20,19 @@ from django.contrib.auth import authenticate, login, logout
def user_login(request):
username = None
password = None
hash_key = CaptchaStore.generate_key()
image_url = captcha_image_url(hash_key)
n = request.GET.get('next', '')
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'):
messages.error(request, u'请输入用户名')
else:
@ -35,12 +53,22 @@ def user_login(request):
cart.save()
count = LineItem.objects.filter(cart=user.cart).count()
request.session['count'] = count
if next and len(next) > 0:
return HttpResponseRedirect(next)
return HttpResponseRedirect('/')
else:
messages.error(request, 'disabled username')
else:
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):

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'
# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = True
DEBUG = False
ALLOWED_HOSTS = ['221.207.32.75']
@ -48,7 +48,7 @@ INSTALLED_APPS = [
'graphics',
'results',
'account',
'captcha',
]
MIDDLEWARE = [
@ -175,6 +175,11 @@ MEDIA_ROOT = '/var/www/g214/shared/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:
from local_settings import *
except ImportError as e:

View File

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