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 supportThe 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 backgroundThe 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 pageYou 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 display1. 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 display1. 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)
|