Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NoReverseMatch at /

Tags:

django

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

This works:

from django.conf.urls.defaults import patterns, url
from places.views import explore_view

urlpatterns = patterns('',
    url(r'', explore_view, name='explore'),
)

This does not:

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'),
)

As I get this error:

Reverse for 'explore' with arguments '()' and keyword arguments '{}' not found.

Here is the code for explore_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))
like image 227
webjay Avatar asked Apr 30 '13 11:04

webjay


2 Answers

This line:

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

...is defining an url that takes an argument countryorcategory in the template. You need to put an argument on your url either of the following in your template:

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

If you want to continue to use non-argument 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 both with and without an argument.

like image 61
mfitzp Avatar answered Nov 19 '22 20:11

mfitzp


For me, I forgot the namespace of the Route. Instead of

{% url 'login' %}

I should have written

{% 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')
like image 22
Jonas Gröger Avatar answered Nov 19 '22 22:11

Jonas Gröger