Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 505773
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T06:37:58+00:00 2026-05-13T06:37:58+00:00

I want to create dynamic menus according to user permissions. As was already discussed

  • 0

I want to create dynamic menus according to user permissions. As was already discussed here and by the the documentation itself, I know that I can achieve this in the templates using the following snippet:

{% if perms.polls.can_vote %}
    <li>
        <a href="/polls/vote">Vote</a>
    </li>
{% endif %}

But the problem is that for security reasons I want to limit the access to the views too. The snippet that I found in the documentation is the following:

from django.contrib.auth.decorators import permission_required

def my_view(request):
    # ...
my_view = permission_required('polls.can_vote', login_url='/loginpage/')(my_view)

Isn’t this against DRY principle? Isn’t there a way to define only in one place what is the permission needed for each url? Perhaps in urls.py?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T06:37:59+00:00Added an answer on May 13, 2026 at 6:37 am

    EDIT: (See end of post for the original text of the answer with the initial, simple idea.)

    After being kindly stricken with a cluebat (see the OP’s comment below), I find I can see more to the problem than before. Sorry it took so long. Anyway:

    Would this kind of template be alright for you?

    {% for mi in dyn_menu_items %}
      {% if mi.authorised %}
         <a href={{ mi.url }}>{{ mi.title }}</a>
      {% endif %}
    {% endfor %}
    

    To make this work on the Python side, you could use RequestContext in your views with a custom context processor setting the dyn_menu_items variable appropriately. In case some background information is required, the Advanced Templates chapter of the Django Book introduces RequestContext, shows how to use it with render_to_response (kinda important :-)) etc.

    Also, I guess at this point it could be useful to put the view functions responsible for the locked-up sections of your site in a list somewhere:

    _dyn_menu_items = [(url1, view1, title1, perm1), ...]
    

    Then you could map a couple of functions, say prepare_pattern and prepare_menu_item across that list, having it work roughly like so:

    def prepare_pattern(menu_item):
        url1, view, title, perm = menu_item
        pattern = PREPARE_URLCONF_ENTRY_SOMEHOW(...) # fill in as appropriate
        return pattern
    
    def prepare_menu_item(menu_item):
        url, view, title, perm = menu_item
        mi = PREPARE_THE_BIT_FOR_REQUESTCONTEXT(...) # as above
        return mi
    

    These could be combined into a single function, of course, but not everybody would find the result more readable… Anyway, the output of map(prepare_menu_item, _dyn_menu_items) would need to be a dictionary to be passed to your views by a helpful context processor (the figuring out of which, it being the slightly tedious bit here, I’ll leave to you ;-)), whereas the output of map(prepare_pattern, _dyn_menu_items), let’s call it dyn_menu_patterns, would be used in patterns('', *dyn_menu_patterns), to be used in your URLconf.

    I hope this makes sense and is of some help…

    THE PRE-EDIT ANSWER:

    Based on your short description, I’m not sure what solution would be best for you… But if the permission_required snippet does what you want, just not DRY-ly enough, how about rolling your own wrapper:

    def ask_to_login(perm, view):
        return permission_required(perm, login_url='/loginpage/', view)
    

    You could put this anywhere, including in URLconf. Then you could replace all mentions of '/loginpage/' with reference to a variable defined towards the top of your URLs file and you’d have yourself a solution with a single mention of the actual login URL, for one-place-only update of said URL should you have to move it around. 🙂

    Of course the views would still need to be wrapped explicitly; if that bothers you, you could try to make ask_to_login into a decorator for easy wrapping at the definition site. (But perhaps it’s really best not to do it, lest you force yourself to dig your views from under the decorator in case you need them undecorated at some point in the future.)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to create dynamic lambda expressions so that I can filter a list
Here is the problem details: 1) I want to create dynamic (ip based) download
I want to create a dynamic Expression<Func<T,Y>> . Here is the code which works
I want to create XML with dynamic Element and attribute according to Database record
I want to create dynamic content based on this. I know it's somewhere, as
I want to create dynamic subdomains on my site. I know how to create
I want to create a dynamic menu that will get it's items from a
i want create Dynamic Slideshow in Jquery i'm Write this code var ctx =
I want to create dynamic tabPages in TabControl. In each tabPage I create dataGridView
I want to create dynamic number of thread which is depends on the database

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.