How to set up a Django project

1. Objective:

This procedure outlines the steps to set up a new Django project in a Python virtual environment. The goal is to establish a structured foundation for developing a web application.

2. Steps:

Step 1: Install Django

Activate your virtual environment and run the following command to install Django:

pip install django

Step 2: Create a Django Project

Use the django-admin command to create a new Django project. Replace project_name with your desired project name:

django-admin startproject project_name

Step 3: Navigate to the Project Directory

Move into the directory of the newly created Django project:

cd project_name

Step 4: Verify the Installation

Run the development server to verify that Django is set up correctly:

python manage.py runserver

Open a browser and go to http://127.0.0.1:8000/. You should see the default Django welcome page.

If you are developing on a remote server, remember to allow traffic on port 8000 in your firewall settings. See "How to manage UFW app profiles" for more information. Furthermore, you have to bind the development server to a public IP address by running:

python manage.py runserver 0.0.0.0:8000

In this case, you may also need to modify the ALLOWED_HOSTS setting in settings.py to include your server's IP address or the domain name. In settings.py, add/modify the following line:

ALLOWED_HOSTS = ['your_server_ip', 'your_domain_name']

Step 5: Apply Migrations

Ensure the database is set up and synchronized with the initial migrations:

python manage.py migrate

Step 6: Start Development

You are now ready to start building your Django application. Create views, templates, and models within your app as needed.

3. Additional Information:

  • Prerequisites: Ensure Python and pip are installed, and you know how to set up and activate a virtual environment.
  • Database Setup: By default, Django uses SQLite. To use another database (e.g., PostgreSQL, MySQL), configure the DATABASES setting in settings.py.
  • Documentation: For more detailed information, refer to the Django Documentation.
  • Common Issues: If django-admin is not recognized, ensure that your Python environment is correctly configured, and django-admin is in your PATH.

Follow this procedure to quickly set up a Django project and begin development.