Re: How to store position of current/active navigation entry
I now came up with this solution: path = req.get_full_path() if path.startswith(entry['href']): entry['active'] = True thanks for helping :-) -- You received this message because you are subscribed to the Google Groups "Django users" group. To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscr...@googlegroups.com. To post to this group, send email to django-users@googlegroups.com. Visit this group at http://groups.google.com/group/django-users. For more options, visit https://groups.google.com/groups/opt_out.
Re: How to store position of current/active navigation entry
On Sun, Aug 4, 2013 at 6:39 AM, DJ-Tom wrote: > Hi, > > I don't think I know how to actually do what you are suggesting. > > Lets say I have three menu options with the following URLS: > > "One" - /one/show > "Two" - /two/show > "Three" - /three/show > > Currently I have somthing like this in my main.html page template: > > {% if navi %} >> {% for entry in navi %} >> {{ >> entry.name}} >> {% endfor %} >> {% endif %} >> > > With the following view function: > > def show_mainPage(request): >> >> navigation = [ {'href':'/one', 'name':"One"}, >>{'href':'/two', 'name':"Two"}, >>{'href':'/three', 'name':"Three"}] >> return TemplateResponse(request, 'main_page/main.html', {'navi': >> navigation, 'main_settings':main_settings.objects.get(id=1)}) >> > > So how would I set the "active" CSS class in my template for the correct > nav entry? > > Perhaps I don't understand the nature of the problem. I'm assuming that the nav entry to be highlighted by default is a function of which page is being viewed. Further, I presume that you know which page to display because a GET request was issued (either vanilla or AJAX), and the path part of the URL (possibly in combination with query parameters, if you have a truly complex scheme) allows the combination of your urlpatterns and view functions to know what to render. It follows (to me, at least) that the information available in the request is enough to know which item should be highlighted. If that's correct, then nclude whatever attribute you need to on a navi entry object that it can be compared with a value derived from the request (either in your view, in a template tag, or (best) placed in the context by a template context processor (still has to be a request context to give you access to the request object)). Use this with an "if" tag to control adding a class to the "li" element (for instance) which, in common with simple CSS, highlights it. There are also schemes with mutually unique classes on your "li" elements, the same, or related, class as for the one to be highlighted on a containing element, and a set of CSS rules, one for each highlight-able menu item, to highlight the "li" if the outer class is an ancestor of the corresponding "LI"'s class. This is messy, potentially requiring dynamically generated CSS if your Nav tree changes dynamically. The use case is where you are pulling new data through AJAX to morph the page into a new one, so that a new menu item should be highlighted. The JavaScript need only change the class on the outer element, allowing your to to avoid having to loop across the nav removing the one highlight class (from the first approach), or from the one that your JavaScript knows was highlighted, before applying it to the newly selected "li" (mutually unique classes are still a help to this JavaScript). -- You received this message because you are subscribed to the Google Groups "Django users" group. To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscr...@googlegroups.com. To post to this group, send email to django-users@googlegroups.com. Visit this group at http://groups.google.com/group/django-users. For more options, visit https://groups.google.com/groups/opt_out.
Re: How to store position of current/active navigation entry
There is no usual way to do this because you can have anything behind menu item. So here is what I do in the same situations. Let say that we have some categories listed in your navi. Every category must have unique ID so in navi view pass some variable with that ID for example "foo = entry.pk" Then in template you can do this {{ entry.name }} hope it helps On Thursday, August 1, 2013 3:30:23 PM UTC+2, DJ-Tom wrote: > > Hi, > > I'm currently creating a web app with django that will have a side bar > menu with several sub-sections. > > I'm wondering if there is a "standard" way to store the "current" or > "active" menu entry so I can highlite it in the menu area each time a > request takes place. > > Is it possible to attach arbitrary information to the current users > session object? > > thanks > thomas > -- You received this message because you are subscribed to the Google Groups "Django users" group. To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscr...@googlegroups.com. To post to this group, send email to django-users@googlegroups.com. Visit this group at http://groups.google.com/group/django-users. For more options, visit https://groups.google.com/groups/opt_out.
Re: How to store position of current/active navigation entry
Hi, I don't think I know how to actually do what you are suggesting. Lets say I have three menu options with the following URLS: "One" - /one/show "Two" - /two/show "Three" - /three/show Currently I have somthing like this in my main.html page template: {% if navi %} > {% for entry in navi %} > {{ entry.name > }} > {% endfor %} > {% endif %} > With the following view function: def show_mainPage(request): > > navigation = [ {'href':'/one', 'name':"One"}, >{'href':'/two', 'name':"Two"}, >{'href':'/three', 'name':"Three"}] > return TemplateResponse(request, 'main_page/main.html', {'navi': > navigation, 'main_settings':main_settings.objects.get(id=1)}) > So how would I set the "active" CSS class in my template for the correct nav entry? -- You received this message because you are subscribed to the Google Groups "Django users" group. To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscr...@googlegroups.com. To post to this group, send email to django-users@googlegroups.com. Visit this group at http://groups.google.com/group/django-users. For more options, visit https://groups.google.com/groups/opt_out.
Re: How to store position of current/active navigation entry
One traditional place to store navigation state is in the URL. If you are reloading the page with a new GET (such as because a link was clicked), you have to indicate what page to load in the URL, whether in the path or in the parameters. The view has access to the URL, and uses it to know where you are in the hierarchy, in turn allowing it to load the correct page. But since it knows where you are, it can encode the Nav side bar accordingly. (Use RequestContexts in all views and render the Nav with a template tag in your base template. The tag can see the request object, so it knows the path, etc.) So far this doesn't require any JavaScript. But the better Navs can open sub-trees on hover, etc., and that does require JavaScript, and means that the whole tree is sent every time anyway, with, for example, CSS classes to control what's highlighted and what's open by default. If, on the other hand, you are not reloading the page, but filling in stuff using AJAX, the AJAX request still has to indicate what is needed. Probably the JavaScript (required to do AJAX) already knows the new spot in the hierarchy, and can perform the highlighting. If not, because, for instance, information the user entered will be looked up in the database to determine the place in the hierarchy, the AJAX response must include Nav information for the AJAX. But, yes, you can store pretty much anything on the session object. But note that this probably does the wrong thing if the user hits back, or uses a bookmark, or otherwise enters a specific path on your site, since the information on the session (he gets the same session) is for where you thought he was. Also, suppose he has two windows or tabs of the same browser open on you site, intending to view separate pages: they use the same session. On Thu, Aug 1, 2013 at 9:30 AM, DJ-Tom wrote: > Hi, > > I'm currently creating a web app with django that will have a side bar > menu with several sub-sections. > > I'm wondering if there is a "standard" way to store the "current" or > "active" menu entry so I can highlite it in the menu area each time a > request takes place. > > Is it possible to attach arbitrary information to the current users > session object? > > thanks > thomas > > -- > You received this message because you are subscribed to the Google Groups > "Django users" group. > To unsubscribe from this group and stop receiving emails from it, send an > email to django-users+unsubscr...@googlegroups.com. > To post to this group, send email to django-users@googlegroups.com. > Visit this group at http://groups.google.com/group/django-users. > For more options, visit https://groups.google.com/groups/opt_out. > > > -- You received this message because you are subscribed to the Google Groups "Django users" group. To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscr...@googlegroups.com. To post to this group, send email to django-users@googlegroups.com. Visit this group at http://groups.google.com/group/django-users. For more options, visit https://groups.google.com/groups/opt_out.
How to store position of current/active navigation entry
Hi, I'm currently creating a web app with django that will have a side bar menu with several sub-sections. I'm wondering if there is a "standard" way to store the "current" or "active" menu entry so I can highlite it in the menu area each time a request takes place. Is it possible to attach arbitrary information to the current users session object? thanks thomas -- You received this message because you are subscribed to the Google Groups "Django users" group. To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscr...@googlegroups.com. To post to this group, send email to django-users@googlegroups.com. Visit this group at http://groups.google.com/group/django-users. For more options, visit https://groups.google.com/groups/opt_out.