Today we introduce some of the little tricks in the Django daemon in the Django Tutorial (http://www.maiziedu.com/course/python/307-3024/), and we all know that we just need to add less code to the Django backend. You can implement powerful features, and the following are specific tips.Chinese Language SupportThe default language for the Django backend is English and can be modified to Chinese. Added in settings.py: language_code = ' zh-cn ' time_zone = ' Asia/shanghai 'Backstage Use bootstrapThe default style of the Django backend is somewhat rudimentary, and you can install and use BOOTSTRAP1. Installing the DJANGO-ADMIN-BOOTSTRAPPED:PIP3 Install Django-admin-bootstrapped2. Added in Installed_apps (settings.py): ' Bootstrap3 ', #放在admin前面 ' django_admin_bootstrapped ', #放在admin前面 ' Django.contrib.admin ',Background List page custom stylesYou can set the display number of columns, paging, searching, filtering and other functions from Django.contrib import adminfrom app.models import blogclass blogadmin (admin. admin.py). Modeladmin): List_display = (' title ', ' content ', ' catalog ') List_per_page = ten 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 such as the following self_name the title and content together to display class article (models. Model): title = models. Charfield (U ' title ', max_length=100) category = models. Charfield (U ' classification ', 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_ti Me '] verbose_name = u ' article ' verbose_name_plural = U ' Article Management ' Def My_property (self): return self.title + ":" + self.content my_p Roperty.short_description = "Self" Self_name = Property (My_property) 2. Modify Admin.pyclass articleadmin (admin. Modeladmin): List_display = (' title ', ' Category ', ' Self_name ')upload images and display1. Modify models.py using the picture control Imagefieldimage = models. ImageField (upload_to= ' images ', blank=true) 2. Modify Settings.pystatic_root and Media_root 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.pyurlpatterns = [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)
|