django using the form caused by errors

A. In general website development process will be used in a form, and will be used in the template file django's <form> tag, I do now is a blog system, has now done the article shows the interface. as the picture shows.

   

II. Now we have to do is log in a user interface, we use the built-in django User class to create a user object code below, our users only defines the user name and password.

from django import forms
from django.contrib.auth.models import User
class UserLoginForm(forms.Form):
    username=forms.CharField()
    password=forms.CharField()

III. The next step is our view function, the following code, the code still believe we can understand

Import UserLoginForm .forms from 
# the Create your views here Wallpaper. 
DEF USER_LOGIN (Request): 
    IF request.method == 'the POST': # request method post 
        user_login_form = UserLoginForm (= Data of request.POST) # post request data to fill the form 
        if user_login_form.is_valid (): # pole data valid 
            username of request.POST = [ 'username'] 
            password of request.POST = [ 'password'] # acquired user name and password 
            user = authenticate (username = username, password = password) # query the existence of this user, users object is returned 
            if user: # presence user 
                login (request, user) # user logs in and keep the session 
                return redirect (Reverse ( 'articles: article this article was')) # interface redirected to the article shows 
            else:  
                return HttpResponse ( 'account or password is incorrect, please re-fill') # user does not exist
        else:
            return HttpResponse ( 'Form format fill in error, please re-fill') # Form malformed 
    elif request.method == 'GET': # request method is GET 
        user_login_form = UserLoginForm () # Create an empty form for the user to enter 
        context = { 'form': user_login_form} 
        return the render (request, 'USERPROFILE / the login.html', context) 
    the else: 
        return the HttpResponse ( 'post or use the get request to obtain data')

 IV. The next step is to prepare our template file, and I also wrong in this place, but also access to a long time to understand the principles involved, the code looks nothing special rare place, but not necessarily every person can learn django finish complete all understand this code. First we look at <form method = "post" action = "{% url 'login: userlogin'%}">, first I do this blog system is followed by a tutorial online to the <form method = "post" action = " . ">, then the next we enter the login screen when url in the address bar - http: what the outcome of it (see Figure) 8000 / login / login and fill in the correct user name and look after the data: // localhost not to directly eject a 404 error, we change the code <form method = "post" action = "{% url 'articles: article'%}"> ({% url 'articles: article'%} representatives url article is to show the interface) that the outcome of it (see Figure)

{% extends 'articles/base.html' %}{% load staticfiles %}
{% block title %}登录{% endblock title %}
{% block content %}
<div class="container">
    <div class="row">
        <div class="col-12">
            <br>
            <form method="post" action="{% url 'login:userlogin' %}">
                {% csrf_token %}
                {{form.as_p}}
                <button type="submit" class="btn btn-primary">提交</button>
                <input type="hidden" name="next" value="{% url 'articles:article' %}"/>
            </form>
        </div>
    </div>
</div>
{% endblock content %}

  I see here seem to think has been successful, but I want to tell you that in fact there did not succeed. I am also a multi-party access to information to know, no way to front-end knowledge is not very understanding. Here I want to tell you that when the button type = 'submit' time, when you fill out the form browser will automatically jump to the address on behalf of the action, that is to say form data will be submitted to the address of the action within the meaning of, and the browser will also jump to that address, is to say when your action = {% url 'articles: article'%} when we view functions written in handling post request that part did not use, because we there is no way to access a post login screen.

So here the situation seems to be clear, when we enter in the address bar http: // localhost: When 8000 / login / login, then we get method is based on the page request, then we will get an empty form for us to fill in If our action = {% url 'login: userlogin'%}, then that data will be submitted after we finished filling the form to the login screen, but this time we are post method to access the login screen, why, because <form method = '' post ">, then the time will leave part of our view of the function of the post processing request, this time through the return redirect (reverse ( 'articles: article')) to redirect the article shows that the interface considered success. is there any way to achieve not jump after completion of the form, the answer is definitely yes, we just button type = 'submit' into buttion type = 'button' can be, we can try their own hands.

Guess you like

Origin www.cnblogs.com/xuzhiyi/p/11606083.html