Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get current route instead of route_path in Pyramid

Tags:

python

pyramid

I have navigation bar, such as :

<div id="nav">
 <ul>
  <li
  % if request.current_route_path == "somepath":
  class="current"
  % endif
  > <a href='/page1"> 1 </a></li>
  <li
  % if request.current_route_path == "another_test":
  class="current"
  %endif
  > <a href="/page2"> 2 </a> <li>
  </ul>
</div>

I want to add some codes to test what is the current route so that I can decide which bar to be highlighted (class="current").

I know Pyramid has a method current_route_path to get the path of current URL. However, I think it's better to use route name instead of route path. Does anyone have ideas about this?

like image 336
Hanfei Sun Avatar asked Nov 25 '12 16:11

Hanfei Sun


2 Answers

What you want is to use the matched_route.

if request.matched_route.name == 'my_route_name':
like image 99
Michael Merickel Avatar answered Sep 17 '22 22:09

Michael Merickel


I think you are looking for something like this:

% if request.url == request.route_url('my_route_name')
##do stuff
%endif

where request.url is the current url and request.route_url('my_route_name') is the url of a named route

like image 33
Brian Cajes Avatar answered Sep 21 '22 22:09

Brian Cajes