Reputation: 39023
I'm developing a website that lets the user read books, displayed in different ways. For example, I have the following URLs:
/read_in_white/book_name/chapter_number
- Shows white pages/read_in_pink/book_name/chapter_number
- Shows pink pages
My view functions are read_in_white(request, book_name, chapter_number)
and read_in_pink(request, book_name, chapter_number)
At first I only had read_in_white
, which showed white pages. It also had some paging controls in a Bootstrap navbar. These were all located in the read_in_white.html
template, which included controls for navigating for a different book or chapter. For example, I had the following in the template:
<a href="{% url "app.views.read_in_white" book='Good Book' chapter=1 %}"/>
Now I want to add the read_in_pink
view and template. The navigation part of read_in_pink
will be almost identical to the one in read_in_white
, with one difference:
<a href="{% url "app.views.read_in_pink" book='Good Book' chapter=1"/>
I want to move all the navigation to a base template, but I can't figure out what to put in the (% url %}
tag. How do I find the current view's name? I know I can add it to the context, or I can change book
and chapter
to be query parameters so the URL is /read_in_pink?book=...&chapter=...
, but I was hoping there was something as simple as {% url this_view ... %}
.
Is there?
Upvotes: 0
Views: 60
Reputation: 47202
No, there is not, since that would introduce a very stronger coupling between view and template and that's usually a bad idea.
But this sounds like you want to parameterize the color, doesn't it? Then you can pass the color to the navigation or wherever you want to.
def read_in_color(request, color, book, chapter):
if color == "white":
return render_to_response("whitetemplate")
if color == "pink":
return render_to_response("pinktemplate")
Then in your templates
{% url "app.views.read_in_color" color="pink" book="Good Book" chapter=1 %}
Upvotes: 2