Django Tutorial: Useful tips

Source: Internet
Author: User
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)
This thread was reviewed by Beckham on 2016-5-17 13:27
  • 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.