Using Django As Your Web Framework

djangoDjango is one of a couple of pre-installed applications we currently offer on our virtual private servers , a summary on the django website describes it as “a high-level Python Web framework that encourages rapid development and clean, pragmatic design.”

What this actually means is , its saves you time  , alot of it. It does this by providing you with a large range of pre-installed modules which you can easily use and configure to suit whatever application your looking to build.
As Django is already pre installed on your server with us i can skip that part and explain how you go about how django is set-up and basic settings you might need to get started

First off, there’s a models.py file which defines your data model by extrapolating your single lines of code into full database tables and adding a pre-built (totally optional) administration section to manage content.
The next element is the urls.py file which uses regular expressions to capture URL patterns for processing.
The actual processing happens in your views which, if you haven’t seen the pattern yet, live in views.py. This is really the meat of Django, since views are where you grab the data you’re presenting to the visitor.

Here’s what happens when a visitor lands on your Django page:
1. First, Django consults the various URL patterns you’ve created and uses the information to retrieve a view.
2. The view then processes the request, querying your database if necessary.
3. The view passes the requested information on to your template.
4. The template then renders the data in a layout you’ve created and displays the page.
To start the server and see the django default homepage run the following command on the server command line

/djangoblog/ $ python manage.py runserver

Now open up your browser and head to http:/YOUR-IP:8000/. You should see a page like this:

Djangopart2_1

Activate the admin site

The Django admin site is not activated by default – it’s an opt-in thing. To activate the admin site for your installation, do these three things:

•    Add “django.contrib.admin” to your INSTALLED_APPS setting.
•    Run python manage.py syncdb. Since you have added a new application to INSTALLED_APPS, the database tables need to be updated.
•    Edit your mysite/urls.py file and uncomment the lines below the “Uncomment the next two lines…” comment. This file is a URLconf; we’ll dig into URLconfs in the next tutorial. For now, all you need to know is that it maps URL roots to applications. In the end, you should have a urls.py file that looks like this:

To login to the admin section of your django framework you need to create a superuser

The manage.py createsuperuser command is new.

manage.py syncdb prompts you to create a superuser the first time you run it after adding 'django.contrib.auth' to your INSTALLED_APPS. If you need to create a superuser at a later date, you can use a command line utility:

manage.py createsuperuser --username=joe
--email=joe@example.com

You will be prompted for a password. After you enter one, the user will be created immediately. If you leave off the --username or the --email options, it will prompt you for those values.

Now head to http://YOUR-IP:8000/admin/. Log in with the user/pass combo you created earlier and you should see something like this:

Djangopart2_2

This blog post only contains very basic information to get you up and running , django has a huge range of functionality – check out the django project doc page for a whole lot more information.

You should also be aware with regards using the ‘python manage.py runserver’ command to serve your site publically , this is primarily used during the development phase  , and should not be used when your site goes live as you’ll see significant load issues.


Share and Enjoy:
  • Print
  • Digg
  • del.icio.us
  • Facebook
  • Google Bookmarks
  • LinkedIn
  • StumbleUpon
  • email
  • Ping.fm
  • Posterous
  • Reddit

Tags: , ,

3 Responses to “Using Django As Your Web Framework”

  1. Dave Jeffery says:

    Considering the nature of this blog, it should probably be pointed out that you should *never* use the ‘python manage.py runserver’ command to serve a site to the public. It will choke under very light load and it is designed for use during the development stages only.

    By the way, I found ‘The Django Book’ to be a great resource for getting started: http://www.djangobook.com/

  2. Aidan says:

    Thanks for the Feedback Dave , and agreed on manage.py i’ll edit the blog to include that point :)

Leave a Reply