2. The Django Project

The Django Project

  • The Web framework for perfectionists (with deadlines). 
  • Django makes it easier to build better Web apps more quickly and with less code. 
  • Django is a high-level Python Web framework that encourages rapid development and clean, pragmatic design. 
  • It lets you build high-performing, elegant Web applications quickly. 
  • Django focuses on automating as much as possible and adhering to the DRY (Don't Repeat Yourself) principle. 
  • See Django

Basic Startup Tutorial


1. Install Database of choice (e.g. MySQL, PostGresSQL)
2. Install Django:

    pip install Django

Writing your first Django app, part 1

Tutorial can be found here:


Throughout this tutorial, we’ll walk you through the creation of a basic poll application.
It’ll consist of two parts:
  • A public site that lets people view polls and vote in them.
  • An admin site that lets you add, change, and delete polls.

Create a Project

If this is your first time using Django, you’ll have to take care of some initial setup. Namely, you’ll need to auto-generate some code that establishes a Django project – a collection of settings for an instance of Django, including database configuration, Django-specific options and application-specific settings.

From the command line, cd into a directory where you’d like to store your code, then run the following command:

django-admin startproject mysite

This will create a mysite directory in your current directory.
Let’s look at what startproject created:

mysite/
    manage.py
    mysite/
        __init__.py
        settings.py
        urls.py
        wsgi.py

These files are:
  • The outer mysite/ root d-irectory is just a container for your project. Its name doesn’t matter to Django; you can rename it to anything you like.
  • manage.py: A command-line utility that lets you interact with this Django project in various ways. You can read all the details about manage.py in django-admin and manage.py.
  • The inner mysite/ directory is the actual Python package for your project. Its name is the Python package name you’ll need to use to import anything inside it (e.g. mysite.urls).
  • mysite/__init__.py: An empty file that tells Python that this directory should be considered a Python package. If you’re a Python beginner, read more about packages in the official Python docs.
  • mysite/settings.py: Settings/configuration for this Django project. Django settings will tell you all about how settings work.
  • mysite/urls.py: The URL declarations for this Django project; a “table of contents” of your Django-powered site. You can read more about URLs in URL dispatcher.
  • mysite/wsgi.py: An entry-point for WSGI-compatible web servers to serve your project. See How to deploy with WSGI for more details.

The Development Server


Let’s verify your Django project works. Change into the outer mysite directory, if you haven’t already, and run the following commands:

py manage.py runserver

Some messages appear including:

Starting development server at http://127.0.0.1:8000/
Quit the server with CONTROL-C.

Now’s a good time to note: don’t use this server in anything resembling a production environment. It’s intended only for use while developing. (We’re in the business of making Web frameworks, not Web servers.)

Now that the server’s running, visit http://127.0.0.1:8000/ with your Web browser. You’ll see a “Congratulations!” page, with a rocket taking off. It worked!

Changing the port

By default, the runserver command starts the development server on the internal IP at port 8000.

If you want to change the server’s port, pass it as a command-line argument. For instance, this command starts the server on port 8080:

/ 
...\> py manage.py runserver 8080









Comments