YouTip LogoYouTip

Django Admin Manage Tool

Django provides a web-based management tool. The Django automatic management tool is part of django.contrib. You can see it in the 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 set of functionalities, and it is part of Django's basic code. * * * ## Activating the Management Tool Usually when we generate a project, it is automatically set up in urls.py, we just need to remove the comments. The configuration items are as follows: ## /HelloWorld/HelloWorld/urls.py File Code: from django.conf.urls import url from django.contrib import admin urlpatterns = [url(r'^admin/', admin.site.urls), ] When all this is configured, the Django management tool can run. * * * ## Using the Management Tool Start the development server, then access http://127.0.0.1:8000/admin/ in the browser, and you will see the following interface: !(#) You can create a superuser by using the command **python manage.py createsuperuser**, as shown below: # python manage.py createsuperuserUsername (leave blank to use 'root'): admin Email address: admin@example.comPassword:Password (again):Superuser created successfully.[root@solar HelloWorld]# Then 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 register the data model to admin first. For example, we created the model Test in TestModel before. Modify TestModel/admin.py: ## HelloWorld/TestModel/admin.py: File Code: from django.contrib import admin from TestModel.models import Test admin.site.register(Test) After refreshing, you can see the Testmodel data table: !(#) * * * ## Complex Models The management page is powerful and fully capable of handling more complex data models. First, add a more complex data model in TestModel/models.py: ## HelloWorld/TestModel/models.py: File Code: from django.db import models 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, on_delete=models.CASCADE,)name = models.CharField(max_length=50)def __unicode__ (self): return self.name There are two tables here. Tag uses Contact as a foreign key. One Contact can correspond to multiple Tags. We can also see many attribute types that we haven't 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 admin.site.register([Test, Contact, Tag]) Refresh the management page, the display result is as follows: !(#) With the above management tool, we can perform complex model operations. > If you haven't created the table structure yet, you can use the following commands: > > $ python manage.py makemigrations TestModel # Let Django know we have some changes in our models $ python manage.py migrate TestModel # Create table structure * * * ## Custom Forms 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 parts. Modify TestModel/admin.py: ## HelloWorld/TestModel/admin.py: File Code: from django.contrib import admin from TestModel.models import Test,Contact,Tag class ContactAdmin(admin.ModelAdmin): fields = ('name', 'email')admin.site.register(Contact, ContactAdmin)admin.site.register([Test, Tag]) The above code defines a ContactAdmin class to explain the display format of the management page. The fields property inside defines which fields to display. Since this class corresponds to the Contact data model, when registering, we need to register them together. The display effect is as follows: !(#) We can also divide the input fields into blocks, and each block can 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 class ContactAdmin(admin.ModelAdmin): fieldsets = (['Main',{ 'fields':('name','email'), }], ['Advance',{ 'classes': ('collapse',), 'fields': ('age',), }])admin.site.register(Contact, ContactAdmin)admin.site.register([Test, Tag]) The above sections are divided into Main and Advance two parts. The classes property specifies the CSS format of the section it belongs to. Here we hide the Advance section: !(
← Cpp While LoopCpp Basic Input Output β†’