home PYTHONJAVA
 

Django Admin Administration Tool

Django provides web-based management tools.

The Django automated management tool is part of django.contrib. You can see it in INSTALLED_APPS in your project's settings.py :

/HelloWorld/HelloWorld/settings.py File code:

INSTALLED_APPS = ( 'django.contrib.admin', 'django.contrib.auth', 'django.contrib.contenttypes', 'django.contrib.sessions', 'django.contrib.messages', 'django.contrib.staticfiles', )

django.contrib is a large feature set that is part of the Django base code.


Activate management tools

Usually we will automatically set up the urls.py when we build the project, we just need to remove the comments.

Configuration items are as follows:

/HelloWorld/HelloWorld/urls.py File code:

# urls.py from django.conf.urls import url from django.contrib import admin urlpatterns = [ url(r'^admin/', admin.site.urls), ]

When this is all configured, the Django management tools will run.


Using management tools

Start the development server, and then visit http://127.0.0.1:8000/admin/ in the browser to get the following interface:

You can create a superuser with the command python manage.py createsuperuser as follows:

# python manage.py createsuperuser
Username ( Leave blank to use 'root'): admin
Email address: admin@welookups.com
Password:
Password ( Again):
Superusercreate successfully.
[root@solar HelloWorld]#

Enter the username and password to log in. The interface is as follows:

In order for the admin interface to manage a data model, we need to first register the data model to admin. For example, we have previously created the Model Test in TestModel. Modify TestModel/admin.py:

HelloWorld/TestModel/admin.py: File Code:

from django .contrib import admin from TestModel.models import Test # Register your models here. admin.site.register(Test)

After refreshing, you can see the Testmodel data table:


Complex Model

The management page is powerful and fully capable of handling more complex data models.

First add a more complex data model to TestModel/models.py:

HelloWorld/TestModel/models.py: File Code:

from django .db import models # Create your models here. class Test(models.Model): name= models.CharField(max_length=20) class Contact(models.Model): name= models.CharField(max_length=200) age = models.IntegerField(default=0) email = models.EmailField() def __unicode__(self): return self.name class Tag(models.Model): contact= models.ForeignKey(Contact) name= models.CharField(max_length=50) def __unicode__(self): return self.name

There are two tables here. Tag takes Contact as the foreign key. A Contact can correspond to multiple tags.

We can also see a number of attribute types that have not been seen before, such as IntegerField for storing integers.

Register multiple models in TestModel/admin.py and display them:

HelloWorld/TestModel/admin.py: File Code:

from django .contrib import admin from TestModel.models import Test,Contact,Tag # Register your models here. admin.site.register([Test, Contact , Tag])

Refresh the management page and display the results as follows:

With the above management tools we can perform complex model operations.

If you have not created a table structure before, you can create it with the following command:

$ python manage.py Makemigrations TestModel # Let Django know that we have some changes in our model 
$ python manage.py migrate TestModel #Create a table structure

Custom Form

We can customize the management page to replace the default page. For example, the "add" page above. We want to display only the name and email sections. Modify TestModel/admin.py:

HelloWorld/TestModel/admin.py: File Code:

from django .contrib import admin from TestModel.models import Test,Contact,Tag # Register your models here. class ContactAdmin(admin.ModelAdmin): fields = ('name', 'email' ) admin.site.register(Contact, ContactAdmin) admin.site.register([Test, Tag ])

The above code defines a ContactAdmin class that describes the display format of the administration page.

The fields property in

defines the fields to display.

Since this class corresponds to the Contact data model, we need to register them together when registering. The display effect is as follows:

We can also divide the input field, and each column can also define its own format. Modify TestModel/admin.py to:

HelloWorld/TestModel/admin.py: File Code:

from django .contrib import admin from TestModel.models import Test,Contact,Tag # Register your models here. class ContactAdmin(admin.ModelAdmin): fieldsets = ( ['Main',{ 'fields':('name',' email'), }], ['Advance',{ 'classes': ('collapse',) , # CSS 'fields': ('age',) , }] ) admin.site.register(Contact, ContactAdmin) admin.site.register([Test, Tag ])

The above columns are divided into Main and Advance. Classes describe the CSS format of the part it is in. Let the Advance part be hidden here:

The Advance section has a Show button next to it for expansion. Once expanded, you can hide it by clicking Hide, as shown below:


Inline display

The above Contact is the external key of the Tag, so there is an external reference relationship.

In the default page display, separating the two does not reflect the affiliation of the two. We can use the inline display to have the Tag attached to the Edit page of Contact.

Modify TestModel/admin.py:

HelloWorld/TestModel/admin.py: File Code:

from django .contrib import admin from TestModel.models import Test,Contact,Tag # Register your models here. class TagInline(admin.TabularInline): model= Tag class ContactAdmin(admin.ModelAdmin): inlines = [TagInline] # Inline fieldsets = ( ['Main',{ 'fields':('name',' email'), }], ['Advance',{ 'classes': ('collapse',) , 'fields': ('age',) , }] )admin.site.register(Contact, ContactAdmin) admin.site.register([Test])

Shows as follows:


Display of list page

After Contact enters several records, the Contact list page looks like this:

We can also customize the display of this page, such as displaying more columns in the list, just add the list_display property to ContactAdmin:

HelloWorld/TestModel/admin.py: File Code:

from django .contrib import admin from TestModel.models import Test,Contact,Tag # Register your models here. class TagInline(admin.TabularInline): model= Tag class ContactAdmin(admin.ModelAdmin): list_display = ('name','age' , 'email') # list inlines = [TagInline] # Inline fieldsets = ( ['Main',{ 'fields':('name',' email'), }], ['Advance',{ 'classes': ('collapse',) , 'fields': ('age',) , }] ) admin.site.register(Contact, ContactAdmin) admin.site.register([Test])

Refresh the page display as follows:

The search feature is very useful when managing large numbers of records. We can use search_fields to add a search bar to the list page:

HelloWorld/TestModel/admin.py: File Code:

from django .contrib import admin from TestModel.models import Test,Contact,Tag # Register your models here. class TagInline(admin.TabularInline): model= Tag class ContactAdmin(admin.ModelAdmin): list_display = ('name','age' , 'email') # list search_fields = ('name',) inlines = [TagInline] # Inline fieldsets = ( ['Main',{ 'fields':('name',' email'), }], ['Advance',{ 'classes': ('collapse',) , 'fields': ('age',) , }] ) admin.site.register(Contact, ContactAdmin) admin.site.register([Test])

In this example we searched for a record with name welookups and the results are as follows:

The Django Admin management tool has a lot of useful features, and interested students can delve into it.






welookups is optimized for learning.© welookups. 2018 - 2019 All Right Reserved and you agree to have read and accepted our term and condition.