Example

1 Create Django project polls app

This example show quickly how to apply django_roles_access. As is necessary a simple Django project and application the example will use the polls application that you can find in Django documentation: Polls_App_example.

Note

The present example assume you have all the code of polls app.

2 App and views name

Make sure you name views in polls/urls.py as in example you can found in writing more views. Also note how has been configurated app_name ='polls' .

3 Start using django_roles_access

Imagine you want to keep polls application public, in this way all users (anonymous or not) can vote a polls; but result should be restricted to a special users with specific role.

Achieve this is very simple with django_roles_access:

  • Install django_roles_access middleware; or add django_roles_access decorators to results views

    polls/views.py
    ...
    from django_roles_access.decorator import access_by_role
    ...
    
    @access_by_role
    def results(request, question_id):
        response = "You're looking at the results of questions %s."
        return HttpResponse(response % question_id)
    
  • Go to admin site.

  • Create a new django_roles_access.models.ViewAccess and configure it:

If new users need access to the view, you only need to add then user to any of the groups added to roles attribute. This can be done in run time from admin site.

Note

Is not nice at all to receive 403 Forbidden after voting!!!. A simple code modification could help:

polls/views.py
...

def vote(...

    ...

    return HttpResponseRedirect(reverse('polls:index'))