SoFunction
Updated on 2024-11-18

Django Forms in the use of code analysis

This article examines the use of Forms in Django, as follows.

Creating Documents

{% extends '' %} 
 
{% block mainbody %} 
   
  <form action="/app1/do/" method="post"> 
    {% csrf_token %} 
    {{ form.as_p }} 
    <input type="submit" value="Submit"> 
  </form> 
 
{% for item in student %} 
  <p>{{  }}, {{ item }}</p> 
{% endfor %} 
 
{% endblock %} 

Add the path in:

url(r'^do/', ''),

Define the do function in

from  import render 
from .context_processors import csrf 
from  import Student 
from django import forms 
 
def do(request): 
  context = {} 
  (csrf(request)) 
  if : 
    form = StudnetForm() 
    if form.is_valid(): 
      name = form.cleaned_data['name'] 
      student = Student(name = name) 
      () 
  form = StudnetForm() 
  context = {} 
  (csrf(request)) 
  student_list = () 
  context['student'] = student_list 
  context['form'] = form  
  return render(request, '', context) 

There are now four pieces of data inside the database:

Visit http://localhost:8000/app1/do/

can be seen

Then add Name.

You can see the data added to the database:

You can define multiple Form just need to view and template in the corresponding can be specific Form of the use of Form API can be viewed , the official Web site:/en/1.8/ref/forms/api/

summarize

Overall, Django is a very good framework, easy to use, many companies recruiting Python also require some knowledge of Django.

Above is this article on the use of Forms in Django code analysis of all content, I hope to help you. Interested friends can continue to refer to other related topics on this site, if there are inadequacies, welcome to leave a message to point out. Thank you for the support of friends on this site!