Python Django template operation instructions

Source: Internet
Author: User

At the time of development, I found that the method of creating forms using Python Django templates is indeed quite limited. However, Custom forms are also commendable, in form, we can customize verification rules. The following describes how to operate Python Django templates.

Freemarker is the most complex and powerful. Supporting jsp tag embedding allows us to reuse many existing components, I have also described this in detail in my previous article (join hands to see the combination of freemarker and displaytag ).

The Python Django template was born for Internet applications. Its concise and fast development features are quite appealing. The basic syntax of most template languages is similar. For example, the explicit value in freemarker is $ {}, and in django is {{}}, in freemarker, if is judged as <# if> </# if>, while in django is:

 
 
  1. {% if msg %}     
  2.     Xx     
  3. {% else %}     
  4. Xx     
  5. {% endif%}   

There are two ways to render templates in django:

 
 
  1. def preparePublish(request):     
  2.     t = loader.get_template(publishInfo)     
  3. return HttpResponse(t.render(Context({'categoryList' : None})))   

Plug in a digress. jsp digress, whether ruby, c ++, or python, uses templates in their web frameworks. There are also many templates in java, we are most familiar with freemarker and velocity. This reflects a pattern in web development from one aspect, that is, our view is basically generated based on templates, and jsp is the product of the times, it was created in that chaotic and backward era, but it is strange that there are still so many people holding it.

Django has two forms: one is to define the form class, and the other is to define the automatic form class through the model we define. Since ahuaxuan only provides a small example of information publishing, it cannot fully understand or understand all the details of form in django.

I do feel a lot limited in using Python Django templates to create forms, because in many cases, the data in the model is not from the page. In this case, after the form object is constructed, ahuaxuan has not found the method for modifying the value of form.

The custom form class is also troublesome, that is, to write your own model. This is different from our previous practice. Here, form represents the value object in java, and model is the domain object, in our ssh framework, we usually inherit the value object from our domain object.

Although a bunch of people have raised objections and said they want to separate the two objects because they are at different levels, we can see from practical experience that, there is nothing wrong with this. However, the separation of custom form and model in django may be in line with the psychology of some people. However, Custom forms are also commendable. In form, we can customize verification rules.

At the same time, we can directly generate content on the page based on the form object, but this is also a bit of a hassle, that is, it is more troublesome to change the style, however, in general, the form of the Python Django template is quite characteristic, and it brings us some aspects.

  1. How to embed Python into C ++ applications?
  2. In-depth discussion of Ruby and Python syntax comparison
  3. Introduction to Python
  4. Python Learning Experience: version, IDE selection and encoding solution
  5. Analysis of Python GIL and thread security

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.