python - How to parse a list in Django urlparser? -
on stack overflow, can view list of questions multiple tags @ url such http://stackoverflow.com/questions/tagged/django+python
.
i'd similar in project working on, 1 of url parameters list of tags, i'm not sure how write regex urlparser can parse out. i'm fond of so's way of using +
sign, it's not dealbreaker. imagine urlparser may have take whole string (foo+bar+baz) single variable give view, fine can split in view itself- is, i'm not expecting url parser give view split list, if can, better!
right have is:
url(r'^documents/tag/(?p<tag>\w+)/$', listdocuments.as_view(), name="list_documents"),
which pulls out 1 single tag since \w+
gets me [a-za-z0-9_]
, not +
. tried like:
url(r'^documents/tag/(?p<tag>[\w+\+*])/$', listdocuments.as_view(), name="list_documents"),
but no longer matched documents/tag/foo
nor documents/tag/foo+bar
.
please assist, i'm not great regex, thanks!
it's not possible automatically. documentation: "each captured argument sent view plain python string, regardless of sort of match regular expression makes." splitting in view way go.
the second regex in answer ok, allow things might not want (e.g. 'django+++python+'
). stricter version might like: (?p<tag>\w+(?:\+\w+)*)
. can simple tag.split('+')
in view without worrying edge cases.
Comments
Post a Comment