[Answer]-How to parse a list in Django urlparser?

1👍

It’s not possible to do this automatically. From the documentation: “Each captured argument is sent to the view as a plain Python string, regardless of what sort of match the regular expression makes.” Splitting it in the view is the way to go.

The second regex in your answer is OK, but it does allow some things you might not want (e.g. 'django+++python+'). A stricter version might be something like: (?P<tag>\w+(?:\+\w+)*). Then you can just do a simple tag.split('+') in the view without worrying about any edge cases.

0👍

This works for now:

url(r'^documents/tag/(?P<tag>[A-Za-z0-9_\+]+)/$', ListDocuments.as_view(), name="list_documents"),

But I’d like to be able to get that w back in there instead of the full list of characters like that.

[Edit]

Here we go:

url(r'^documents/tag/(?P<tag>[\w\+]+)/$', ListDocuments.as_view(), name="list_documents"),

I will still select a better answer if there is a way for the Django urlparser to give the view an actual list instead of just one big long string, but if that’s not possible, this solution does work.

👤Joseph

Leave a comment