Search code examples
pythondjangourlfilepathfile-structure

Keep URL while surfing data Structure in Django web app


I am attempting to create a django based website. The goal of one part of the site is to show the contents of a database in reference to its file structure.

I would like to keep the URL the same while traveling deeper into the file structure, as opposed to developing another view for each level of the file structure a person goes into.

The simplest way I can think to achieve this is to pass the current directory path in the request variable but I am not sure how to do this or if it is possible since it would have to be linked in the html file, and not the view file that is written in python.

If you are able to at very least point me in the right direction it would be greatly appreciated!


Solution

  • Do you mean that you want to avoid urls like www.example.com/db-contents/?level=2? Passing variables in through request.GET like that is the normal way to do it. All the requests will go to the same url and view, and you can just do request.GET['level'] in the template.

    If you absolutely want the url to stay completely static (`www.example.com/db-contents/) then you can send and receive your data via AJAX calls.

    by adding ?foo=bar to the url it means you can access the 'foo' key in the request.GET dictionary, and the value of that key will be "bar". for multiple variables you separate with the & symbol.

    For example:

    www.example.com/db-contents/?foo=bar&somevar=1&anothervar=
    
    >>> request.GET['foo']
    'bar'
    >>> request.GET['somevar']
    '1'
    >>> request.GET['anothervar']
    None
    >>> "anothervar" in request.GET
    True
    >>> "notsetvar" in request.GET
    False