2011-11-04 34 views
1

我只是想在某一年的某個月裏列出我的博客帖子,但沒有任何帖子顯示。當我輸入正確的網址:2011/nov時,沒有帖子顯示,我有2011年11月的帖子保存在我的管理員。爲什麼Django 1.3中的date_based.archive_month不能顯示我的博客文章?

另外,出於某種原因,我的CSS文件沒有被考慮在內。當我去到網址,2011 /新手,我只是沒有造型和我的帖子沒有HTML。我在這裏做錯了什麼?

#models.py 
class Post(models.Model): 
    title = models.CharField(max_length=120) 
    slug = models.SlugField(max_length=120, unique = True) 
    body = models.TextField() 
    published = models.DateTimeField(default=datetime.now) 
    categories = models.ManyToManyField(Category) 

    def __unicode__(self): 
     return self.title 

#urls.py 
info_dict = { 
    'queryset': Post.objects.all(), 
    'date_field': 'published', 
} 

urlpatterns = patterns('', 
    (r'^(?P<year>\d{4})/(?P<month>[a-z]{3})/$', 
    date_based.archive_month, 
    dict(info_dict, template_name='blog/archive.html',)), 

#blog/archive.html 
<link href="../static/style.css" rel="stylesheet" type="text/css" media="screen" /> 
. 
. 
. 
{% for post in post_list %} 
<h3 class="title"><a href="{% url single_post slug=post.slug %}">{{post.title}}</a>  
</h3>       
{% endfor %} 

回答

1
  1. CSS未顯示,因爲您將其定義爲../static/style.css。當地址是/2011/jan瀏覽器試圖從/2011/static/style.css獲得css。修復:將路徑設置爲絕對路徑:/static/style.css

  2. 您應該循環使用名爲object_list而不是post_list的對象。

    {% for post in object_list %}

+0

謝謝!它現在正在工作,但我怎麼不得不將它稱爲object_list?我嘗試設置'template_object_name':'post_list',但這也不起作用。 – djblue2009

0

除了什麼pastylegs上面寫的,你也應該改變這一行

published = models.DateTimeField(default=datetime.now) 

這樣:

published = models.DateTimeField(auto_now_add=True) 

在Python,named arguments are only evaluated once。這意味着默認的published值是您的服務器上次編譯models.py文件的時間。 Django爲此提供了一個解決方案:設置參數auto_now_add=True,使django在首次創建時使用實際當前時間。同樣,設置auto_now=True可以讓django在保存時設置字段的當前時間。

+0

感謝小費 - 得到它現在的工作! – djblue2009

相關問題