DEV Community

Cover image for How to start your django project the right way
Strerius
Strerius

Posted on

How to start your django project the right way

How to Start Your Django Project the Right Way

Django is a robust and versatile Python framework designed to simplify web development. However, how you start your Django project can significantly impact its scalability, maintainability, and performance. This guide provides a comprehensive, step-by-step walkthrough to help you start your Django project the right way, ensuring a solid foundation for success.


1. Set Up Your Environment

Install Python

Django is a Python-based framework, so you'll need Python installed on your system. Visit python.org to download the latest version (3.8 or higher recommended). Verify the installation:

python --version
Enter fullscreen mode Exit fullscreen mode

Install Pip

Pip is Python’s package manager, typically bundled with Python. Check if pip is installed:

pip --version
Enter fullscreen mode Exit fullscreen mode

If not, install it by following instructions on the official pip website.


2. Use a Virtual Environment

A virtual environment isolates your project dependencies, preventing conflicts with other projects. To create one:

  1. Install virtualenv:
   pip install virtualenv
Enter fullscreen mode Exit fullscreen mode
  1. Create a virtual environment:
   mkdir django_project
   cd django_project
   virtualenv venv
Enter fullscreen mode Exit fullscreen mode
  1. Activate the virtual environment:

    • On Windows:
     venv\Scripts\activate
    
  • On macOS/Linux:

     source venv/bin/activate
    

You’ll notice your terminal now shows (venv), indicating the virtual environment is active.


3. Install Django

Within the virtual environment, install Django:

pip install django
Enter fullscreen mode Exit fullscreen mode

Verify the installation:

django-admin --version
Enter fullscreen mode Exit fullscreen mode

4. Create Your Django Project

To start a new project, use the startproject command:

django-admin startproject myproject .
Enter fullscreen mode Exit fullscreen mode

This creates the following structure:

myproject/
├── manage.py
├── myproject/
│   ├── __init__.py
│   ├── asgi.py
│   ├── settings.py
│   ├── urls.py
│   ├── wsgi.py
Enter fullscreen mode Exit fullscreen mode

5. Configure Your Settings

Open myproject/settings.py and make the following essential configurations:

DEBUG Mode

Set DEBUG to True during development. For production, this must be set to False.

DEBUG = True
Enter fullscreen mode Exit fullscreen mode

Allowed Hosts

Add your domain or IP address to the ALLOWED_HOSTS list:

ALLOWED_HOSTS = ['127.0.0.1', 'localhost']
Enter fullscreen mode Exit fullscreen mode

Secret Key Management

Use environment variables or libraries like python-decouple to keep your SECRET_KEY secure. Replace the hardcoded key with:

from decouple import config
SECRET_KEY = config('SECRET_KEY', default='unsafe-default-key')
Enter fullscreen mode Exit fullscreen mode

6. Set Up the Database

Django defaults to SQLite for development, but you can configure a production database like PostgreSQL or MySQL. Update DATABASES in settings.py as needed. For example, to use PostgreSQL:

  1. Install the PostgreSQL client:
   pip install psycopg2
Enter fullscreen mode Exit fullscreen mode
  1. Configure DATABASES:
DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.postgresql',
        'NAME': 'mydatabase',
        'USER': 'myuser',
        'PASSWORD': 'mypassword',
        'HOST': 'localhost',
        'PORT': '5432',
    }
}
Enter fullscreen mode Exit fullscreen mode

Run migrations to apply initial database configurations:

python manage.py migrate
Enter fullscreen mode Exit fullscreen mode

7. Create a Superuser

Create an admin account for your project:

python manage.py createsuperuser
Enter fullscreen mode Exit fullscreen mode

Provide a username, email, and password when prompted.


8. Run the Development Server

Start the server to verify your project setup:

python manage.py runserver
Enter fullscreen mode Exit fullscreen mode

Visit http://127.0.0.1:8000/ in your browser. If you see the default Django welcome page, your project is successfully running.


9. Version Control with Git

Initialize Git in your project directory:

git init
Enter fullscreen mode Exit fullscreen mode

Add all files and make your first commit:

git add .
git commit -m "Initial Django project setup"
Enter fullscreen mode Exit fullscreen mode

Create a .gitignore file to exclude unnecessary files:

venv/
*.pyc
__pycache__/
db.sqlite3
Enter fullscreen mode Exit fullscreen mode

10. Plan Your App Structure

Django projects are built around modular apps. To add functionality, create an app:

python manage.py startapp myapp
Enter fullscreen mode Exit fullscreen mode

Register the app in settings.py under INSTALLED_APPS:

INSTALLED_APPS = [
    ...
    'myapp',
]
Enter fullscreen mode Exit fullscreen mode

11. Set Up Static and Media Files

Define paths for static and media files in settings.py:

STATIC_URL = '/static/'
STATICFILES_DIRS = [BASE_DIR / 'static']

MEDIA_URL = '/media/'
MEDIA_ROOT = BASE_DIR / 'media'
Enter fullscreen mode Exit fullscreen mode

Run the following command to collect static files for production:

python manage.py collectstatic
Enter fullscreen mode Exit fullscreen mode

12. Implement Security Best Practices

Before deploying to production, implement Django’s security features:

  1. Set DEBUG = False.
  2. Use environment variables for sensitive data.
  3. Configure HTTPS for your server.
  4. Add secure middleware settings like SECURE_HSTS_SECONDS.

Final Thoughts

Starting a Django project the right way involves more than just running commands—it's about setting up a clean, scalable, and maintainable foundation. By following these steps, you ensure your project is ready for growth and meets best practices for both development and production environments. Happy coding!

Top comments (0)