Damon Julian
Damon Julian

Reputation: 3989

if condition in template to display link in django

In my django app's index.html (which I consider as the home page of the site)I am giving links to various locations in the website.When the user reaches one of the locations,he should find a link there, to get back to the home page.So I created a base.html to be inherited by all the pages including index.html.In the base.html,I am providing these links.

base.html

...
<a href="{%url archives  %}"> Archives </a>
<br/>
<a href="{% url reports   %}"> Reports </a>
<br/>
<a href="{% url home   %}"> Home </a>
<br/>
..

My problem is that ,when I am on index page(home) ,I don't want to display the home link.Is this possible to do so in the template,using an {% if %} condition? Currently I am not passing any parameter in the view

@login_required
def index(request, template_name):
    print 'index()::template=',template_name
    return custom_render(request, {},template_name )

def custom_render(request,context,template):
    req_context=RequestContext(request,context)
    return render_to_response(template,req_context)

Upvotes: 1

Views: 3059

Answers (2)

Cera
Cera

Reputation: 1909

This isn't exactly what you asked for, but an alteratively way to do this which I often much prefer is to use JavaScript to either hide or change the color of links to the current page. For example, if you were using jQuery:

// Assumes relative paths in a elements
$('a[href="' + window.location.pathname + '"]').each(function (index, element) {
   $(element).addClass('someClass');
});

someClass could be a CSS rule to set the 'display' attribute of that element to 'none', or to simply make the link look like plain text, and behave like plain text when mousing over or clicking on it.

This has two possible advantages:

  • it prevents your template from becoming too convoluted, and from being difficult to maintain if you start arbitrarily adding nav elements
  • you have the ability to keep your nav consistent between pages, which in my opinion is a good design decision

It's no silver bullet, but I think it's important to recognise that sometimes these things aren't necessarily best done in templates. I think of my django templates first and foremost as a way of structuring data. There will always be some element of presentation in them, but I try to remove that as much as possible. Others will disagree.

Upvotes: 1

Timmy O&#39;Mahony
Timmy O&#39;Mahony

Reputation: 53971

Firstly, you can use the django render shortcut to avoid having to use the long-winded custom_render.

from django.shortcuts import render
@login_required
def index(request, template_name):
    return render(request, template_name,extra={})

You can be cheeky and in your base.html do something like:

{% url home as home_url %}
{% if request.path != home_url %}<a href="{{ home_url }}">Home</a>{% endif %}

to quickly check if the current page is the home page.

If it might get more complicated in the future, you can use a template tag to check whether or not the current URL you are on matches another URL. Here is a template tag I have used before to check if a menu link is currently active:

https://gist.github.com/2049936

Upvotes: 4

Related Questions