Skip to main content

Creating Your First Django Project

Django Installation and Setup

Before we start creating our first Django project, we need to ensure Django is properly installed and set up on our system.

Installation

First, you need to have Python installed on your machine. Django is a Python framework, so it requires Python to run. You can download the latest version of Python from here.

Once Python is installed, you can install Django using pip, which is a package manager for Python. Open your terminal or command prompt and type the following command:

pip install django

This will download and install the latest stable version of Django. To verify that Django was installed correctly, you can use the following command:

python -m django --version

This will output the version of Django that you have installed.

Creating a New Django Project

Now that Django is installed, we can create a new Django project. In Django, a project is a collection of settings and configurations for the website you're building.

To create a new Django project, navigate to the directory where you want to create your project in your terminal or command prompt, and then type the following command:

django-admin startproject mysite

This will create a new Django project named 'mysite'.

Understanding the Project Structure

When you create a new Django project, it creates a directory structure as follows:

mysite/
manage.py
mysite/
__init__.py
settings.py
urls.py
asgi.py
wsgi.py

Here's what each file does:

  • manage.py: A command-line utility that allows you to interact with your project in various ways.
  • mysite/: The inner mysite directory is the actual Python package for your project.
  • __init__.py: An empty file that tells Python that this directory should be considered a Python package.
  • settings.py: Settings/configuration for this Django project.
  • urls.py: The URL declarations for this Django project.
  • asgi.py: An entry-point for ASGI-compatible web servers to serve your project.
  • wsgi.py: An entry-point for WSGI-compatible web servers to serve your project.

Running the Project

To run your project, navigate to the outer 'mysite' directory (the one that contains manage.py) in your terminal or command prompt, and then type the following command:

python manage.py runserver

This will start the development server at http://127.0.0.1:8000/. If you navigate to this URL in your web browser, you should see a 'Congratulations' page, indicating that your Django project is running successfully.

And that's it! You've installed Django and created your first Django project. In the next sections, we'll dive deeper into Django and start building a full-fledged web application. Happy coding!