1. Start the django Site Management Function
1. about django. contrib package
It includes many additional components that django comes with, including:
1) management tool: django. contrib. admin
2) User Identification System: django. contrib. auth
3) supports anonymous sessions: django. contrib. sessions
4) User comment system: django. contrib. comments
2. packages required to start the management tool in settings. py
INSTALLED_APPS = ( 'django.contrib.admin', 'django.contrib.auth', 'django.contrib.contenttypes', 'django.contrib.sessions', #'django.contrib.messages', #'django.contrib.staticfiles', 'django_manage_app',)MIDDLEWARE_CLASSES = ( 'django.contrib.sessions.middleware.SessionMiddleware', 'django.middleware.common.CommonMiddleware', #'django.middleware.csrf.CsrfViewMiddleware', 'django.contrib.auth.middleware.AuthenticationMiddleware', #'django.contrib.messages.middleware.MessageMiddleware', #'django.middleware.clickjacking.XFrameOptionsMiddleware',)
3. "Additional tables" required for creating management tools"
Ctrl + alt + r
Syncbd
4. Create Super Users
1) create a Super User after the system creates a table
2) Subsequent command creation in pyCharm
Ctrl + alt + r
Createsuperuser
Password: admin
Note: Only when INSTALLED_APPS contains django. contrib. auth,
The createsuperuser command can be used.
The user created by createsuperuser cannot log on normally (a Password error occurs due to creation errors ).
You can use the admin account to manually change the password:
To install: 'django. contrib. messages. middleware. MessageMiddleware ',
3) After logging on to admin
PS: Since no modules have been created, there are currently only two basic modules: "User Group" and "user.
5. The management interface is translated into Chinese
Add: 'django. middleware. locale. LocaleMiddleware ',
MIDDLEWARE_CLASSES = ( 'django.contrib.sessions.middleware.SessionMiddleware', 'django.middleware.common.CommonMiddleware', #'django.middleware.csrf.CsrfViewMiddleware', 'django.contrib.auth.middleware.AuthenticationMiddleware', 'django.contrib.messages.middleware.MessageMiddleware', #'django.middleware.clickjacking.XFrameOptionsMiddleware', 'django.middleware.locale.LocaleMiddleware',)
Execution result:
6. Solution for page not being rendered
Adjust the following comments for multiple times, and finally comment them out: (the reason is to be determined)
Final Page effect:
2. add your own models to the Management Interface
1. Before custom models is added:
2. Source Code:
Models. py:
from django.db import modelsclass Publisher(models.Model): name = models.CharField(max_length=30) address = models.CharField(max_length=50) city = models.CharField(max_length=60) state_province = models.CharField(max_length=30) country = models.CharField(max_length=50) website = models.URLField() def __unicode__(self): return u'%s, %s'%(self.name,self.country) class Meta: ordering = ['-name']class Author(models.Model): first_name = models.CharField(max_length=30) last_name = models.CharField(max_length=40) email = models.EmailField()
def __unicode__(self):
return u'%s, %s, %s, %s'%(self.id,self.first_name,
self.last_name,self.email)class Book(models.Model): title = models.CharField(max_length=100) authors = models.ManyToManyField(Author) publisher = models.ForeignKey(Publisher) publication_date = models.DateField() def __unicode__(self): return self.title
Admin. py:
from django.contrib import adminfrom django_manage_app.models import Publisher,Author,Book# Register your models here.admin.site.register(Publisher)admin.site.register(Author)admin.site.register(Book)
3. Enter the Management Interface
4. manually add data
1) add three author instances.
2) Add 2 publisher
3) add book
Authors: many-to-many
Publisher: foreign key
Database storage:
use django_db;select *from django_manage_app_book;select *from django_manage_app_book_authors;
Execution result:
3. Field settings
1. A field is allowed to be empty.
When it cannot be blank:
Null allowed:
class Author(models.Model): first_name = models.CharField(max_length=30) last_name = models.CharField(max_length=40) email = models.EmailField(blank=True)
Note: The structure of the author table in the database is not changed. The email field is still not null,
Only the "Null String" is saved, but the django code layer allows null check to pass.
2. "NULL value" and "allow a field to be blank"
String type: NULL does not exist. If NULL is left blank, the system automatically enters "NULL String"
Date type: DateField, TimeField, DateTimeField
"NULL String" is not accepted. Enter "NULL value"
When leaving null, you must specify (blank = True, null = True)
Numeric type: IntegerField, DecimalField, FloatField
"NULL String" is not accepted. Enter "NULL value"
When leaving null, you must specify (blank = True, null = True)
Eg:
class Book(models.Model): title = models.CharField(max_length=100) authors = models.ManyToManyField(Author) publisher = models.ForeignKey(Publisher) publication_date = models.DateField(blank=True, null=True)
Note: in fact, the database structure is not changed at this time, or not null
3. Use of field aliases
Class Author (models. model): first_name = models. charField (max_length = 30) last_name = models. charField (max_length = 40) email = models. emailField (blank = True, verbose_name = 'email ')
4. Customize "display field" and "search field"
You can click the display field name to sort
Before custom display fields: the _ unicode _ method is used by default.
Class Author (models. model): first_name = models. charField (max_length = 30) last_name = models. charField (max_length = 40) email = models. emailField (blank = True, verbose_name = 'email ') def _ unicode _ (self): return u' % s, % s' % (self. id, self. first_name, self. last_name, self. email)
Customize "display field" and "search field ":
Admin. py
#coding:utf8from django.contrib import adminfrom django_manage_app.models import Publisher,Author,Book# Register your models here.class AuthorAdmin(admin.ModelAdmin): list_display = ('first_name', 'last_name', 'email') search_fields = ('first_name', 'last_name')admin.site.register(Publisher)admin.site.register(Author,AuthorAdmin)admin.site.register(Book)
Execution result:
5. Customize "filter fields"
class BookAdmin(admin.ModelAdmin): list_display = ('title', 'publisher', 'publication_date') list_filter = ('publication_date',)admin.site.register(Publisher)admin.site.register(Author,AuthorAdmin)admin.site.register(Book,BookAdmin)
Execution result:
Hierarchical field filtering (ascending result set ):
class BookAdmin(admin.ModelAdmin): list_display = ('title', 'publisher', 'publication_date') date_hierarchy = 'publication_date' ordering = ('publication_date',)
The time filter navigation bar goes deeper layer by layer, from year to month to day
Date_hierarchy only receives strings, not tuples. Only one field can be filtered by layer.
5. Others
1) The order in which fields are displayed and editable when a new record is added.
2) when the number of alternative records for multiple to multiple fields is large
3) when there are many alternative records for foreign key fields
For how to handle this, see: http://djangobook.py3k.cn/2.0/chapter06/