Regular expression issue when using Generic Views in Django
I am a front-end developer currently dabbling into Django to build a simple portfolio site to display my work. I used Django quite regularly in my previous job but its now been over 6 months and unfortunately I do have old work to look over.
My site consists of:
A homepage with links to featured "projects" A project list page A project detail page
I am trying to take advantage of Django's Generic Views to create the latter 2 pages for me. However I am encountering issues when editing my urls.py f开发者_运维百科ile. Here it is:
from django.conf.urls.defaults import *
# Uncomment the next two lines to enable the admin:
from django.contrib import admin
from django.conf import settings
admin.autodiscover()
from django.views.generic import list_detail
from homepage.models import Project
projects_list_info = {
'queryset' : Project.objects.all(),
'allow_empty': True,
'template_name' : '../templates/project_list.html',
}
projects_detail_info = {
'queryset' : Project.objects.all(),
'template_object_name' : 'project',
'slug' : 'slug',
'slug_field' : 'slug',
'template_name' : '../templates/project_detail.html',
}
urlpatterns = patterns('',
# Example:
# (r'^howelltocode/', include('howelltocode.foo.urls')),
# Uncomment the admin/doc line below to enable admin documentation:
# (r'^admin/doc/', include('django.contrib.admindocs.urls')),
# Uncomment the next line to enable the admin:
(r'^admin/', include(admin.site.urls)),
(r'^test','homepage.views.viewTest'),
(r'^$','homepage.views.viewHome'),
(r'projects/$', list_detail.object_list, projects_list_info),
(r'^projects/(?P<slug>[-\w]+)/$', list_detail.object_detail, projects_detail_info),
(r'^site_media/(?P<path>.*)$', 'django.views.static.serve', {'document_root': settings.MEDIA_ROOT}),
)
The projects list view page successfully navigates to '/templates/project_list.html' when I type the url 'http://localhost:8000/projects'. However if I type 'http://localhost:8000/projects/htc-wildfire-facebook-application/' to view the "../templates/project_detail.html" page I get a 404 page with a "No project found matching your query" message.
I see this as encouraging as it obviously looking for a project but I think my regular expression could be wrong. Any help would be greatly appreciated.
Thanks
James
You've overwritten the slug coming from the URL with the bare value "slug" in your projects_detail_info dictionary. Remove that entry from the dict.
精彩评论