Achieve Django project records (a) and dynamic user login verification code

Now ready to learn Python web of knowledge, and the development of a simplified version of the Mu class network, operation and maintenance to the development direction of learning

Be realized login feature

Django there are several methods frequently used
from django.contrib.auth import authenticate,login,logout
in which the parameters login function is the request with the User, login () parse the source code
logout () function only request parameter on the line, to exit log of
from django.urls import reversereverse first method is urls.py inside urlName, is a route f anti-analytic functions, you can find the corresponding URL by name.
logoutThe method used to exit, inside pass a request parameter on the line. authenticateDjango built-in method is used to complete the account password authentication of
request.user.is_authenticatedwhether the user has logged in for judge, when the user has logged in, is True, Django2 which is_authenticatedis a property, not a method, not with brackets

from django.shortcuts import render
from django.views.generic import View
# 这个authenticate方法是Django内置的用于完成账号密码认证的,要记住
# login方法是用来登录我们的用户
from django.contrib.auth import authenticate, login, logout
# reverse方法使我们只需要通过urlName就可以找到对应的页面
from django.urls import reverse
from django.http import HttpResponseRedirect


# Create your views here.

# 记得所以的class都要继承View
# 写一个退出登录的View


class LogoutView(View):
    def get(self, request, *args, **kwargs):
        logout(request)
        if request.user.is_authenticated:
            return HttpResponseRedirect(reverse('logout'))
        else:
            return HttpResponseRedirect(reverse('login'))


class LoginView(View):
    # 重载get与post方法
    def get(self, request, *args, **kwargs):
        # 判断用户是否已经登录,登录了就重定向到index页面
        # 注意这里属性是is_authenticated不要写错了,记得Django2中这是属性不是方法,所以不加括号
        if request.user.is_authenticated:
            return HttpResponseRedirect(reverse('index'))
        return render(request, 'login.html')

    def post(self, request, *args, **kwargs):
        user_name = request.POST.get("username", "")
        password = request.POST.get("password")
        user = authenticate(username=user_name, password=password)
        if user:
            # login函数用来登录用户记得加上request和user;两个参数
            login(request, user)
            return HttpResponseRedirect(reverse('index'))
        else:
            return render(request, "login.html", {"msg": "用户名或密码错误"})

Use image verification code

When using dynamic codes to sign usually have a lot of pages authentication code something, and today I also learned about the realization of functions in this area

There is an open source project captcha (verification code) GitHub above, we can directly configure it to come in,
Django-configure the Simple-captcha

  • Pip recommended installation, bug will be relatively small,
  • After installation, the captcha to INSTALL_APPS arranged inside, and then directly to migrate
  • Next, configure the url
from django.conf.urls import url,include
 url(r'^captcha/', include('captcha.urls')),
  • According to migrate after the expiry of its steps, we create a file forms.py
from django import forms
from captcha.fields import CaptchaField


class LoginForm(forms.Form):
    username = forms.CharField(required=True, min_length=2)
    password = forms.CharField(required=True, min_length=3)


class DynamicLoginForm(forms.Form):
    captcha = CaptchaField()
  • Next is the logical view to edit our first introduced from . import forms, followed by the inside view of a DynamicLoginForm example objects, and then use the value passed to render the front end
class LoginView(View):
    # 重载get与post方法
    def get(self, request, *args, **kwargs):
        # 判断用户是否已经登录,登录了就重定向到index页面
        # 注意这里属性是is_authenticated不要写错了,记得Django2中这是属性不是方法,所以不加括号
        if request.user.is_authenticated:
            return HttpResponseRedirect(reverse('index'))
        login_form = forms.DynamicLoginForm()
        return render(request, 'login.html', {
            "login_form": login_form
        })
  • Plus front-end form which {{ login_form.captcha }}can be, because Django inside to take the form of a column, or a field, in fact, it will give us generate HTML pages by default, you do not want it to generate the page, directly add value on the line

Success is shown below
Here Insert Picture Description

Published 62 original articles · won praise 38 · views 10000 +

Guess you like

Origin blog.csdn.net/happygjcd/article/details/104014860