»
S
I
D
E
B
A
R
«
WriteBite's Django Architecture
February 11th, 2010 by Ahmed

WriteBite is going open-source soon! Follow us on GitHub to track the code development of the site. Once we publish our project code, we hope others will use our model to make other interesting writing contest sites. The possibilities are endless.

The following document is an introduction to WriteBite’s Django-powered architecture. As the site’s code base grows, we’ll be updating this page as necessary.

Introduction to Django and the MTV

If you are unfamiliar with Django’s MTV setup (model-template-view), you should read this intro to Django.  From here on this document assumes familiarity with Django’s MTV setup.

Models:

  • Person: Extends Django’s User model and stores user info about the user, such as username, email address, join date, and much more. Note that user’s password is not stored in the model because authentication is done using Django User model and built-in authentication.
  • Rant: Stores rants. This includes their content, title, author, publication status, and url. The url field is basically the rant’s title processed as URL friendly (see makeRant). The rant content is stored as raw HTML so that it can be outputted with formatting latter.
  • CommentRant: Simple stores comments made on RANTS.
  • ComentUser: Stores comments made on users.
  • VoteRant: Keeps track of user’s voting record for rants. This is to avoid users from voting more than once on a rant.
  • VoteUser: Keeps track of user’s voting record for USERS. Users will be able to rate other users in the future.
  • VoteComment: Keeps track of user’s voting record for comments. Users will be able to rate comments in the future.
  • Topic: Stores the weekly topic, its publication date, and its winners. Note that weekly topics are updated manually via the site’s admin panel.

Main Templates:

  • base.html: This is our base template. All other templates inherit this one. It contains blocks for the title, header, top-right navigation (based on whether the user is logged in or not), body, footer. These content blocks can be replaced by other templates that inherit base.html.
  • index.html: Our home page. Displays top 5 rants and some information about last week’s topic and winners.
  • login.html: Displays a form to login the user (username and password)
  • register.html: Displays a form to register the user (username, password and email)
  • singlerant.html: Displays a single rant with metadata.
  • makerant.html: Displays form (TinyMCE) for rant creation.
  • profile.html: Displays user profile.

Views:

  • index: Handles processing for the main page, such as getting the top 5 rants from the DB and returning them. It also returns data about last week’s topic and contest winners.
  • register: Handles registration forms. Creates a new User and Person using built-in Django views with some customization for our Person model.
  • makeRant: Processes and adds rants to the DB. Processing including checking for a valid form and parsing the rant title into a clean url (no spaces).
  • singleRant: Handles the display of a single rant. Also displays rants comments.
  • voteRant: Handles voting for a rant. Checks to see if user has voted before. If not, let them vote and change the rant’s rating.
  • logout_view: Logs out the user with a Django function.

Note: I used the a built-in decorator called @login_required to check to see if a user is logged in before allowing voting and rant creation.

Other Python Files: There are a few other default .py files that come with Django and are not of interest to this document. Their are however three custom .py files:

  • forms.py
  • auth_login.py
  • auth_backends.py

See urls.py for an insight as to how our urls are mapped to our views. Simply put, a regex check is done on urls and when a match is found, python is sent to a view, sometimes with strings from urls being passed into views as parameters (other times the request object contains all the data that the view needs, i.e. POST data)


One Response  
Matthew C. Kriner writes:
March 6th, 2010 at 3:37 pm

Thanks for your article. I am new at django and this is a big help.


Leave a Reply

»  Substance: WordPress   »  Style: Ahren Ahimsa