Django's admin component

Source: Internet
Author: User

1. The admin app in the Django project

Django provides a web-based management tool.

The Django Automatic management tool is part of the Django.contrib. You can see it in the settings.py of the project Installed_apps:

#application DefinitionInstalled_apps= [    'Django.contrib.admin',    'Django.contrib.auth',    'Django.contrib.contenttypes',    'django.contrib.sessions',    'django.contrib.messages',    'Django.contrib.staticfiles',    'APP01']

2. Start Admin management tool

The Django feature has been configured by default for admin routing. In urls.py, the following:

 from Import Admin  from Import  = [    path ('admin/', Admin.site.urls),]

3. Using admin

3.1. Before using admin, create MySQL database:

DATABASES = {    'default': {        'ENGINE':'Django.db.backends.mysql',        'NAME':'Book_info',        'HOST':'localhost',        'PORT': 3306,        'USER':'User name',        'PASSWORD':'Password',    }}

3.2, and configure the Pymysql

Configure Pymysql in the project Catalog "__init__.py":

Import pymysqlpymysql.install_as_mysqldb ()

3.4. Define the data table in APP01

 fromDjango.dbImportModels#Create your models here.classPublisher (models. Model): ID= Models. Autofield (primary_key=True) name= Models. Charfield (max_length=64,unique=True)#def __str__ (self):    #return Self.nameclassBook (models. Model): ID= Models. Autofield (primary_key=True) ISBN= Models. Bigintegerfield (null=false,unique=True) name= Models. Charfield (max_length=64) Price= Models. Floatfield (null=False) URL= Models. Charfield (max_length=128) Publish=models. ForeignKey ( to="Publisher", To_field="ID", Related_name="Books",        #related_query_name= "Book_q",On_delete=models. CASCADE) Pub_date= Models. Datefield (null=true,blank=True)#def __str__ (self):    #return Self.nameclassAuthor (models. Model): ID= Models. Autofield (primary_key=True) name= Models. Charfield (max_length=64) Book= Models. Manytomanyfield (to=" Book")    #def __str__ (self):    #return Self.nameclassTranslator (models. Model): ID= Models. Autofield (primary_key=True) name= Models. Charfield (max_length=64,null=False) Book= Models. Manytomanyfield (to=" Book")    #def __str__ (self):    #return Self.name
View Code

3.5. Create an object-relational mapping

Execute in Terminal:

Python manage.py Makemigrationspython manage.py Migrate

3.6. Create super users

Execute in Terminal:

Python manage.py Createsuperuser

Enter the user name, and password.

3.7. Introduce the table in models in the admin.py of APP01

 from Import Book,publisher,author,translator

3.8. Use

To manage a data model in the Admin interface, we need to register the data model with admin first.

Admin.site.register (book) Admin.site.register (Publisher) admin.site.register (Author) Admin.site.register ( Translator)

3.9. Login

In the browser input: http://127.0.0.1:8009/admin/, using the user, password login, you can achieve the model corresponding data table additions and deletions of the function.

4, the admin of the customization

If you want to do more custom operations, you need to take advantage of modeladmin to do so, such as:

#Registration Method 1classpublisheradmin (admin. Modeladmin): List_display= ("name","Address") Admin.site.register (models. Publisher, Publisheradmin)#Registration Method 2@admin. Register (models. Book)classbookadmin (admin. Modeladmin): List_display= ("title"," Price","publish_date","Publisher")

Django's admin component

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.