Reputation: 58
I am creating a database site and one of the desired features is to have a button in the navbar that will that the user to a random object in a model. The Django Cookbook gives an example of a random function for a model, which I implemented.
However, I cannot seem to call this function from the navbar, since the navbar is in a separate html file that is included
above the {% block content %}
, which allows it to appear on every page of the site. However, this means it never sees the model object itself, so I cannot access the function with {{object.get_random}}
in the navbar.
One idea I had was to use a link in the navbar like this
<a href="{% url 'roma:category_list.object.get_random.get_absolute_url' %}">Random Category</a>
where category_list
is the view that has the model passed to it. From there I hoped to grab an object from the model, then the get_random
function, and finally the url from the object returned from the get_random
function.
This obviously does not work.
Is there a solution that will allow me to access the function in the navbar html?
Upvotes: 0
Views: 188
Reputation: 96
Instead of doing the randomization when rendering the template, I would make a link to a dedicated view with a url like "/random". When that url is called, the view can do the randomization and redirect you to the random model.
Upvotes: 2