Django


This draft deletes the entire topic.

expand all collapse all

Examples

  • 54

    Django is a web development framework based on Python. Django 1.10 (the latest stable release) requires Python 2.7, 3.4 or 3.5 to be installed. Assuming pip is available, installation is as simple as running the following command:

    $ pip install django
    

    Web applications built using Django must reside within a Django project. You can use the django-admin command to start a new project in the current directory:

    $ django-admin startproject myproject
    

    where myproject is a name that uniquely identifies the project and can consist of numbers, letters, and underscores.

    This will create the following project structure:

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

    To run the application, start the development server

    $ cd myproject
    $ python manage.py runserver
    

    Now that the server’s running, visit http://127.0.0.1:8000/ with your web browser. You’ll see the following page:

    enter image description here

    By default, the runserver command starts the development server on the internal IP at port 8000. This server will automatically restart as you make changes to your code. But in case you add new files, you’ll have to manually restart the server.

    If you want to change the server’s port, pass it as a command-line argument.

    $ python manage.py runserver 8080
    

    If you want to change the server’s IP, pass it along with the port.

    $ python manage.py runserver 0.0.0.0:8000
    

    Note that runserver is only for debug builds and local testing. Specialised server programs (such as Apache) should always be used in production.

    Adding a Django App

    A Django project usually contains multiple apps. This is simply a way to structure your project in smaller, maintainable modules. To create an app, go to your projectfolder (where manage.py is), and run the startapp command (change myapp to whatever you want):

    python manage.py startapp myapp
    

    This will generate the myapp folder and some necessary files for you, like models.py and views.py.

    In order to make Django aware of myapp, add it to your settings.py:

    # myproject/settings.py
    
    # Application definition
    INSTALLED_APPS = [
        ...
        'myapp',
    ]
    

    The folder-structure of a Django project can be changed to fit your preference. Sometimes the project folder is renamed to /src to avoid repeating folder names. A typical folder structure looks like this:

    directory structure

  • 35

    Although not strictly required, it is highly recommended to start your project in a "virtual environment." A virtual environment is a container (a directory) that holds a specific version of Python and a set of modules (dependencies), and which does not interfere with the operating system's native Python or other projects on the same computer.

    By setting up a different virtual environment for each project you work on, various Django projects can run on different versions of Python, and can maintain their own sets of dependencies, without risk of conflict.

    Python 3.3+

    Python 3.3+ already includes a standard venv module, which you can usually call as pyvenv. In environments where the pyvenv command is not available, you can access the same functionality by directly invoking the module as python3 -m venv.

    To create the Virtual environment:

    $ pyvenv <env-folder>
    # Or, if pyvenv is not available
    $ python3 -m venv <env-folder>
    

    Python 2

    If using Python 2, you can first install it as a separate module from pip:

    $ pip install virtualenv
    

    And then create the environment using the virtualenv command instead:

    $ virtualenv <env-folder>
    

    Activate (any version)

    The virtual environment is now set up. In order to use it, it must be activated in the terminal you want to use it.

    To 'activate' the virtual environment (any Python version):

    $ source <env-folder>/bin/activate
    

    This changes your prompt to indicate the virtual environment is active. (<env-folder>) $

    From now on, everything installed using pip will be installed to your virtual env folder, not system-wide.

    To leave the virtual environment use deactivate :

    (<env-folder>) $ deactivate
    

    Alternatively: use virtualenvwrapper

    You may also consider using virtualenvwrapper which makes virtualenv creation and activation very handy as well as separating it from your code:

    # Create a virtualenv
    mkvirtualenv my_virtualenv
    
    # Activate a virtualenv
    workon my_virtualenv
    
    # Deactivate the current virtualenv
    deactivate
    

    When using virtualenvs, it is often useful to set your PYTHONPATH and DJANGO_SETTINGS_MODULE in the postactivate script.

    #!/bin/sh
    # This hook is sourced after this virtualenv is activated
    
    # Set PYTHONPATH to isolate the virtualenv so that only modules installed
    # in the virtualenv are available
    export PYTHONPATH="/home/me/path/to/your/project_root:$VIRTUAL_ENV/lib/python3.4"
    
    # Set DJANGO_SETTINGS_MODULE if you don't use the default `myproject.settings`
    # or if you use `django-admin` rather than `manage.py`
    export DJANGO_SETTINGS_MODULE="myproject.settings.dev"
    

    Set your Project Path

    It is often also helpful to set your project path inside a special .project file located in your base <env-folder>. When doing this, everytime you activate your virtual environment, it will change the active directory to the specified path.

    Create a new file called <env-folder>/.project. The contents of the file should ONLY be the path of the project directory.

    /path/to/project/directory
    

    Now, initiate your virtual environment (either using source <env-folder>/bin/activate or workon my_virtualenv) and your terminal will change directories to /path/to/project/directory.

  • 16

    django-admin is a command line tool that ships with Django. It comes with several useful commands for getting started with and managing a Django project. The command is the same as ./manage.py , with the difference that you don't need to be in the project directory. The DJANGO_SETTINGS_MODULE environment variable needs to be set.

    A Django project is a Python codebase that contains a Django settings file. A project can be created by the Django admin through the command django-admin startproject NAME. The project typically has a file called manage.py at the top level and a root URL file called urls.py. manage.py is a project specific version of django-admin, and lets you run management commands on that project. For example, to run your project locally, use python manage.py runserver. A project is made up of Django apps.

    A Django app is a Python package that contains a models file (models.py by default) and other files such as app-specific urls and views. An app can be created through the command django-admin startapp NAME (this command should be run from inside your project directory). For an app to be part of a project, it must be included in the INSTALLED_APPS list in settings.py. If you used the standard configuration, Django comes with several apps of it's own apps preinstalled which will handle things like authentication for you. Apps can be used in multiple Django projects.

    The Django ORM collects all of the database models defined in models.py and creates database tables based off of them. To do this, first, setup your database by modifying the DATABASES setting in settings.py. Then, once you have defined your database models, run python manage.py makemigrations followed by python manage.py migrate to create or update your database's schema based on your models.

Please consider making a request to improve this example.

Remarks

Django advertises itself as "the web framework for perfectionists with deadlines" and "Django makes it easier to build better Web apps more quickly and with less code"

Versions

VersionRelease Date
1.102016-08-01
1.92015-12-01
1.82015-04-01
1.72014-09-02
1.62013-11-06
1.52013-02-26
1.42012-03-23
1.32011-03-23
1.22010-05-17
1.12009-07-29
1.02008-09-03
Still have a question about Getting started with Django? Ask Question

Topic Outline