Django Learning form (forms)

Source: Internet
Author: User

Our blog now has implemented a view of blog list, blog view. Now it's time for us to create and update blogs.

To create and update your blog, we need to learn about Django forms.

What does the Django form function do during the process of working with the form?

    • Preparation and reconstruction of data transfer
    • Create an HTML form for your data
    • Receive and process submitted forms and data from the client

The Django form class is the core component of the system. In Django, the model describes the logical architecture of an object, the behavior that it shows to us in the way it is. Similarly, the form class describes a form and determines how it works and displays.

A field of a model class is mapped to a field in a database, similar to a field in a form class that maps to the <input> element of a form in HTML.

First, create a template file that creates a blog edit.html

<!DOCTYPE HTML><HTML>    <Head>        <title>My Blog</title>    </Head>    <Body>        <formMethod= ' Post '>{% Csrf_token%} {{form.as_p}}<Buttontype= "Submit">Save</Button>        </form>    </Body></HTML>

Yes, add a link to create a blog on the homepage (no matter what the landscaping, the implementation of the function again)

<!DOCTYPE HTML><HTML>    <Head>        <title>My Blog</title>    </Head>    <Body>        <formMethod= ' Post '>{% Csrf_token%} {{form.as_p}}<Buttontype= "Submit">Save</Button>        </form>    </Body></HTML>[Email protected]:~/python/django/myproject/blog/templates/blog$ more index.html<!DOCTYPE HTML><HTML>    <Head>        <title>My Blog</title>    </Head>    <Body>            <ahref={% url ' blogs:edit '%}>Edit</a>{% If blogs%} {% for blog in blogs%}<ahref={% url ' blogs:detail ' blog.id%}><H2>{{Blog.title}}</H2></a>        <P>Published {{Blog.published_time}}</P>        <P>{{Blog.text | linebreaks}}</P>{% endfor%} {% Else%}<P>No Blog was published</P>{% endif%}</Body></HTML>

Next, add the URL

 fromDjango.conf.urlsImportPatterns,url fromBlog.viewsImport*Urlpatterns= Patterns ("', the URL (r'^$', index,name='Index'), url (r'^edit/$', edit,name='Edit'), url (r'^(? p<id>\d+)/$', detail,name='Detail'),)

Add a URL corresponding to the view function

 fromDjango.shortcutsImportRender,get_object_or_404,redirect fromBlog.modelsImportBlog,postformImportdatetimedefIndex (Request): Blogs=Blog.objects.all ()returnRender (Request,'blog/index.html',{'Blogs': Blogs})defDetail (request,id): Blog=get_object_or_404 (blog,pk=ID)returnRender (Request,'blog/detail.html',{'Blog': Blog})defedit (Request):ifrequest.method=='POST': Form=Postform (Request. POST)ifform.is_valid (): Post=form.save (commit=False) Post.user=Request.user Post.created_time=Datetime.datetime.now () post.published_time=Datetime.datetime.now () post.save ( )returnRender (Request,'blog/detail.html',{'Blog':p OST}) Else: Form=Postform ()returnRender (Request,'blog/edit.html',{'form': Form})

OK, start the server, enter the URL, we can add a blog post.

Django Learning form (forms)

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.