我正在尝试制作非常有意义的网址,但我想我做错了。
这有效:
from django.conf.urls.defaults import patterns, url
from places.views import explore_view
urlpatterns = patterns('',
url(r'', explore_view, name='explore'),
)
这不会:
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'),
)
当我收到此错误时:
未找到带有参数“()”和关键字参数“{}”的“探索”的反向操作。
这是 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))