skip to Main Content

Is it possible to make URL in Django to follow folder path like in GitHub, Dropbox

https://github.com/<username>/<path>/<path>/<file>

Currently, I am doing it like this

re_path(r'^.*', myview)

then using the split('/') function in view

Is there is a batter way to do that?

The functionality of the website is that you can create a folder and inside that folder, you can either create another new folder or create a file.

Let’s say if you create a folder called python and create a file in this folder called list, then the URL must be like this

www.bla-bla.com/python/list/

And if you create a new folder in the python folder called “data type” and create a file “list” in “data type” folder then the URL must look like this

www.bla-bla.com/python/data-type/list/

Just to make URL more SEO friendly.

Thanks

2

Answers


  1. I couldn’t completely understand what is your problem. When designing a web app you gotta create routes and then from inside of the functions to handle each route you render w/e html file you want. Example

    from django.urls import path
    from . import views
    
    urlpatterns = [
        # ex: /polls/
        path('', views.index, name='index'),
        # ex: /polls/5/
        path('<int:question_id>/', views.detail, name='detail'),
        # ex: /polls/5/results/
        path('<int:question_id>/results/', views.results, name='results'),
        # ex: /polls/5/vote/
        path('<int:question_id>/vote/', views.vote, name='vote'),
    ] 
    

    reference – https://docs.djangoproject.com/en/2.2/intro/tutorial03/#targetText=A%20view%20is%20a%20%E2%80%9Ctype,page%20for%20a%20single%20entry.

    that’s how it’s done in flask – https://hackersandslackers.com/the-art-of-building-flask-routes/#targetText=Flask%20contains%20a%20built%2Din,a%20function%20containing%20route%20logic.

    Login or Signup to reply.
  2. If I understand your question correctly, the best way to do this is:

    urls.py

    from django.urls import path
    from . import views
    
    urlpatterns = [
        path('<username>/<path>/<filename>/', views.file, name='file'),
    ] 
    

    views.py

    def file(request, username, path, filename):
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search