我一直在努力在Django中设置视图/网址几天。为了解决这个问题,每个人似乎都略有不同。
我想要达到的目标是:
我希望有一个博客,我可以在网站上发布新闻帖子,例如mysite.com/blog/,然后你可以点击查看各个帖子,从每个帖子中拉出slu .. (几乎可以工作)。
但是我想从博客应用程序中提取帖子以便主页。因此,您可以查看最新帖子的片段。我现在明白我需要在app文件夹和主目录之外创建一个视图和一个URL。然而,我正在努力让他们联系在一起。我目前收到上面显示的错误消息。 '没有Blog匹配给定的查询。'这是我的URL代码,模型和2个不同的视图文件夹。
//URLS for app and main directory
import authority
from django.conf import settings
from django.conf.urls import patterns, include, url
from django.conf.urls.defaults import url, include, patterns
from django.contrib import admin
admin.autodiscover()
authority.autodiscover()
urlpatterns = patterns('',
url(r'^admin/doc/', include('django.contrib.admindocs.urls')),
(r'^authority/', include('authority.urls')),
(r'^i18n/', include('django.conf.urls.i18n')),
(r'^admin/', include(admin.site.urls)),
url(r'^$', 'views.blog', name='index'),
url(r'^blog/(?P<slug>[-\w]+)/$', 'blog.views.blog', name="blog"),
url(r'^blog/$', 'blog.views.blog_index', name="blog_index"),
)
if settings.DEBUG:
urlpatterns += patterns('',
# Trick for Django to support static files (security hole: only for Dev environement! remove this on Prod!!!)
url(r'^admin/pages/page(?P<path>.*)$', 'django.views.static.serve', {'document_root': settings.PAGES_MEDIA_ROOT}),
url(r'^media/(?P<path>.*)$', 'django.views.static.serve', {'document_root': settings.MEDIA_ROOT}),
url(r'^admin_media/(?P<path>.*)$', 'django.views.static.serve', {'document_root': settings.ADMIN_MEDIA_ROOT}),
)
urlpatterns += patterns('',
(r'^', include('pages.urls')),
)
//主页视图
from django.http import HttpResponse
from django.shortcuts import render_to_response, get_object_or_404
from django.template import RequestContext
from blog.models import Blog, NewsPost
def blog_index(request):
blogs = Blog.objects.filter(active=True)
return render_to_response('index.html', {
'blogs':blogs,
}, context_instance=RequestContext(request))
//浏览博客APP
from django.http import HttpResponse
from django.shortcuts import render_to_response, get_object_or_404
from django.template import RequestContext
from blog.models import Blog, NewsPost
def blog_index(request):
blogs = Blog.objects.filter(active=True)
return render_to_response('blog/index.html', {
'blogs':blogs,
}, context_instance=RequestContext(request))
def blog(request, slug):
blog = get_object_or_404(Blog, active=True, slug=slug)
return render_to_response('blog/blog_post.html', {
'blog': blog
}, context_instance=RequestContext(request))
//来自博客APP的模特
from django.contrib.auth.models import User
class TimeStampedActivate(models.Model):
created = models.DateTimeField(auto_now_add=True)
modified = models.DateTimeField(auto_now=True)
active = models.BooleanField(default=False, help_text="Controls
whether or now this news post is live")
class Meta:
abstract = True
class Blog(TimeStampedActivate):
title = models.CharField(max_length=255, help_text="Can be
anything up to 255 character")
slug = models.SlugField()
description = models.TextField(blank=True, help_text="Give a short
description of the news post")
content = models.TextField(blank=True, help_text="This is the main
content for the news post")
user = models.ForeignKey(User, related_name="blog")
def __unicode__(self):
return self.title
@models.permalink
def get_absolute_url(self):
return ('blog', (), {
'slug': self.slug
})
class NewsPost(TimeStampedActivate):
title = models.CharField(max_length=255, help_text="title of the post")
slug = models.SlugField()
description = models.TextField(blank=True, help_text="Give a short
description of the news post")
content = models.TextField(blank=True, help_text="This is the main
content for the news post")
publish_at = models.DateTimeField(default=datetime.datetime.now(),
help_text="Choose when the post is visible")
blog = models.ForeignKey(Blog, related_name="posts")
def __unicode__(self):
return self.title
class Meta:
ordering = ['-publish_at', '-modified', '-created']
如果您需要更多信息,请随时提出!我对这一切都很陌生,所以很容易! :)提前致谢!
答案 0 :(得分:1)
更改
url(r'$', blog),
要
url(r'$', 'views.blog_index', name='index'),
或者写一个单独的视图。
您收到错误的原因是因为您尝试执行blog
函数,该函数需要您的标题页中有slug
。您要执行的操作是显示标题页中的index
,但不会显示slug
。
此外,以下内容会让您感到痛苦:
from blog.views import blog_index, blog
from views import blog_index
您想要使用哪个blog_index
?您最好在网址中使用'views.blog_index'
表示法。删除上面的那些导入,并且只在您的网址中使用基于字符串的视图名称,就像您为blog/
和blog_index/
所做的那样。
编辑:这是您的整个网址应显示的内容(以使其正常运行..)
import authority
from django.conf import settings
from django.conf.urls import patterns, include, url
from django.conf.urls.defaults import url, include, patterns
from django.contrib import admin
admin.autodiscover()
authority.autodiscover()
urlpatterns = patterns('',
url(r'^admin/doc/', include('django.contrib.admindocs.urls')),
(r'^authority/', include('authority.urls')),
(r'^i18n/', include('django.conf.urls.i18n')),
(r'^admin/', include(admin.site.urls)),
url(r'^$', 'views.blog', name='index'),
url(r'^blog/(?P<slug>[-\w]+)/$', 'blog.views.blog', name="blog"),
url(r'^blog/$', 'blog.views.blog_index', name="blog_index"),
)
if settings.DEBUG:
urlpatterns += patterns('',
# Trick for Django to support static files (security hole: only for Dev environement! remove this on Prod!!!)
url(r'^admin/pages/page(?P<path>.*)$', 'django.views.static.serve', {'document_root': settings.PAGES_MEDIA_ROOT}),
url(r'^media/(?P<path>.*)$', 'django.views.static.serve', {'document_root': settings.MEDIA_ROOT}),
url(r'^admin_media/(?P<path>.*)$', 'django.views.static.serve', {'document_root': settings.ADMIN_MEDIA_ROOT}),
)
urlpatterns += patterns('',
(r'^', include('pages.urls')),
)