SaltyCrane Blog — Notes on JavaScript and web development

Django Blog Project #10: Adding support for multiple authors

Here is a quick post on how I added support for multiple users on my blog.

Modfiy the model
Excerpt from ~/src/django/myblogsite/myblogapp/
import re
from django.db import models
from django.contrib.auth.models import User

class Post(models.Model):
    author = models.ForeignKey(User)
    title = models.CharField(maxlength=200)
    slug = models.SlugField(maxlength=200,
    date_created = models.DateTimeField(auto_now_add=True)
    date_modified = models.DateTimeField(auto_now=True)
    tags = models.CharField(maxlength=200, help_text="Space separated.")
    body = models.TextField()
    body_html = models.TextField(editable=False, blank=True)
    lc_count = models.IntegerField(default=0, editable=False)

    def get_tag_list(self):
        return re.split(" ", self.tags)

    def get_absolute_url(self):
        return "/blog/%d/%02d/%s/" % (self.date_created.year,

    def __str__(self):
        return self.title

    class Meta:
        ordering = ["-date_created"]

    class Admin:
Update the database
  • List the SQL commands Django would use the create the database tables:
    $ cd ~/src/django/myblogsite/
    $ python sqlall myblogapp
    CREATE TABLE "myblogapp_post" (
        "id" integer NOT NULL PRIMARY KEY,
        "author_id" integer NOT NULL REFERENCES "auth_user" ("id"),
        "title" varchar(200) NOT NULL,
        "slug" varchar(200) NOT NULL,
        "date_created" datetime NOT NULL,
        "date_modified" datetime NOT NULL,
        "tags" varchar(200) NOT NULL,
        "body" text NOT NULL,
        "body_html" text NOT NULL,
        "lc_count" integer NOT NULL
    CREATE INDEX myblogapp_post_author_id ON "myblogapp_post" ("author_id");
    CREATE INDEX myblogapp_post_slug ON "myblogapp_post" ("slug");
  • Enter the sqlite shell:
    $ sqlite3 mydatabase.sqlite3

    and enter the following statement:
    sqlite> ALTER TABLE myblogapp_post ADD COLUMN author_id integer REFERENCES auth_user (id);
    sqlite> .exit
Update the template
Excerpt from ~/src/django/myblogsite/templates/singlepost.html:
  <h3>{{ post.title }}</h3>
  {{ post.body }}
  <div class="post_footer">
    Author: {{ }}<br>
    Date created: {{ }}<br>
    {% ifnotequal %}
      Last modified: {{ }}<br>
    {% endifnotequal %}
    {% for tag in post.get_tag_list %}
      <a href="/blog/tag/{{ tag }}/">{{ tag }}</a>{% if not forloop.last %}, {% endif %}
    {% endfor %}
    <a href="/admin/myblogapp/post/{{ }}">Edit post</a>

Now you should be able to go in to the Admin interface select a user to associate with each post. Unfortunately, it does not automatically associate the logged in user with the post.

Here is a snapshot screenshot of what I'm calling version 0.1.1. Yeah, I know, I skipped 0.1.0-- I consider that to be the point where I said Goodbye Blogger and Hello Saltycrane.


#1 jim commented on :

cool information.

#2 Jay commented on :

I know this increases logic, but why not break out users to it's own table and link it to Articles? That way you can have more than 2 authors.

#3 Eliot commented on :

I'm not sure what you are talking about. This method uses the Users model from django.contrib.auth. It supports more than 2 authors.