Reputation: 2291
I'm making a browsable directory structure in Django, and I have made it using button forms and GET
requests, however, I'm trying to achieve the same using links instead of buttons, and I'm confused with regex, infact I do not know a thing about it. I tried some stuff and other related answers from SO, but no avail.
Here is my url pattern (for the one working using buttons and I'd like to make it work with links as well):
url(r'^realTime/', views.view_real_time_index, name='view_real_time_index'),
What I want is to extract the path following the realTime/
in the suffix from the url.
And this is the template snippet I'm trying to work with:
{% for name,path in directory %}
<li>
<p>
<a href='/realTime/{{path}}'> {{name}} </a>
</p>
</li>
{% endfor %}
Upvotes: 1
Views: 717
Reputation: 179
In your urls.py use the following code
url(r'^realTime/(?P<path>.*)', views.view_real_time_index, name='view_real_time_index'),
And in your views.py you can retrieve and use "path" as:
def view_real_time_index(request, path) :
print(path)
#your code
Upvotes: 0
Reputation: 627344
You need to define the path
named capture group in your regex in order to be able to use {{path}}
later. To match anything after realTime/
you only need .*
, you do not even need to define $
:
url(r'^realTime/(?P<path>.*)', views.view_real_time_index, name='view_real_time_index'),
^^^^^^^^^^^
Or, if there must be at least 1 character in the path, replace *
with +
:
url(r'^realTime/(?P<path>.+)', views.view_real_time_index, name='view_real_time_index'),
^^
Upvotes: 1