Django Learning (ix)---templates filter and Django Shell and admin enhancements

Source: Internet
Author: User

First, Templates filter

Filters are part of the Django template language

Modify variables in a template to display different content

{{value | filter}}

Example: {{list_nums | length}} indicates the length of the list

{{Value | filter | filter | filter}} can be superimposed

Django Template If there is a non-existent variable, will not error, will only give a null value

So modify add_article.html Remove {{% if *%}} Add filter default

<!DOCTYPE HTML><HTML><Head>    <title>Add new articles</title></Head><Body><formAction= "{% url ' blog:sub_article '%}"Method= "POST">{% Csrf_token%}<inputtype= ' hidden 'name= ' article_id 'value= "{{article.id | default: ' 0 '}}">article title<inputtype= "text"name= ' title 'value= ' {{Article.title}} '/>        <BR>article content<inputtype= "text"name= ' content 'value= ' {{article.content}} '/>        <BR>        <inputtype= "Submit"value= "Submit">    </form></Body></HTML>

Second, Django Shell

Python interactive command-line program

Automatic introduction of Project environment

You can use it to interact with the project

Start the Django Shell

Pyhton manage.py Shell

Examples of interactions:

From Blog.models Import article

Article.objects.all ()

Effect: 1) Commissioning work 2) test unknown method

Third, admin enhancements

Register the admin configuration class:

Class Articleadmin (admin. Modeladmin)

Registration: Admin.site.register (models. Article,articleadmin)

Show additional fields

List_diplay = (' title ', ' content ') (is the field name in models)

The admin.py is as follows:

 from Import Admin  from Import Models # Register your models here. class articleadmin (admin. Modeladmin):    = ('title','content' ) Admin.site.register (models. Article,articleadmin)

Admin Filter

List_diplay = (' Pub_time ',)

Note that there is only one member of the tuple, add a comma

In models. Article class add release date in py

 from Import Models # Create your models here. class article (models. Model):    = models. Charfield (max_length=32,default='title')    = models. TextField (null=True)    = models. Datetimefield (auto_now=True)    def__str__(self):          return Self.title

Data migration (can only be displayed in the database)

Execute python manage.py makemigrations

Re-executepython manage.py migrate

生成了数据表

To add a filter to the admin.py:

 fromDjango.contribImportAdmin from.ImportModels#Register your models here.classarticleadmin (admin. Modeladmin): List_display= ('title','content','Pub_time') List_filter= ('Pub_time',) Admin.site.register (models. Article,articleadmin)

Django Learning (ix)---templates filters and django Shell and admin enhancements

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.