Home > Backend Development > PHP Tutorial > Django tutorial: very practical tips

Django tutorial: very practical tips

WBOY
Release: 2016-07-25 08:43:46
Original
1097 people have browsed it
Today I will introduce you to the django tutorial (http://www.maiziedu.com/course/python/307-3024/) some tips in the django background, we all know that in We only need to add a little code to the backend of django to achieve powerful functions. Let’s introduce these tips in detail below.
Chinese language support
DjangoThe default language in the background is English and can be modified to Chinese. Add in settings.py:
LANGUAGE_CODE = 'zh-CN'
TIME_ZONE = 'Asia/Shanghai'
Use BootStrap in the background
DjangoThe default style of the background is a bit crude , You can install and use Bootstrap
1.Install django-admin-bootstrapped:
pip3 install django-admin-bootstrapped
2.In INSTALLED_APPS(settings. py) Add in it: o'Bootstrap3 ',#Put in front of' django_admin_bootstrapped ',#Put in front of admin's' django.contrib.admin',
The backup table custom style can be in AdminMin, .py
Set the display column number, paging, search, filtering and other functions by yourselffrom 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)
Customized column display1.Modify
models.pyFor example, use self_name
to change title and content Spliced ​​together to displayclass 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'modification time', auto_now=True, null=True)
def __str__(self):
return self.title
class Meta:
ordering = ['-create _time'] _ Verbose_name = u 'Article'
Verbose_name_plural = U 'Article Management'
DEF My_Property (Self):
Return Self.title + ":" + Self.Content
hort_descripting = " self"
self_name = property(my_property)
2.
Modify
admin.pyclass ArticleAdmin(admin.ModelAdmin):
list_display = ('title', 'category', 'self_name' )
Upload pictures and display1.Modify
models.py
Use image controlImageField
image = models.ImageField(upload_to='images', blank=True)
2.Modifysettings.py
STATIC_ROOT and MEDIA_ROOT Requires different settings Path
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.Modifyurls.py
urlpatterns = [
                                                                         .... ..
                                                                                                                                                     
This topic was reviewed and approved by Xiaobei on 2016-5-17 13:27

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template