Creating Django Apps

I’ve been doing a bit of work with the Django framework lately, and because I’m a total noob at it, Derek has been nice enough to hold my hand throughout the dev process.

Django relies on the MVC design pattern, so you’ll want to modularize your components by creating Django apps.  From the Django book:

if you’re using Django’s database layer (models), you must create a Django app. Models must live within apps. Thus, in order to start writing our models, we’ll need to create a new app.

The Django book is actually quite nice and useful, so if you’re new to Django, I recommend reading it.  I still haven’t gotten through the whole thing myself, but I’ve got Derek :P

The steps to setup a Django app is actually quite long, so I made a nice little cheatsheet to help me remember the steps, so I’d though I’d share:

  1. Open up the console and cd into your project folder.  To create the template files for your new app, you need to run python manage.py startapp [your app's name]
  2. Go into setup.py and add a new entry into the INSTALLED_APPS dictionary to include your new app, like this: '[Project Name].[App Name]',
  3. [OPTIONAL] Inside your new app’s folder, create a subdirectory to hold all your html templates.  By convention I called it templates
  4. In setup.py update the TEMPLATE_DIRS dictionary to include your html templates like this: CUR_DIR + '[App Name]/templates', where CUR_DIR = os.getcwd() + os.setp()
  5. If necessary, define the link to your application’s view in urls.py by modifying urlpatterns

You’ll still need to create the html templates and define views for your application, but that’s basically how to start a new app in a nutshell.

Let me know if I missed out any steps (or if something’s just plain wrong:S)

This entry was posted in Django, python, Tech and tagged , , , . Bookmark the permalink. Post a comment or leave a trackback: Trackback URL.

Post a Comment

Your email is never published nor shared. Required fields are marked *

*
*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>