In a previous tutorial, we had created the directory mysite and installed our virtual environment env.
From the command line, cd into mysite. We are going to create our Django project here but before doing that we need to activate the virtual env:
cd mysite
source env/bin/activate
Create your project with Django’s startproject command
To create a Django project run the following command:
django-admin startproject mysite
ls -la
I’ve called my project mysite like the outer directory but the two directories can have different names.
Let’s look at what startproject created:
As per the Django Documentation, these files are:
- The outer mysite/ root directory is 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.
- 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.
- mysite/settings.py: Settings/configuration for this Django project.
- mysite/urls.py: a “table of contents” of your site’s URLs.
- mysite/asgi.py: An entry-point for ASGI-compatible web servers to serve your project.
- mysite/wsgi.py: An entry-point for WSGI-compatible web servers to serve your project.
Django’s development server
Let’s verify that our new project works.
Change into the outer mysite directory (i.e. the directory at the same level of the env directory) and run the following commands:
python3 manage.py runserver
Ignore the warning about unapplied database migrations for now; we’ll deal with them shortly.
Now visit http://127.0.0.1:8000/ and if you see a page with a rocket taking off, it means that everything is working.
Do not use this server in a production environment. It’s intended only for use while developing.
The development server automatically reloads Python code for each request. You don’t need to restart the server for code changes to take effect. However, some actions like adding files don’t trigger a restart, so you’ll have to restart the server in these cases.