I suspect it may be something to do with the way I am styling my regular expressions, because I get the following output when I try to go to: http://127.0.0.1:8000/recipes/search/fish/ for example…
Using the URLconf defined in gfp.urls, Django tried these URL patterns, in this order:
^recipes/$
^recipes/category/(?P<category>\d+)/$
^recipes/search/(?P<term>\d+)/$
^recipes/view/(?P<slug>\d+)/$
^admin/
The current URL, recipes/search/fish/, didn’t match any of these.
For reference here is my URLconf
urlpatterns = patterns('',
url(r'^recipes/', 'main.views.recipes_all'),
url(r'^recipes/category/(?P<category>\d+)/$', 'main.views.recipes_category'),
url(r'^recipes/search/(?P<term>\d+)/$', 'main.views.recipes_search'),
url(r'^recipes/view/(?P<slug>\d+)/$', 'main.views.recipes_view'),
For reference here are the views I am attemping to use at the moment
def recipes_all(request):
return HttpResponse("this is all the recipes")
def recipes_category(request, category):
return HttpResponse("this is the recipes category % s" % (category))
def recipes_search(request, term):
return HttpResponse("you are searching % s in the recipes" % (term))
def recipes_view(request, slug):
return HttpResponse("you are viewing the recipe % s" % (slug))
I suspect it is my regular expression, would anyone be able to explain what is wrong with it please? I have seen /w(?) used on some url regex, but it doesnt go into it in the Django tuorial here:
'^recipes/search/(?P<term>\d+)/$'matches/recipes/search/123456/while
'^recipes/search/(?P<term>[-\w]+)/$'is probably what you need. (Updated with hyphens)Take a look at Python re docs to understand what ‘\d’, ‘\w’ and others mean.