tag (views)

archive

restricting views to staff users in django Sep
02
1
0

Hopefully you've already found the login_required decorator for Django views, which make it incredibly simple to integrate authentication into the site.

One thing that's not documented well is there's another decorator @staff_member_required. This works the same way @login_required does except it restricts access to users who are both authenticated and has staff status set in their settings.

This comes in handy when extending the admin or writing custom moderation applications.

You can do something similar to:

from django.shortcuts import render_to_response
from django.template.context import RequestContext
from django.contrib.admin.views.decorators import staff_member_required


@staff_member_required
def view(request):

    ...

    return render_to_response('template.html', context_instance=RequestContext(request))

comments

get the current site object in a django view Mar
22
1
0

You can use the default Sites framework to get the current site in a django view.

from django.contrib.sites.models import Site

site = Site.objects.get_current()
domain = Site.objects.get_current().domain

or

from django.contrib.sites.models import Site
from django.conf import settings

site = Site.objects.get(id=settings.SITE_ID)

comments