1

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 %}
2
  • 3
    r'realTime/(?P<path>.*)$' should be fine to begin with. Commented Jul 11, 2016 at 16:59
  • @RohitJain Thank you, it worked, I was sure the regex must be having some symbols after <path> but wasn't able to figure out which ones. Commented Jul 11, 2016 at 17:08

2 Answers 2

1

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'),
                         ^^
Sign up to request clarification or add additional context in comments.

1 Comment

I'm using the realTime/ as a Home Page where it lists all the drive letters and other shortcut paths. Thank you for the additional information, I'll certainly keep that in mind. I'm actually using the path as an argument for the view which further processes the variable and returns the render for a new dictionary according to the path in path.
0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.