The Python framework's Django Learning notes (12)

Source: Internet
Author: User

Django Site Management

11 in the blink of an eye, said the charge did not go smoothly, but leisurely watching TV dramas and went to the cinema to see the new release of the "Ecstatic Road put", "Dear" and "golden Age", said that the rest of the rest now look back, if sleep meal is a word, that is quite a lot. Yes, 11 has passed, also should accept win their hearts, began to prepare for the new year. Ah, no, get ready for work. This is the end of the talk, and next, start learning about Django's site management.

The management interface has a problem: creating it is too cumbersome. Web development is interesting when you develop functionality for the public, but the creation of a management interface is often stereotyped. You have to authenticate users, display and manage tables, validate input validity, and so on. It's tedious and repetitive. What improvements have Django made to these tedious and repetitive tasks? It does everything for you with no less code. It is no longer a problem to create a management interface in Django. This blog is about Django's automatic management interface. This feature works: it reads the metadata in your schema and provides you with a powerful and accessible interface that the site manager can use to work immediately.

  Django.contrib Bag

The Django Automatic management tool is part of the Django.contrib. Django.contrib is a huge set of features that are part of the Django CodeBase, and the Django framework consists of a number of basic code that contains add-ons (add-on). You can think of django.contrib as a practical implementation of the optional Python standard library or universal schema. They are bundled with Django so you don't have to "reinvent the wheel" in development.

Management tools are a part of Django.contrib. Technically speaking, it is called django.contrib.admin. Other features available in Django.contrib such as user identification System (DJANGO.CONTRIB.AUTH), anonymous session support (Django.contrib.sessioins), and user commentary system ( django.contrib.comments). Before you become a Django expert, you will know more about the features of Django.contrib. For now, you just need to know that Django comes with a lot of good add-ons, all of them in the Django.contrib package.

  Activating the Management interface

The first step is to make the following changes to your settings file:

  • Add ' django.contrib.admin ' to Setting's installed_apps configuration (the order of configuration inInstalled_apps is not related, But we like to keep a certain order to facilitate people to read.
  • guaranteed installed_apps contains ' Django.contrib.auth ' , < Span class= "pre" > ' Django.contrib.contenttypes ' and Django.contrib.sessions ' , the Django management tool requires these 3 packages. (If you follow this article to make a MySite project, then please note that we commented on these three items of Installed_apps entries in the last study.) Now, please cancel the comment. )
  • Make sure middleware_classes contains ' Django.middleware.common.CommonMiddleware ',' Django.contrib.sessions.middleware.SessionMiddleware ' and ' Django.contrib.auth.middleware.AuthenticationMiddleware ' . (Again, if you have to follow the MySite, please cancel the comments in front.) )

Step two, runpython manage.py syncdb 。 This step will generate additional database tables used by the management interface. When you put' Django.contrib.auth 'Add inInstalled_appsAfter the first runsyncdbcommand, you will be asked to create a super user. If you don't do this, you need to runpython manage.py createsuperuser To create another Admin user account, otherwise you will not be able to login admin (reminder: only ifInstalled_appsContains' Django.contrib.auth 'Whenpython manage.py createsuperuser This command is only available.)

Third, the admin access is configured in Urlconf (remember, in urls.py ). By default, the command django-admin.py startproject The generated file urls.py the path to the Django Admin is commented out, All you have to do is uncomment. Please note that the following must be ensured:

1 #Include These import statements ...2  fromDjango.contribImportAdmin3 Admin.autodiscover ()4 5 #And include this Urlpattern ...6Urlpatterns = Patterns ("',7     # ...8(r'^admin/', include (Admin.site.urls)),9     # ...Ten)

When this is all configured, you will now find that the Django management tool is ready to run. Start the development server (ex: ' Python manage.py runserver ') and then access it in the browser:http://127.0.0.1:8000/admin/.

Management Interface Introduction

The management interface is designed for non-technical people, so it should be self-explanatory. However, here is a brief introduction to its basic features.

The first thing you see is the login screen as shown.

You want to use the user name and password of the superuser you originally set up. If you can't sign in, run "Python manage.py createsuperuser" to make sure you've created a super user.

Once you are logged in, you will see the administration page. This page lists all the data types that can be edited in the Administration tool. Now, since we have not created any modules, this list has only a few categories: it has only two default management-edit modules: User group (Groups) and users.

Other languages:

Want a second to change the type of language, Django can easily do it, open the settings.py file, find the following section:

1 ' en -US ' 2 # language_code = ' de-ch ' 3 # language_code = ' De-at ' 4 # language_code = ' pt-br ' 5 # language_code = ' ZH-CN '

Change the first line to line fifth, refresh the next screen, and you will find that your management site has become a full Chinese version.

Add your models to admin management

Let's add our own modules to the management tool so that we can use this nice interface for adding, modifying, and deleting objects in the database. We will continue with the last "book" Example. In this, we define three modules:publisher , Author, and book .

Open the file: "admin.py" under the "Books" Directory ("Mysite/books"), and enter the following code:

1  from Import Admin 2  from Import Publisher, Author, Book 3 4 Admin.site.register (Publisher) 5 Admin.site.register (Author) 6 admin.site.register (book)

When you're done, open the page "http://127.0.0.1:8000/admin/" and you'll see a books area with authors, books, and publishers.

Now you have a full-featured management interface to manage these three modules. It's simple!

The Python framework's Django Learning Note (12)

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.