Django Year/Month based posts archive

前端 未结 3 424
情话喂你
情话喂你 2021-02-06 12:04

i\'m new to Django and started an application, i did the models, views, templates, but i want to add some kind of archive to the bottom of the page, something like this http:/

相关标签:
3条回答
  • 2021-02-06 12:31

    You might want to consider starting with a generic view and building off that.

    0 讨论(0)
  • 2021-02-06 12:34

    Ok... so the final code that works for me is:

    in view:

     rom_months = ['Ian', 'Feb', 'Mar', 'Apr', 'Mai', 'Iun', 
    'Iul', 'Aug', 'Sept', 'Oct', 'Noi', 'Dec']
    
    def arhiva(request):
        arch = Post.objects.dates('data', 'month', order='DESC')
    
        archives = {}
    
        for i in arch:
            year = i.year
            month = i.month
            try:
                archives[year][month-1][1] = True
            except KeyError:
    
                archives[year]=[[datetime.date(year,k+1,1),False,rom] for k, rom in enumerate(rom_months)]
                archives[year][month-1][1] = True
    
        return render_to_response('blog/arhiva.html', {'archives':sorted(archives.items(),reverse=True)})
    

    and in template:

    {% for year, month_list in archives %}
        {{ year }} Arhive: 
        {% for month, has_link, rom_month in month_list %}
            {% if has_link %}<a href="/{{ month.year }}/{{ month.month }}/">{% endif %}
                 {{ rom_month }}
            {% if has_link %}</a>{% endif %} 
        {% endfor %}
        <br />
    {% endfor %}
    

    and the result:

    2009 Arhive: Ian Feb Mar Apr Mai Iun Iul Aug Sept Oct Noi Dec 
    2008 Arhive: Ian Feb Mar Apr Mai Iun Iul Aug Sept Oct Noi Dec 
    2007 Arhive: Ian Feb Mar Apr Mai Iun Iul Aug Sept Oct Noi Dec 
    2003 Arhive: Ian Feb Mar Apr Mai Iun Iul Aug Sept Oct Noi Dec 
    

    Thanks a lot again for help. You're the best! I'm the n00b! :)

    0 讨论(0)
  • 2021-02-06 12:47

    Firstly, the datetime format strings are given in the django docs. I think you want capital instead of lowercase 'M'.

    Since you want to display all 12 months of a year, even if only some have posts, we'll create an archives object to pass to the template. I've chosen to use a dictionary where

    • the keys are the years
    • the values are a list of 12 [datetime, bool] pairs, where datetime represents a month, and bool is True if there are posts for that month.

    Here's how we build the archives object in the view.

    from datetime import date
    
    def archive(request):
        arch = Post.objects.dates('date', 'month', order='DESC')
    
        archives = {}
    
        for i in arch:
            year = i.year
            month = i.month
            try:
                archives[year][month-1][1]=True
            except KeyError:
                # catch the KeyError, and set up list for that year
                archives[year]=[[date(y,m,1),False] for m in xrange(1,13)]
                archives[year][month-1][1]=True
    
        return render_to_response('blog/arhiva.html', 
                  {'archives':sorted(archives.items(),reverse=True)})
    

    In the template, we loop through the months for each year, and display the link if appropriate.

    {% for year, month_list in archives %}
      {{ year }} archives: 
      {% for month, has_link in month_list %}
        {% if has_link %}<a href="/{{ month.year }}/{{ month.month }}/">{% endif %}
          {{ month|date:"M" }}
        {% if has_link %}</a>{% endif %}
      {% endfor %}
    {% endfor %}
    

    I haven't checked all the code so there might be a couple of bugs. It would be better to use the url template tag for the link, instead of hardcoding the url format. I have a feeling my answer might be overly complicated, but I've spent a while typing it up, so I may as well share it with the world.


    Internationalization

    I haven't used the internationalization features of Django, so I can't really help with the translation. I recommend you have a look at the documentation, and ask another question if there's a particular bit you don't understand.

    Having said that, if you want to display the months is Romanian only, here's an ugly way to do it.

    First, add the following line to the top of your archive function in the view.

    rom_months = ['Ian', 'Feb', 'Mar', 'Apr', 'Mai', 'Iun', 
                  'Iul', 'Aug', 'Sept', 'Oct', 'Noi', 'Dec']
    

    Then substitute the following line into your view

    archives[year]=[[date(y,k+1,1),False,rom] for k, rom in enumerate(rom_months)]
    

    Finally substitute the following into the template

    ...
    {% for month, has_link, rom_month in month_list %}
      {% if has_link %}<a href="/{{ month.year }}/{{ month.month }}/">{% endif %}
      {{ rom_month }}
    ...
    
    0 讨论(0)
提交回复
热议问题