2012-03-21 148 views
0

我想讓我的博客帖子返回一個固定鏈接,以便我可以將它們ping到博客directores,但是當我調用永久鏈接功能時,我創建的總是有錯誤。我從googleing/stackoverflowing中找到的所有內容都會得到一個或另一個錯誤!Django永久鏈接不工作博客應用程序

最常見的錯誤我得到的回覆是:

Reverse for 'viewpost' with arguments '()' and keyword arguments '{'slug': u'site-redesign'}' not found. 

我給這家誤差模型:

class BlogPost(models.Model): 
    author = models.ForeignKey(User, related_name='posts') 
    title = models.CharField(max_length=200) 
    slug = models.SlugField(max_length=200) 
    ... 
    @models.permalink 
    def get_absolute_url(self): 
     return ('viewpost',(), { 'slug':self.slug}) 

我的urls.py:

urlpatterns = patterns('blog.views', 
    ... 
    (r'^post/(?P<page_slug>[a-zA-Z0-9-]+)/$', 'viewpost'), 
    ... 
) 

如果我打印: reverse('blogPost',kwargs = {'slug':self.slug})我得到了正確的url。

我在做什麼錯。

我通過調用函數:

signals.post_save.connect(
    ping_directories(content_attr = 'body', url_attr = 'get_absolute_url'), sender = BlogPost, weak = False) 

如果您需要任何更多的信息,請讓我知道。

--edit更多的意見

#pages 

(r'^post/(?P<page_slug>[a-zA-Z0-9-]+)/$', 'viewpost'), 

(r'^category/(?P<category_slug>[a-zA-Z0-9-]+)/page/(?P<page_number>[0-9]+)/$', 'viewcategories'), 
(r'^category/$', 'listcategories'), 

(r'^tag/(?P<tag_slug>[a-zA-Z0-9-]+)/page/(?P<page_number>[0-9]+)/$', 'viewtag'), 
(r'^tag/$', 'listtags'), 

(r'^category/(?P<category_slug>[a-zA-Z0-9-]+)/$', 'viewcategories'), 
(r'^tag/(?P<tag_slug>[a-zA-Z0-9-]+)/$', 'viewtag'), 
(r'^comments/', include('django.contrib.comments.urls')), 

(r'^page/(?P<page_number>[0-9]+)/$', 'index'), 
(r'^$', 'index'), 
) 

和觀點:

def viewpost(request, page_slug): 
    blog_post = get_object_or_404(BlogPost, post_status = 'published', slug=page_slug) 
    path = settings.BLOG_PATH 

    return render_to_response("blog/detail.html", { "post": blog_post, "path":path}, context_instance=RequestContext(request)) 

回答

1

你叫url和蛞蝓的kwargs page_slug在permlink

+0

請問我只是做:回報( 'viewpost',()) – Designer023 2012-03-21 20:27:01

+2

'回報( 'viewpost',(){ 'page_slug':self.slug})' – John 2012-03-21 20:31:58

+0

與回來:爲'viewpost'反轉參數'()'和關鍵字參數'{'page_slug':u'site-redesign'}'找不到。 – Designer023 2012-03-21 20:33:22

0

原來,方法調用我的get_absolute_url還需要額外的回調,所以我被誤導了。抱歉的混亂!違規代碼:

signals.post_save.connect(ping_directories(content_attr = 'body', url_attr = 'get_absolute_url', feed_url_fun=lambda x: 'http://example.com/feed/address/here/'), sender = BlogPost, weak = False) 
相關問題