OS X 10.9.2 and Django 1.6.5.
1. Authenticate ()
User authentication.
Views. py
from django.contrib.auth import authenticateuser = authenticate(username=username, password=password)if user is not None: # the password verified for the user if user.is_active: print (‘User is valid, active, and authenticates‘) else: print (‘The password is valid, but the account has been disabled!‘)else: print (‘The username and password were incorrect.")
''' Distinguishes the content based on whether the user logs on. ''' articles = [] If request. user. is_authenticated (): articles = article. objects. all () else: articles = article. objects. filter (group _ GT = 1) return Articles
/Templates/temp.html
{% If user. is_authenticated %} <I> welcome, <strong >{{ user. username }}</strong> </I> <a href = "/accounts/logout/"> quit </a >{% else %} <a href = "/accounts /login/"> login </a> {% endif %}
2. Permission and group
Create licenses and groups, and add licenses and groups to users
>>> From blog. models import Article >>> from Django. contrib. auth. models import group, permission >>> from Django. contrib. contenttypes. models import contenttype >>># create a license >>> permission = permission. objects. create (codename = 'can _ add_article', name = 'can post on article', content_type = content_type) >>> permission <permission: blog | Article | can post on Article >>> permission2 = permission. objects. create (codename = 'can _ modify_article', name = 'can modify article', content_type = content_type) >>> permission2 <permission: blog | Article | can modify Article >>> permission3 = permission. objects. create (codename = 'can _ del_article', name = 'can del article', content_type = content_type) >>> permission3 <permission: blog | Article | can del article >>># create group >>> group1 = group. objects. create (name = 'can _ publish ') >>> group1 <group: can_publish >>># Add a permission to a group >>>> group1.permissions. add (permission, permission2) >>## create a user >>> from Django. contrib. auth. models import user >>> user = user. objects. create (username = 'Tom ', password = 'Password') >>> user <User: Tom >>># add group >>> user. groups. add (group1) >>## add license >>> user. user_permissions.add (permission3) >>## view permissions in the user group >>> user. get_group_permissions () Set ([u'blog. can_modify_article ', u'blog. can_add_article ']) >>## view all permissions of the user >>> user. get_all_permissions () Set ([u'blog. can_modify_article ', u'blog. can_del_article ', u'blog. can_add_article ']) >>## verify whether the user has a permission >>> user. has_perm ('blog. can_del_article ') True >>> # verify that the user Shi fo has certain permissions >>> user. has_perms (['blog. can_del_article ', 'blog. can_add_article ', 'blog. can_modify_article ']) True
Views. py
# Use the decorator from Django. contrib. auth. decorators import permission_required @ permission_required ('blog. delete_article ', login_url ='/accounts/login/') def article_delete (request, num = 0): '''delete log''' if num: article = article. objects. get (ID = int (Num) else: article = article try: article. delete () return httpresponseredirect ('/blog/article/') failed t exception: Return httpresponseredirect ('/blog/article/page/') # No decorator if request is used. user. has_perm ('accounts. can_mark'): Pass
/Emplates/temp.html
{% if perms.foo %} <p>You have permission to do something in the foo app.</p> {% if perms.foo.can_vote %} <p>You can vote!</p> {% endif %} {% if perms.foo.can_drive %} <p>You can drive!</p> {% endif %} {% else %} <p>You don‘t have permission to do anything in the foo app.</p> {% endif %}