Creating a news archive in Django

I want to create a news archive in python / django, I have no idea where to start with. I need a point of view to pull out all the news articles and I did that, then I need to split them in months and years, for example,

September 09 October 09

Then I need that every time a new news article is created for a new month it needs to output a new month, so if a news article was written in November then the archive will be

September 09 Oct. 09 Nov 09

Any help?

+2


source to share


3 answers


A great place to start is James Bennett's Practical Django Projects . Among other things, it will help you design a web blog with multiple temporary views (monthly, etc.) that should serve you as the basis for your application.



+3


source


One option you can try is to create a custom manager for your model that will give you the option to pull the archives. Here's the code I'm using:

from django.db import models, connection
import datetime

class EntryManager(models.Manager):
    def get_archives(self, level=0):
        query = """
                    SELECT
                        YEAR(`date_posted`) AS `year`,
                        MONTH(`date_posted`) AS `month`,
                        count(*) AS `num_entries`
                    FROM
                        `blog_entry`
                    WHERE
                        `date_posted` <= %s
                    GROUP BY
                        YEAR(`date_posted`),
                        MONTH(`date_posted`)
                    ORDER BY
                        `year` DESC,
                        `month` DESC
                """
        months = ('January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December')
        cursor = connection.cursor()
        cursor.execute(query, [datetime.datetime.now()])
        return [{'year': row[0], 'month': row[1], 'month_name': months[int(row[1])-1], 'num_entries': row[2]} for row in cursor.fetchall()]

      

Of course you need to attach it to the model with:



objects = EntryManager()

      

Returns a list of dictionaries containing the year, numeric month, month name, and number of entries. You call it the following:

archives = Entry.objects.get_archives()

      

+3


source


It looks like you are trying to make one view that pulls in the data and then try to order it by date ect. I don't think this would be the best way to do it.

Instead, you can pretend to show articles of the month. I don't know your models, but something like:

articles = ArticleModel.objects.filter(date__month=month, date__year=year)

      

the month and year you get from your url, fx archive / 2009/9.

If you want to make sure that you only show your links to content archives, the simple solution is to get all articles and mark the months containing content. There must be a better way to do this, but if you put it in middleware and cache it, it shouldn't be a problem.

0


source







All Articles