Django registration login verification code summary

The reference for the registration and login verification code is:

    http://django-simple-captcha.readthedocs.io/en/latest/usage.html#adding-to-a-form

The following are the operations of the first few parts:

Using django-simple-captcha

Installation

  1. Install django-simple-captcha via pippip install django-simple-captcha

  2. Add captcha to the INSTALLED_APPS in your settings.py

  3. Run python manage.py migrate

  4. Add an entry to your urls.py:

    urlpatterns + = [
        url(r'^captcha/', include('captcha.urls')),
    ]
    

Note: PIL and Pillow require that image libraries are installed on your system. On e.g. Debian or Ubuntu, you'd need these packages to compile and install Pillow:

apt-get -y install libz-dev libjpeg-dev libfreetype6-dev python-dev

Define to give the form form:

#Registration interface, captcha verification code
class Register_Form(forms.Form):
    email = forms.EmailField(required=True)
    password = forms.CharField(max_length=10,min_length=3)
    captcha = CaptchaField(error_messages={"invalid":u"Captcha error"})

html front-end page:

        <form class="form-signin" role="form" method="POST" action="{% url 'registViews' %}">
            {% csrf_token %}
            <div>
                邮  箱:<input type="email" name="email" id="username"/>
            </div>
            <br>
            <div>
                密   码:<input type="password" name="password" id="pwd"/>
            </div>

            <div >
{#                <input type="text" name="captcha" id="captcha"/>#}
                Captcha: {{ register_form.captcha }}
            </div>
            <div>
                <input type="submit" value="Sign up and log in"/>
                <input type="reset" name="reset" value="重置"/>
                <div id="result"></div>
            </div>

        </form>


views.py

#register
def registViews(request):
    if request.method =="GET":
        register_form = Register_Form()
        return render(request,'rc_test/register.html',context= {'register_form':register_form })
    else:
        reg = Register_Form(request.POST)
        print reg
        if reg.is_valid():
            email = reg.cleaned_data['email']
            password = reg.cleaned_data['password']
            print email
            print password

            if UserInfo.objects.filter(email=email):
                return render(request,'rc_test/register.html',{'register_form':reg,
                                                               'msg': 'User already exists'
                                                               })

            #If there is no registration, save it to the database, but it has not been activated, send an email to wait for verification
            user_proinfo = UserInfo()
            user_proinfo.username = email
            user_proinfo.email = email
            user_proinfo.is_active = False
            user_proinfo.password = make_password(password) #make_password() is the encryption method of hash
            user_proinfo.save()
            #send email
            # send_register_email(email,'register')

            return HttpResponseRedirect(reverse('loginViews'))

        # return render(request,'rc_test/register.html')
        return HttpResponseRedirect(reverse('registViews'))

Page display:

Define a userinfo table to store registration information

#User information table
class UserInfo(models.Model):
    username = models.CharField(max_length=50)
    password = models.CharField(max_length=100)
    email = models.EmailField()



Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325682130&siteId=291194637