M
M
Maxim Dunayevsky2015-04-15 11:53:28
Django
Maxim Dunayevsky, 2015-04-15 11:53:28

How to create a urlpattern with an arbitrary number of parameters of the same type?

How to compose a url that allows you to pass several parameters to the view, the number of which is not known in advance?
It is clear that the classical approach is as follows:

from django.conf.urls import url

from .views import desktop
from .views import users

urlpatterns = [
    url(r'^desktop/$', my_view),
    url(r'^desktop/users/$, users)
]

I want to make a class-based-view that will render the necessary templates, which are beautifully arranged in folders inside the templates directory. However, each folder can have subfolders. The view will analyze the number of received parameters ( *args ) and, depending on this, determine the nesting depth and the path to the desired html file.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
M
Maxim Dunayevsky, 2015-04-15
@dunmaksim

Comrades from StackOverflow recommend this solution to the issue:

urlpatterns = [
    url(r'^(?P<path>[-\w/]+)/$', TemplateView.as_view()),
]

Z
zelsky, 2015-04-15
@zelsky

not a solution but look seriously towards regex

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question