15π
A possibility that you might consider is matching the entire string of possible values within the url pattern portion and pull out the specific pieces within your view. As an example:
urlpatterns = patterns('',
url(r'^browse/(?P<match>.+)/$', 'app.views.view', name='model_browse'),
)
def view(request, match):
pieces = match.split('/')
# even indexed pieces are the names, odd are values
...
No promises about the regexp I used, but I think you understand what I mean.
(Edited to try and fix the regexp.)
3π
I agree with Adam, but I think the pattern in urls.py should be:
... r'^browse/(?P<match>.+)/$' ...
The β\wβ will only match βwordβ characters, but the β.β will match anything.
- [Django]-Using django-admin on windows powershell
- [Django]-How can I get MINIO access and secret key?
- [Django]-How to get username from Django Rest Framework JWT token
1π
Iβve an alternative solution, which isnβt quite different from the previous but itβs more refined:
url(r'^my_app/(((list\/)((\w{1,})\/(\w{1,})\/(\w{1,3})\/){1,10})+)$'
Iβve used unnamed url parameters and a repetitive regexp. Not to get the βis not a valid regular expression: multiple repeatβ i place a word at the beginning of the list.
Iβm still working at the view receiving the list. But i think illβ go through the args or kwargs.. Cannot still say it exactly.
My 2 cents
- [Django]-Form with CheckboxSelectMultiple doesn't validate
- [Django]-VueJS + Django Channels
- [Django]-Django :How to integrate Django Rest framework in an existing application?
0π
Same answer came to me while reading the question.
I believe model_browse view is the best way to sort the query parameters and use it as a generic router.
- [Django]-Django composite unique on multiple model fields
- [Django]-Django-taggit β how do I display the tags related to each record
- [Django]-What is the difference render() and redirect() in Django?
0π
I think the answer of Adam is more generic than my solution, but if you like to use a fixed number of arguments in the url, you could also do something like this:
The following example shows how to get all sales of a day for a location by entering the name of the store
and the year
, month
and day
.
urls.py:
urlpatterns = patterns('',
url(r'^baseurl/location/(?P<store>.+)/sales/(?P<year>[0-9][0-9][0-9][0-9])-(?P<month>[0-9][0-9])-(?P<day>[0-9][0-9])/$', views.DailySalesAtLocationListAPIView.as_view(), name='daily-sales-at-location'),
)
Alternativly, you could also use the id of the store by changing (?P<store>.+)
to (?P<store>[0-9]+)
. Note that location
and sales
are no keywords, they just improve readability of the url.
views.py
class DailySalesAtLocationListAPIView(generics.ListAPIView):
def get(self, request, store, year, month, day):
# here you can start using the values from the url
print store
print year
print month
print date
# now start filtering your model
Hope it helps anybody!
Best regards,
Michael
- [Django]-Django composite unique on multiple model fields
- [Django]-Django: Implementing a Form within a generic DetailView
- [Django]-Create a field whose value is a calculation of other fields' values