[Django]-Django URL Config โ€“ Match filename

12๐Ÿ‘

โœ…

\w does not match ., so the .png extension is not being matched. Add the . dot to your character class. You want to drop the trailing slash as well:

uploads_url_patterns = patterns('',
    url(r'^uploads/view/(?P<file_name>[\w.]{0,256})$', ShowImageView.as_view(), name='show'),
)

This now will match the literal text uploads/view/ followed by between 0 and 256 letters, digits, underscores and dots.

Quick demo:

>>> import re
>>> re.search(r'^uploads/view/(?P<file_name>[\w.]{0,256})$', 'uploads/view/test_filename.png')
<_sre.SRE_Match object at 0x103aa5828>
>>> re.search(r'^uploads/view/(?P<file_name>[\w.]{0,256})$', 'uploads/view/test_filename.png').groupdict()
{'file_name': 'test_filename.png'}
๐Ÿ‘คMartijn Pieters

Leave a comment