Django: How to create a new project in the current directory

Updated: January 21, 2024 By: Guest Contributor Post a comment

Introduction

Starting a new Django project is a primary step for beginning development with one of the most popular web frameworks for Python. This tutorial will guide you through the creation of a new Django project in the current directory of your choice. By the end of this guide, you will have a ready-to-go local Django project environment where you can start building your web application.

TL;DR

In order to initialize an ew Django project in the current directory, the point is to add a dot (.) at the end of the django-admin startproject command as shown below:

django-admin startproject projectname .

For more information, continue reading the article.

Detailed Instructions

Step 1: Install Django

If you haven’t installed Django yet, you can install it using pip. Open your terminal or command prompt and execute the following command:

pip install django

Step 2: Start a Django Project in the Current Directory

To create a Django project, you typically use the django-admin command followed by startproject and the project name. However, to create a project in the current directory, the syntax changes slightly. Here’s how it’s done:

$ cd /path/to/your/directory
$ django-admin startproject projectname .

Note the dot (.) at the end of the command, which tells Django to create the project in the current directory.

Understanding the Django Project Structure

After executing the above command, you’ll notice a number of files and directories created in your current directory. Here’s a brief on those:

  • manage.py: A command-line utility that lets you interact with your Django project in various ways.
  • projectname/: This directory houses the actual Python package for your project.
  • projectname/__init__.py: An empty file that tells Python that this directory should be considered a Python package.
  • projectname/settings.py: Settings/configuration for this Django project. Django settings will tell you all about how settings work.
  • projectname/urls.py: The URL declarations for this Django project; a “table of contents” of your Django-powered site.
  • projectname/wsgi.py: An entry-point for WSGI-compatible web servers to serve your project.

Step 3: Verify Your Project

It’s time to verify if your project is working properly. Run the following commands:

python manage.py runserver

If everything is set up correctly, you should be able to open your web browser and go to http://127.0.0.1:8000/ to view the default Django welcome page.

Step 4: The Development Server

You’ve just used Django’s development server. This server is included with Django so you can develop your site rapidly without having to deal with configuring a production server – like Apache or Nginx – until you’re ready for production.

Setting Up a Virtual Environment (optional but recommended)

It’s a best practice to create a Python virtual environment for your project. This provides an isolated environment to manage the dependencies for your specific project.

# Install virtualenv if you haven't installed it yet
pip install virtualenv

# Create a virtual environment
virtualenv venv

# Activate the virtual environment
# On Windows:
venv\Scripts\activate
# On Unix or MacOS:
source venv/bin/activate

# Now your virtual environment is active, you can install Django in it
pip install django

Setting Up Your Database

Django supports several major databases by default –SQLite, PostgreSQL, MySQL, and Oracle. By default, Django uses SQLite, so you won’t need to configure anything at this point. If you decide to use another database, you’ll have to modify your settings.py file accordingly.

Next Steps

Now that you have a running Django project, the next steps in your development could include:

  • Defining your data models in models.py
  • Setting up routing rules in urls.py
  • Creating views to interact with your models
  • Designing templates for your views
  • Securing your application
  • Conducting tests to confirm your application’s functionality
  • Deploying your application to a live server

These are the very basics of starting a Django project in the current directory. Although this guide is just the start, it’s a significant first step in your journey with Django. Happy coding!