NoReverseMatch in /

I'm trying to make pretty meaningful urls, but I think I'm doing it wrong.

It works:

from django.conf.urls.defaults import patterns, url from places.views import explore_view urlpatterns = patterns('', url(r'', explore_view, name='explore'), ) 

It does not mean:

 from django.conf.urls.defaults import patterns, url from places.views import explore_view urlpatterns = patterns('', url(r'(?P<countryorcategory>[0-9A-Za-z._%+-]+)', explore_view, name='explore'), ) 

How do I get this error:

The converse for 'explore' with the arguments '()' and the arguments of the keyword '{}' was not found.

Here is the code for study_view:

 def explore_view(request, countryorcategory=None): """ This is the explore view - to view places sugeested by ambassadors """ user = request.user page = request.GET.get("page", 1) per_page = request.GET.get("per_page", 20) category_id = request.GET.get("category_id", None) attrs = request.GET lat = safe_attr(attrs, "lat", "float", None) lon = safe_attr(attrs, "lon", "float", None) q = request.GET.get('q', None) if q and not lat or lon: cache_key = 'GoogleGeocode-{}'.format(hashlib.md5(q.encode('UTF-8', 'replace')).hexdigest()) latlon = cache.get(cache_key) if not latlon: latlon = geocode(q) if latlon: cache.set(cache_key, latlon) if latlon: lat = latlon['lat'] lon = latlon['lng'] if not q: q = '' category_names = getattr(settings, "EXPLORE_CATEGORIES", []) categories = [Category.objects.get(name=cat_name).serialize() for cat_name in category_names] more = True places = Place.objects.explore_places(user, category_id=category_id, lat=lat, lon=lon, page=page, per_page=20) if len(places) != per_page: more = False return render_to_response('explore/main.html', {'places': places, 'categories': categories, 'category_id': category_id, 'lat': lat, 'lon': lon, 'more': more, 'q': q}, RequestContext(request)) 
+7
source share
3 answers

This line:

 url(r'(?P<countryorcategory>[0-9A-Za-z._%+-]+)', explore_view, name='explore') 

... defines the URL that takes the countryorcategory argument in the template. You need to specify an argument in your template at your url:

 {% url 'explore' argument %} {% url 'explore' countryorcategory=argument %} 

If you want to continue to use non-verbal URLs with the same name, you can define additional URLs with the same name but with different patterns. For example:

 urlpatterns = patterns('', url(r'(?P<countryorcategory>[0-9A-Za-z._%+-]+)', explore_view, name='explore'), url(r'', explore_view, name='explore'), ) 

Then {% url 'explore' %} should work with and without the argument.

+11
source

I assume that you are using a template with something like this:

  {% url 'explore' argument %} 

And this error probably means that argument not configured for anything.

+1
source

For me, I forgot the Route namespace. Instead

 {% url 'login' %} 

I had to write

 {% url 'accounts:login' %} 

with this configuration:

 # root URLs url(r'^accounts/', include('myproject.accounts.accounts.urls', namespace='accounts')) # accounts URLs url(r'^login$', views.login, name='login') 
+1
source

All Articles