python - NoReverseMatch error when using Django url template tag with optional parameters, but NOT when visiting the URL in the browser -
python - NoReverseMatch error when using Django url template tag with optional parameters, but NOT when visiting the URL in the browser -
i have url needs business relationship next patterns:
localhost:8000/staffing-agencies localhost:8000/staffing-agencies/90210 (zip code) localhost:8000/staffing-agencies/portland-or (city-state)when type in of these urls in browser, work expected. however, i'm getting noreversematch
error when seek refer url django's url template tag.
here relevant url.py files:
# urls.py urlpatterns = patterns('', url(r'^', include('bos.apps.search.urls', namespace='search', app_name='search')), ) # search/urls.py urlpatterns = patterns('bos.apps.search', url(r'^staffing-agencies/' r'((?p<city>[a-za-z]+)-(?p<state>[a-za-z]{2}))?' r'((?p<zip>[0-9]{5}))?$', 'views.main', name='main'), )
i thought might optional parameters, of these variances throw noreversematch
error:
<a href="{% url "search:main" zip=97214 %}">test</a> <a href="{% url "search:main" city="portland" state="or" %}">test</a>
this variance not throw error:
<a href="{% url "search:main" %}">test</a>
i'm using django 1.6.5
this not improve solution, 1 of solution
url(r'^staffing-agencies/(?p<city>[a-za-z]+)*-(?p<state>[a-za-z]{2})*?(?p<zip>[0-9]{5})*?$', 'views.main', name='main'),
in views:
def main(request, city=none, state=none, zip=none):
in html:
<a href="{% url "search:main" city='sadasd' state='ds' zip=12345 %}">test</a>
in case url work this,
localhost:8000/staffing-agencies localhost:8000/staffing-agencies/-90210 (zip code) localhost:8000/staffing-agencies/portland-or (city-state) python django url django-templates
Comments
Post a Comment