In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-15 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article mainly explains the specific usage of Admin management tools in Django. The content of the explanation is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn the specific usage of Admin management tools in Django.
The Django admin automatic management tool is part of django.contrib, and we can see it in the INSTALLED_APPS settings of the project's settings.py file. We can think of it as an app application that comes with django itself.
INSTALLED_APPS = ('django.contrib.admin',' django.contrib.auth', 'django.contrib.contenttypes',' django.contrib.sessions', 'django.contrib.messages',' django.contrib.staticfiles',)
Django.contrib is a huge set of features that is part of the basic code of Django.
Management tools
Add url (r'^ admin/', admin.site.urls) to the urls.py file of the project, which is usually automatically set up when you build the project, we just need to remove the comments.
From django.conf.urls import urlfrom django.contrib import adminurlpatterns = [url (r'^ admin/', admin.site.urls),] use administrative tools
Start the development server, then access http://127.0.0.1:8000/admin/ in the browser to get the login interface, as shown in the following figure:
You can then create a superuser by commanding python manage.py createsuperuser.
> > python manage.py createsuperuserUsername (leave blank to use 'root'): username Email address: email Password: password Password (again): confirm password Superuser created successfully.
After creating the superuser, we can enter the user name and password in the login screen. After the login is successful, the interface is as follows:
In order for the admin interface to manage a data model, we need to register the data model with admin first. For example, we have previously created the model Test in TestApp. Modify the TestApp/admin.py file:
From django.contrib import adminfrom TestApp.models import Test# Register your models here.admin.site.register (Test)
After refreshing in the web page, you can see the TestApp data table, as shown below:
Complex background data model
Our django page management is very powerful and fully capable of dealing with more complex data models.
First, we can add two tables (data model) to the TestApp/models.py file:
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.nameclass Tag (models.Model): contact = models.ForeignKey (Contact, on_delete=models.CASCADE) ) name = models.CharField (max_length=50) def _ _ unicode__ (self): return self.name
In the above two tables, Tag has Contact as the foreign key. A Contact can correspond to multiple Tag.
Register multiple models in the TestApp/admin.py file and display:
From django.contrib import adminfrom TestApp.models import Test,Contact,Tag# Register your models here.admin.site.register ([Test,Contact,Tag])
Refresh the administration page and display the results as follows:
With the above management tools, we can operate complex models.
Custom form
We can customize the management page to replace the default page. Such as the "add" page above. We want to show only the name and email sections. Modify TestApp/admin.py:
From django.contrib import adminfrom TestApp.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 illustrates the display format of the management page. Where the fields property defines the fields to be displayed.
Since this class corresponds to the Contact data model, we need to register them together when we register. The display effect is as follows:
You can also divide the input fields into blocks, and each column can define its own format. Modify the TestApp/admin.py to:
From django.contrib import adminfrom TestApp.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 column is divided into Main and Advance. Classes describes the CSS format of the part it is in. Let the Advance be partially hidden here:
There is a Show button next to the Advance section to expand. After expansion, you can click Hide to hide it, as shown in the following figure:
Inline (Inline) display
The Contact above is the external key of Tag, so there is an external reference relationship. In the default page display, the two will be separated, can not reflect the subordinate relationship between the two. We can use inline display to have Tag attached to the editing page of Contact.
Modify TestApp/admin.py:
From django.contrib import adminfrom TestApp.models import Test, Contact, Tag# Register your models here.class TagInline (admin.TabularInline): model = Tagclass 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])
The effect after visiting the page is as follows:
Display of list pages
We can also customize the display of Contact's list page, such as displaying more columns in the list, just add the list_display attribute to the ContactAdmin:
From django.contrib import adminfrom TestApp.models import Test, Contact, Tag# Register your models here.class TagInline (admin.TabularInline): model = Tagclass 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])
The effect after refreshing the page is as shown in the figure:
Search function
The search feature is great for managing a large number of records, and we can use search_fields to add a search bar to the list page:
From django.contrib import adminfrom TestApp.models import Test, Contact, Tag# Register your models here.class TagInline (admin.TabularInline): model = Tagclass 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]) Thank you for your reading The above is the content of "specific usage of Admin management tools in Django". After the study of this article, I believe you have a deeper understanding of the specific usage of Admin management tools in Django, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.