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