0

I want to create a regex that matches urls like this

filmslist?name=allaboutmymother?director=pedroalmodovar

in urlpatterns, I've put this:

path('api/filmslist\?name=?P<name>(.*)\?director=?P<director>(.*)', views.filmslist)

but it doesn't match. Thanks in advance

4
  • api/filmslist should be enough, everything from ? onwards is a querystring (GET parameters) Commented Apr 28, 2021 at 16:57
  • You have your named capture groups wrong, it is (?P<some_cool_name>...). That said, it should be filmslist\?name=(?P<name>.*)\?director=(?P<director>.*), see regex101.com/r/eLfjsX/1 Commented Apr 28, 2021 at 16:59
  • The qyerystring is not part of the path. You will need to parse this in the view itself. Commented Apr 28, 2021 at 17:02
  • 1
    Are there really two ?s in the URL? Commented Apr 28, 2021 at 17:03

2 Answers 2

3

Everything after the first ? in a url is a querystring and is not involved in the pattern matching. So if your url is like filmslist?name=allaboutmymother?director=pedroalmodovar then your url pattern should be like:

path('api/filmslist', views.filmslist)

Now to get those parameters in your view you can do something like:

def filmslist(request):
    name = request.GET.get('name')
    director = request.GET.get('director')
    ...

Also as noted by @Code-Apprentice in their comment their should only be one ? in a url and the parameters need to be separated by an & so your url should be filmslist?name=allaboutmymother&director=pedroalmodovar

Sign up to request clarification or add additional context in comments.

Comments

2

The qyery string is not part of the path. You will need to parse this in the view itself. In the view you can access parameters with request.GET which is a dictionary-like structure, so:

path('api/filmslist/', views.filmslist)

in the view you can then inspect the values with:

def filmslist(request):
    name = request.GET.get('name')
    director = request.GET.get('director')
    # …

If the parameter is not passed in the queryset, the corresponding name or director will be None.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.