-1

I am learning Django, usually I use urls like www.mysite.com/post/1, and I have understood them because they are well described in the documentation. Now I need to create a url like: www.mysite.com/calendar/?day=21&?month=12&year=2020, I don't understand what should I write in my ulrpatterns list. I have tried something like this:

url(r'^search/(?P<parameter>\w+)', views.calendar, name='calendar')

but it doesn't work. What should I write as regex expression?

Thanks

Bob Rob
  • 164
  • 2
  • 10
  • 1
    I think this answer https://stackoverflow.com/a/3711911/683329 should work? Short version: The part from '?' onwards is not part of the URL path, it's treated separately. – Jiří Baum Aug 26 '20 at 13:37

1 Answers1

2

These parameters are not part of the path, you can't capture them using the url patterns.

You can access them directly inside your view by using request.GET, see the docs.

A common pattern is the following:

def calendar(request):
    day = request.GET.get("day")  # Will be None if "day" isn't in the query
    month = request.GET.get("month")
    year = request.GET.get("year")
    [...]
Gabriel
  • 734
  • 11
  • 26