WDTUTORIALS

Django - How To Create A Minimal Django Project

Tutorial on how to create a minimal Django project and run the development server.

Quickstart

mkdir project && cd project
python3 -m venv venv
source venv/bin/activate
pip install django
django-admin startproject main .
python manage.py runserver
django-admin startapp base
django-admin startapp blog

Longer Version

Create the project folder:

mkdir project && cd project

Create a virtual environment:

python3 -m venv venv

This creates the environment in the venv folder. Note that in this example we created the venv folder in the same folder with the source code of the project. If you are using git, make sure you add the venv folder in the .gitignore file. Check How To Create Virtual Environments to learn more about keeping project packages in isolated environments.

Activate the virtual environment:

source venv/bin/activate

You should now see (venv) in the terminal to indicate that the environment is active.

Install Django:

pip install django

This will download the latest Django package inside the activated virtual environment. (project/venv/lib/python3.6/site-packages/django)

pip is a package manager for Python.

Use pip freeze > requirements.txt to store package information in the requirements file and move that file to the testing / production environment with git. In the target environment run pip install -r requirements to install the packages listed in that file.

Create the project file structure:

django-admin startproject main .

This creates the basic stucture for a Django project. main is the main app that contains things like the project settings.py file.

django-admin is a command line tool for administrative tasks. Each generated Django project has a manage.py file that replicates the django-admin functionality.

Now you should have this kind of file structure:

project
├── main
│   ├── __init__.py
│   ├── settings.py
│   ├── urls.py
│   └── wsgi.py
├── manage.py
└── venv
    ├── bin
    ├── include
    ├── lib
    ├── pip-selfcheck.json
    └── pyvenv.cfg

Run the development server :

python manage.py runserver

Visit http://127.0.0.1:8000/ to see the welcome page.

Use this server just for development. It’s not suitable for production.

Start creating apps for the project:

django-admin startapp base
django-admin startapp blog

Now you should have a folder stucture like this:

project
├── base
├── blog
├── main
├── manage.py
└── venv

Consider creating a re-usable base app for base templates and other global files (like css and js).

Samuli Natri (Entrepreneur, WDTutorials.com founder) has been building websites since the late 90's. He attended Helsinki University Of Technology (Computer Science) and Helsinki University (Social Sciences).

Latest Tutorial CKEditor Tutorial With CodeSnippet Syntax Highlighting

Copyright @ 2018 WDTutorials.com. All rights reserved. Privacy policy.Terms Of Service.