--django--forms use of Python learning

Source: Internet
Author: User

1.

Create File do.html

{% extends ' base.html '%} {% block mainbody%}<form action= "/app1/do/" method= "POST" >{% csrf_token%}{{form.as_p}}<input type= "Submit" V  Alue= "Submit" ></form>{% for item in student%}<p>{{Item.id}}, {{Item}}</p>{% endfor%}{% Endblock %}
To add a path in urls.py:

    URL (r ' ^do/', ' app1.views.do '),

Defining the Do function in views.py

From django.shortcuts import renderfrom django.core.context_processors import csrffrom app1.models import Studentfrom Django Import formsdef do (Request): Context = {}context.update (request) (CSRF) if request. Post:form = Studnetform (Request. POST) if Form.is_valid (): name = form.cleaned_data[' name ']student = student (name = name) Student.save () Form = Studnetform ( ) context = {}context.update (CSRF (request)) Student_list = Student.objects.all () context[' student '] = Student_ listcontext[' form ' = form return render (Request, ' do.html ', context)
Now there are four data in the database:


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

Can see


Then add the name:

You can see that the data was added to the database:



You can define multiple form only in the view and template in the corresponding, the use of the specific form can see the form of the API, the official website: https://docs.djangoproject.com/en/1.8/ref/forms/api/



--django--forms use of Python learning

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.