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/models.py:
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, prepopulate_from=['title'], unique_for_month='date_created') 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, self.date_created.month, self.slug) def __str__(self): return self.title class Meta: ordering = ["-date_created"] class Admin: pass
Update the database
- List the SQL commands Django would use the create the database tables:
$ cd ~/src/django/myblogsite/ $ python manage.py sqlall myblogapp
BEGIN; 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"); COMMIT; - 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 }}
<hr>
<div class="post_footer">
Author: {{ post.author.first_name }}<br>
Date created: {{ post.date_created.date }}<br>
{% ifnotequal post.date_modified.date post.date_created.date %}
Last modified: {{ post.date_modified.date }}<br>
{% endifnotequal %}
Tags:
{% for tag in post.get_tag_list %}
<a href="/blog/tag/{{ tag }}/">{{ tag }}</a>{% if not forloop.last %}, {% endif %}
{% endfor %}
<br>
<a href="/admin/myblogapp/post/{{ post.id }}">Edit post</a>
</div>
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.
3
Comments
—
Comments feed for this post
Post a comment
About
I'm Eliot and this is my notepad for programming topics such as Python, Django, Ubuntu, Emacs, etc... more »
Search Blog
Tags
-
algorithms
(4)
-
aws
(8)
-
blogproject
(20)
-
c_cplusplus
(12)
-
cardstore
(8)
-
colinux
(2)
-
concurrency
(9)
-
conkeror
(2)
-
cygwin
(18)
-
datastructures
(15)
-
datetime
(3)
-
dell
(3)
-
django
(39)
-
emacs
(20)
-
files_directories
(10)
-
install_setup
(7)
-
javascript
(3)
-
keyboard
(6)
-
matplotlib
(5)
-
mercurial
(4)
-
nginx
(2)
-
preferences
(8)
-
processes
(3)
-
pyqt
(18)
-
python
(122)
-
ratpoison
(3)
-
regexes
(5)
-
rsync
(3)
-
softwaretools
(17)
-
sql
(13)
-
ssh
(7)
-
subversion
(6)
-
twisted
(6)
-
ubuntu
(60)
-
urxvt
(5)
-
vxworks
(25)
-
webservices
(4)
-
wmii
(7)
Blogroll
- Adam Gomaa
- Alex Clemesha
- Amir Salihefendic
- Armin Ronacher
- David Beazley
- David Ziegler
- Duncan McGreggor
- Gareth Rushgrave
- Glyph Lefkowitz
- Guido van Rossum
- Ian Bicking
- Jacob Kaplan-Moss
- James Bennett
- James Tauber
- Jesper Noehr
- Matt Harrison
- Nikolay Kolev
- Parand Darugar
- Peter Baumgartner
- Peter Bengtsson
- Rob Hudson
- Simon Willison
- Will McGugan
#1 jim commented on 2008-08-04:
cool information.