django site management details

  • 2020-06-15 09:44:13
  • OfStack

The management interface is a very important part of the infrastructure. This is a web page based and limited trusted administrator interface that allows you to add, edit and delete web content. Django has its own automatic administration interface. This feature works like this: it reads the metadata in your schema and then provides you with a powerful and usable interface that web site administrators can use to work immediately.

The administrator module of Django is part 1 of Django's standard library, django.contrib. The package also includes 1 other useful modules:

django.contrib.auth

django.contrib.sessions

django.contrib.comments

We will cover the use of django's management interface (admin) in several sections.

configuration

The configuration of admin is not something we have to worry about. Both django and app will be created automatically for us. You can comment it out if you don't need it. To use admin you need to check the following points:

1. In the INSTALLED_APPS Settings file


'django.contrib.admin'
'django.contrib.auth',
'django.contrib.contenttypes'
'django.contrib.sessions'

Because the admin module depends

2. In the MIDDLEWARE_CLASSES attribute


'django.middleware.common.CommonMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware'
'django.contrib.auth.middleware.AuthenticationMiddleware'.

Also because of dependence

3. Run pythonmanage.pymakemigrations and ES48en.pymigrate to create new database tables for modules such as admin.

Since there is an auth module in apps, you will be asked to create a superuser. So before that you can create a superuser using the pythonmanage.pycreatesuperuser command. This one is pretty simple, just type it when prompted.

4. Set admin mapping in ES59en.py, which is also automatically added by the system.


from django.contrib import admin
admin.autodiscover()
urlpatterns = patterns('',
  #...
  (r'^admin/', include(admin.site.urls)),
  #...
)

After the above steps, run runserver and you can access the admin interface via /admin/ this url. It's just that at this point the admin interface has nothing but users and user groups

admin py file

The admin. py file is automatically created under each app. The compilation of this file directly affects the display content of the admin interface.

1. Register the model class

2.


from django.contrib import admin
from mysite.books.models import Publisher,Author, Book
admin.site.register(Publisher)
admin.site.register(Author)
admin.site.register(Book)

This is the most basic data addition function. When you add a new file to the project folder, restart server to take effect. You should see the data model you defined in the main interface of admin. In addition if you want to add data of your admin interface function, need to make sure of MIDDLEWARE_CLASSES 'django. contrib. messages. middleware. MessageMiddleware'. This allows us to easily manipulate the database contents in the admin interface and the system will automatically synchronize to the database.

Customize admin module display

Although we have an admin interface that can basically meet the requirements through the above Settings, comparison list 1 is displayed. The basic display Settings should be in the models file. Such as:


class Author(models.Model):
  first_name = models.CharField(max_length=30)
  last_name = models.CharField(max_length=40)
email =models.EmailField(blank=True)

Simply set blank=True in the field declaration, which defaults to False, to make email null.

Property verbose_name in the Field function controls how this field is displayed in admin

verbose_name in the Meta inner class gives the model a more readable name, and ordering determines how the data is displayed.

The return value of ___ can determine what name the table should appear with in admin.

Rights management

Because you are logged in as a power user, you can create, edit, and delete any image. However, different environments require different permissions, and the system does not allow everyone to be a superuser. Management tool has a user permission system, through which you can according to the needs of users to specify their permission, so as to achieve part of the purpose of access to the system.

User accounts should be generic and usable outside of the administrative interface. But we now think of it as part 1 of the management interface. In Chapter 104, we'll show how to integrate user accounts with your website (not just management tools) in one place.

You edit users and their permissions through the admin interface just like you edit any other object. We saw this earlier in this chapter when we browsed the user and user group areas. As you might expect, the user object has a standard username, password, email address, and real name, and it also has permission definitions for using the administrative interface. First, there is a set of three Booleans:

The active flag, which controls whether the user is active or not. If a user's account is disabled and the user tries to log in with it, he will not be able to log in even if the password is correct.

The member flag, which controls whether the user can log in to the administrative interface, can be used to distinguish public users from administrative users, since the user system can be used to control public pages.

The superuser flag, which gives the user permission to add, modify, and delete any item in the administrative interface. If a user account has this flag, all permissions (even if they are not) are ignored.

Normal active, non-superuser admin users can enter according to 1 set of permissions. Each editable object in the admin interface (books, authors, publishers) has three permissions: create permissions, edit permissions, and delete permissions. Licensing a user also indicates that the user can perform the operations described by the license.

When you create a user, it doesn't have any permissions, and it's up to you to decide what permissions it should have. For example, you can give a user permission to add and modify publishers without giving him permission to delete it. Note that these permissions are defined at the module level, not the object level. For example, you can ask Jack Bauer to change any book, but you can't ask him to change only books published by the Press of The Machine Industry.

Permission management systems also control editing users and permissions. If you give someone permission to edit a user, he can edit his own permission, which might not be what you want. Giving a user permission to modify a user essentially turns him into a superuser.

You can also assign users to groups. A group simplifies the action of applying a set of permissions to all members of a group. Groups are useful when giving specific permissions to a large number of users.

conclusion

Above is the django site management details of the entire content, I hope to help you. Interested friends can continue to refer to other related topics in this site, if there is any deficiency, welcome to comment out. Thank you for your support!


Related articles: