SoFunction
Updated on 2024-11-16

Form forms and django form form additions

Button buttons in a form

<button>submit</button> :put in the form form, there will be a submit event, will submit the form data, the

<input type="button" value="submit">: is a simple button without any default events.

Match the url and the view function first

from  import url
from  import admin

from app01 import views

urlpatterns = [
  url(r'^admin/', ),
  url(r'^login/', ),
]

view function

def login(request):

  if  == "POST":
    return HttpResponse("OK!")



  return render(request,"")

Display on front-end pages

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>Title</title>
</head>
<body>

<form action="/login/" method="post">
{# csrf_token in the front-end will render an input tag, is a set of key-value pairs, the key is csrfmiddlewaretoken, the value is a random string, will be submitted along with the following input tag, only this form of sending a request for post can be received, #}

  {% csrf_token %}
  <p>user ID:<input type="text" name="user"></p>
  <p>cryptographic:<input type="password" name="pwd"></p>
  <input type="submit">

</form>
</body>
</html>

The ---- above is the normal native form form using HTML.

Now use the form form provided by djangoIn the views, you have to create a class that inherits from django's forms class.

A form object is then instantiated in the view function and displayed on the front-end page using the fields of the class.

as_p is to display all the fields in the backend, not good customization, can be customized, use objects, keep up with the fields

Create a key class in views.

from  import render,HttpResponse

# Create your views here.

# Using django's form class
from django import forms

class LoginForm():
  user = ()
  pwd = ()


def login(request):

  if  == "POST":
    return HttpResponse("OK!")

  form_obj = LoginForm() -------------------Instantiating Objects,
  return render(request,"",{"form_obj":form_obj}) ---Transmit to front-end display

Receiving on the front end, rendering

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>Title</title>

</head>
<body>

<form action="/login/" method="post">
{# csrf_token in the front-end will render an input tag, is a set of key-value pairs, the key is csrfmiddlewaretoken, the value is a random string, will be submitted along with the following input tag, only this form of sending a request for post can be received, #}

  {% csrf_token %}
  <p>user ID:{{ form_obj.user }}</p> ------------
  <p>name of an ancient state classifier for length or distance (yard), happenings etc:{{ form_obj.pwd }}</p> -------------------
  <input type="submit">

</form>

</body>

</html>

View the front-end elements that

--- The front-end can now enter data, but the back-end has to validate the submitted data.

Use the is_valid() method to get all the submitted data, cleaned_data, and if there are errors, use errors.

# Using django's form class
from django import forms

class LoginForm():
  user = (max_length=9,min_length=5,error_messages={"required":"Required"})
  pwd = ()


def login(request):

  if  == "POST":
    Request validation for #post
    formobj = LoginForm()# Get all the submissions
    if formobj.is_valid():#All meet the field requirements.

      # Take out all the data that meets the field requirements with cleaned_data
      print(formobj.cleaned_data)# Get the data in dictionary format

    else:
      print()# If you are submitting incorrect data, use errors



    return HttpResponse("OK!")

  form_obj = LoginForm()



  return render(request,"",{"form_obj":form_obj})

This is the whole content of this article.