问题
I've been struggling with setting up views / urls in Django for a couple of days. Struggling to get my head around it everyone seems to do it slightly differently.
What i want to achieve is:
I want to have a blog where i can post news posts on the site that will be located at example - mysite.com/blog/ then you can click through to view the posts individually it pulls the slug through from each post. (which pretty much works).
However I want to pull the posts from the blog app so the homepage. So you can view a snippet of the latest posts. I now understand that I need to create a view and a URL outside of the app folder and in the main directory. However i'm struggling to get them to link together. I am currently getting the error message displayed above. 'No Blog matches the given query.' Here is my code for the URL's, Models and the 2 different view folders.
//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')),
)
//MAIN VIEW FOR HOMEPAGE
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))
//VIEWS FOR BLOG 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))
//MODELS FROM THE BLOG 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']
If you need anymore information feel free to ask! I'm new to all this so go easy! :) Thanks in advance!
回答1:
Change
url(r'$', blog),
To
url(r'$', 'views.blog_index', name='index'),
Or write a separate view.
The reason you're getting your error, is because you're attempting to execute the blog
function which expects a slug
from your title page. What you're wanting to do is show the index
from your title page which does not take a slug
.
Also, the following is going to cause you pain:
from blog.views import blog_index, blog
from views import blog_index
Which blog_index
do you want to be using? You're better off using 'views.blog_index'
notation in your URLs. Delete those imports above, and only use string based view names in your URLs like you've done for blog/
and blog_index/
.
Edit: this is what your entire URLs should show (to get this working..)
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')),
)
来源:https://stackoverflow.com/questions/11091330/exception-value-blog-takes-exactly-2-arguments-1-given