在 Django 中使用通用视图时的正则表达式问题
我是一名前端开发人员,目前正在尝试使用 Django 来构建一个简单的作品集网站来展示我的作品。我在之前的工作中经常使用 Django,但现在已经超过 6 个月了,不幸的是我确实有旧的工作需要检查。
我的网站包括:
主页,其中包含特色“项目”的链接 项目列表页面 项目详细信息页面
我试图利用 Django 的通用视图为我创建后 2 个页面。但是,我在编辑 urls.py 文件时遇到问题。如下:
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}),
)
当我输入 URL“http://localhost:8000/projects”时,项目列表视图页面成功导航到“/templates/project_list.html”。但是,如果我输入“http://localhost:8000/projects/htc-wildfire-facebook-application/”来查看“../templates/project_detail.html”页面,我会得到一个 404 页面,其中显示“没有找到匹配的项目”您的查询”消息。
我认为这令人鼓舞,因为它显然是在寻找项目,但我认为我的正则表达式可能是错误的。任何帮助将不胜感激。
谢谢
詹姆斯
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 file. 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'^
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
,'homepage.views.viewHome'),
(r'projects/
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
, list_detail.object_list, projects_list_info),
(r'^projects/(?P<slug>[-\w]+)/
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
, list_detail.object_detail, projects_detail_info),
(r'^site_media/(?P<path>.*)
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
, '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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您已使用projects_detail_info 字典中的裸值“slug”覆盖了来自URL 的slug。从字典中删除该条目。
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.