Django Tutorial: very useful tips

Source: Internet
Author: User
Tags install django django tutorial
Django Tutorial: very useful tips

Today to introduce you to django tutorial (http://www.maiziedu.com/course/python/307-3024/) in the django background in some tips, we all know that in the django background we only need to add less code, you can achieve powerful functions, the following describes these tips.

Chinese language support

The default Django background language is English and can be changed to Chinese. Add the following in settings. py:

Export age_code = 'zh-cn'

TIME_ZONE = 'Asia/Shanghai'

Use BootStrap in the background

The default style of the Django background is a little simple. you can install and use Bootstrap

1. install django-admin-bootstrapped:

Pip3 install django-admin-bootstrapped

2. in INSTALLED_APPS (settings. py), add:

'Bootstrap3', # put 'Django _ admin_bootstrapped' in front of admin, # put 'Django. contrib. admin' in front of admin ',

Custom style on the background list page

You can set the display columns, pagination, search, and filtering functions in admin. py.

From django. contrib import adminfrom app. models import Blogclass BlogAdmin (admin. ModelAdmin ):

List_display = ('title', 'content', 'catalog ')

List_per_page = 10

Search_fields = ['title',]

List_editable = ['Category ',]

List_filter = ['create _ time',] # Register your models here.

Admin. site. register (Blog, BlogAdmin)

Custom column display

1. modify models. py

For example, the following uses self_name to splice the title and content to display them together.

Class Article (models. Model ):

Title = models. CharField (u 'title', max_length = 100)

Category = models. CharField (u'category', max_length = 50, blank = True)

Content = models. TextField (u'content', blank = True, null = True)

Create_time = models. DateTimeField (u'creation time', auto_now_add = True)

Update_time = models. DateTimeField (u'modify time', auto_now = True, null = True)

Def _ str _ (self ):

Return self. title

Class Meta:

Ordering = ['-create_time']

Verbose_name = u'文'

Verbose_name_plural = u'article management'

Def my_property (self ):

Return self. title + ":" + self. content

My_property.short_description = "self"

Self_name = property (my_property)

2. modify admin. py

Class ArticleAdmin (admin. ModelAdmin ):

List_display = ('title', 'Category ', 'Self _ name ')

Upload images and display

1. modify models. py

Use the image control ImageField

Image = models. ImageField (upload_to = 'images', blank = True)

2. modify settings. py

STATIC_ROOT and MEDIA_ROOT need to set different paths

STATIC_URL = '/static /'

STATIC_ROOT = OS. path. join (BASE_DIR, 'Article/static ')

MEDIA_URL = '/upload /'

MEDIA_ROOT = OS. path. join (BASE_DIR, 'Article/upload ')

3. modify urls. py

Urlpatterns = [

Url (r '^ admin/', include (admin. site. urls )),

......

] + Static (settings. STATIC_URL, document_root = settings. STATIC_ROOT)

+ Static (settings. MEDIA_URL, document_root = settings. MEDIA_ROOT)


This topic was approved by Beckham at, May 17

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.