author_profile_image

mariangela

Updated on August 01, 2024

Category:

Django

Tags:

Django

startproject

development server

manage.py

Create a new project in Django

We are going to create a very simple Django application to sell some product. It will only have one page from where customers can buy our products, and the admin panel.

In a previous tutorial, we had created the directory mysite and installed our virtual environment env.

01_linux_list_directories

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.

02_django_create_new_project

Let’s look at what startproject created:

03_django_startproject_files

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
04_run_django_development_server

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.

05_django_development_server_success

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.