Explain the permissions and groups and messages in Django

Source: Internet
Author: User
There are other features in the certification framework. We'll take a closer look at them in the next few sections.
Permissions

Permissions make it easy to identify actions that users and user groups can perform. They are used by the Django Admin admin site, and you can also use them in your own code.

The Django Admin site uses the following permissions:

    • Only users who have the Add permission set can use the Add form to add a view of the object.
    • Only users who have the Change permission set can use the changes list, change the table, and change the view of the object.
    • Only users who have the Delete permission set can delete an object.

Permissions are set according to the object of each type and are not specific to the specific instance of the object. For example, we can allow Mary to change a new story, but for now it is not allowed to set Mary to change only the new story she created, or to select a permission based on the given status, publication date, or ID number.

will automatically create three basic permissions for each Django model: Add, change, and delete. These permissions are added to the Auth_permission database table when you run the manage.py syncdb command.

The permissions appear in the form ". _ ".

Just like users, permissions are django.contrib.auth.models in the Django model. So if you want, you can also manipulate permissions directly through the Django database API.
Group

Groups provide a common way for you to categorize users according to certain permission rules and other tags. A user can be affiliated to any number of groups.

A user in a group automatically gets the permissions assigned to the group. For example, the Site editors group has can_edit_home_page permissions, and any user in that group has this permission.

Groups can also extend functionality by giving certain user-specific tags. For example, you create a ' special users ' group and allow users in the group to access some of the VIP parts of the site, or send a VIP mail message.

As with user management, the Admin interface is the simplest way to manage groups. However, the group is the Django model Django.contrib.auth.models, so you can use the Django database API to access these groups at the bottom.
News

The message system will receive messages for a given user. Each message is associated with a User.

After each successful operation, the Django Admin management interface uses the message mechanism. For example, when you create an object, you will see the message on the top of the admin page of the object was created successfully.

You can also use the same API to queue and receive messages in your own app. The API is very simple:

    • To create a new message, use User.message_set.create (message= ' Message_text ').
    • To get/delete a message, use User.get_and_delete_messages (), which returns a list of message objects and removes the returned items from the queue.

In the example view, the system saves a message to the user after it has created a play order (playlist).

def create_playlist (Request, Songs):  # Create the playlist with the given songs.  # ...  Request.user.message_set.create (    message= "Your playlist was added successfully."  )  Return Render_to_response ("playlists/create.html",    context_instance=requestcontext (Request))

When using RequestContext, the currently logged in user and his or her message will appear in the context of the template with the template variable {{messages}}.

{% If messages%}
 
  
  
    {% for message in messages%}
  • {{Message}}
  • {% endfor%}
{% ENDIF%}

It is important to note that RequestContext calls get_and_delete_messages in the background, so even if you do not display them, they will be deleted.

Finally, note that this message framework can only serve users who exist in the user database. If you want to send messages to anonymous users, use the session framework directly.

  • Related Article

    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.