Constant Contact Labs Developer Blog

  • Rapid Development with Django Posted Monday, December 28, 2009 Huan Lai 1 Comment

    Django is an open source Python web framework that uses a variation of the MVC architectural pattern, called MTV for Model-Template-View. Having used Django for quite a few projects now, I have grown rather fond of it, despite some annoying aspects. Development using Django is very easy and painless, allowing for rapid prototyping and development.

    Django can run on top of a variety of standard web servers, such as Apache, lighttpd and Cherokee. It also ships with a development web server, but that is for testing purposes only. I personally used Apache with mod-wsgi, instructions on how to set that up here. Django doesn’t serve media files itself; it leaves that to whichever web server you choose. It is reccommended that the media isn’t served up by the same web server, but I made them both live on the same server.

    Project Files

    Other than a few configuration differences inside the main settings.py file, Django can use almost any sort of database without any differences in terms of actually coding the application. The settings.py file will have all of the database information, such as type and path and authentication credentials and the like. I will talk more about accessing the database in a little bit. Inside the settings.py is also where a list of all the “installed_apps” is kept. An app is a Web application that does something—e.g., a weblog system, a database of public records or a simple poll app. A project is a collection of configuration and apps for a particular Web site. A project can contain multiple apps. An app can be in multiple projects. http://docs.djangoproject.com/en/dev/intro/tutorial01/?from=olddocs is a very good tutorial that I used to get my feet wet with django

    Also in the root directory is urls.py which has a list of url patterns in regex and the app, and the function within that app’s view, that they each associate with.

    The manage.py file inside the main directory is used for many purposes. python manage.py startapp [appname] can be used to automatically generate a new app directory with some pre-created files. python manage.py sql [appname]... can be used to turn the model definitions within each app and turn that into CREATE TABLE sql statements. python manage.py syncdb can be used to automatically update the database that is assoicated with project to the model definitions, used after the manage.py sql command has been run for all of the apps.

    App Files

    When an app gets created by manage.py, a few files will be automatically created for you. models.py is where all of the definitions for the object-relational mapping is declared. views.py is where the handlers for each url request is declared. While one could have the entire output to the client stored in the views.py, it is generally best practice to use the view to do all of the database and computational work, and then pass off the presentation to the tempates. The tutorial link that I included earlier will have much more detail on how that works.

    Templates

    Templates are basically html files that can also include {{ }} or {% %} directives. They’re responsible for all of the presentation.

    Gripes

    • No support for composite (multi-column) primary keys.
    • Incompatibility between versions, leading to a rather annoying upgrade process.
    • Raw SQL queries are possible, when the OR mapper simply doesn’t cut it. This is a great feature, but remember that when Django creates the tables for you, it will change the names of some of your attributes and tables.
    • While django comes with a method of auto-generating models from an existing database, the integration is not perfect. The ideal solution is to design the schema from the ground up using Django, but this isn’t always a possible solution.
     
    The opinions expressed here represent those of the author and not those of Constant Contact, Inc. Read Blog Terms
    Next Post Previous Post
     

Comments (1) +comment on this post
 

  • DJ flower | 12:43 AM December 23, 2011

    yea soy DJ

Add your comment below

Remember me

Please enter the word you see in the image below:


*  Please be aware that all comments are moderated.

Interested in a particular topic?

If there are specific topics you’d like to see us discuss on our blog or other ideas you’d like to share, please let us know. Click here to contact us.