2012-09-28 29 views
1

我在我的網站的單獨頁面上構建了以前的存檔。不過,Django這次似乎不想與我合作。在Django實施每年一次的月度存檔

class IndexView(ArchiveIndexView): 
    template_name = 'cms/blog_base.html' 
    date_field = 'pub_date' 
    latest = '20' 
    model = Entry 

In template {% for year in date_list %} {{ year|date:"Y" }} gives me a date relating to none of my entries. {% for entry in object_list % } {{ entry.pub_date|date:"Y" }} obviously outputs the correct date for the entry but as the entries grow I can only imagine it will continue to duplicate the years and months. 

那麼我做錯了什麼?爲了將日期與我的條目集合關聯起來,我需要在ArchiveIndexView和模板標籤中做些什麼?過去,它們在單獨的頁面上,因此被URL conf中的正則表達式過濾。我看到的一個解決方案是使用一些原始SQL創建自定義管理器,這就是我所看到的?如果是的話,我會重新考慮這一切。預先感謝社區。

更新:例如:我在我的主頁上想要的東西與本頁上的內容類似https://unweb.me/blog/monthly-archives-on-Django我現在也在考慮嘗試他們的解決方案,因爲它看起來像一個不錯的UI/UX。但是,我是一個簡單的人,如果有一個簡單的路線,我會很樂意。

回答

2

一個更好的解決辦法,我已經從一個簡短的教程博客網上找到實現以下。我驗證它的工作原理。

def mkmonth_lst(): 
"""Make a list of months to show archive links.""" 
if not Post.objects.count(): return [] 

# set up vars 
year, month = time.localtime()[:2] 
first = Post.objects.order_by("created")[0] 
fyear = first.created.year 
fmonth = first.created.month 
months = [] 

# loop over years and months 
for y in range(year, fyear-1, -1): 
    start, end = 12, 0 
    if y == year: start = month 
    if y == fyear: end = fmonth-1 

    for m in range(start, end, -1): 
     months.append((y, m, month_name[m])) 
return months 

def main(request): 
"""Main listing.""" 
posts = Post.objects.all().order_by("-created") 
paginator = Paginator(posts, 10) 
try: page = int(request.GET.get("page", '1')) 
except ValueError: page = 1 

try: 
    posts = paginator.page(page) 
except (InvalidPage, EmptyPage): 
    posts = paginator.page(paginator.num_pages) 

return render_to_response("list.html", dict(posts=posts, user=request.user, 
              post_list=posts.object_list, months=mkmonth_lst())) 

# The template info 
    <div id="sidebar"> 
    Monthly Archive 
    <p> 
    {% for month in months %} 
     {% ifchanged month.0 %} {{ month.0 }} <br /> {% endifchanged %} 
     <a href="{% url blog.views.month month.0 month.1 %}">{{ month.2 }}</a> <br /> 
    {% endfor %} 
    </p> 
</div> 

這將在您的主頁上生成一個年份分隔列表,其中僅包含帖子的月份。

+0

這包括所有年/月,而不管帖子。你知道如何讓它只輸出有年份/月份的文章嗎? –

+0

我不得不''進口時間'和'進口日曆'使這項工作。 – woodbine

3

您是否在尋找regroup -feature?

例子:

{% regroup object_list by date_field|date:"Y" as year_list %} 
{% for year in year_list %} 
    {% regroup year.list by date_field|date:"F" as month_list %} 
    {% for month in month_list %} 
     {{ month.grouper }}/{{ year.grouper }} <br /> 
     {{ month.list }} 
    {% endfor %} 
{% endfor %} 
+0

我會試試這個。我認爲問題在於,我想將它全部放在首頁,因此沒有來自urlconf,自定義管理器或我將遇到麻煩的複雜視圖的任何參數。我會嘗試你的解決方案並讓你知道。謝謝:) – eusid

+0

是的,這是做我想做的,但我不知道它將如何表現隨着條目的增長。我只想要一個年份列表(不可點擊),並在下面鏈接鏈接到當年的月份檔案。我假設我之前遇到的問題是我的Entry模型沒有分組到日期中?它顯示了2011年的一個日期,我假設它是object_list中某個對象的日期。我確實在ArchiveIndexView的子類中指定了date_field。我想要的東西就像是在這個頁面的右欄,也想着使用他們的sol。 https://unweb.me/blog/monthly-archives-on-Django – eusid

+0

我使用這個用於約85個用戶的時間記錄系統,每個工作日每個系統都有8個條目。我顯示了最近6個月 - 因此object_list中有大約960個條目。 – Thomas