Django

6.0.3 · active · verified Sun Mar 29

Django is a high-level Python web framework that encourages rapid development and clean, pragmatic design. It is currently at version 6.0.3 and follows a time-based release schedule with feature releases approximately every eight months. Long-term support (LTS) releases occur every two years and receive security and data loss fixes for three years.

Warnings

Install

Imports

Quickstart

This quickstart guides you through setting up a new Django project and application, running migrations, and creating an admin superuser. It modifies `settings.py` to include your new app and provides instructions to start the development server.

import os

def run_django_quickstart():
    project_name = 'myproject'
    app_name = 'myapp'

    print(f"Creating Django project '{project_name}'...")
    os.system(f'django-admin startproject {project_name} .')
    os.chdir(project_name)

    print(f"Creating Django app '{app_name}'...")
    os.system(f'python manage.py startapp {app_name}')

    # Modify settings.py to include the new app
    settings_path = os.path.join(project_name, 'settings.py')
    with open(settings_path, 'r') as f:
        content = f.readlines()

    insert_index = -1
    for i, line in enumerate(content):
        if 'INSTALLED_APPS' in line:
            insert_index = i + 1
            break

    if insert_index != -1:
        content.insert(insert_index, f"    '{app_name}',\n")
    else:
        print("Warning: Could not find INSTALLED_APPS in settings.py. Please add manually.")

    with open(settings_path, 'w') as f:
        f.writelines(content)

    print("Applying migrations...")
    os.system('python manage.py makemigrations')
    os.system('python manage.py migrate')

    print("Creating superuser (username: admin, email: admin@example.com, password: password). Please change in production!")
    create_superuser_script = (
        "from django.contrib.auth import get_user_model; "
        "User = get_user_model(); "
        "User.objects.filter(username='admin').exists() or User.objects.create_superuser('admin', 'admin@example.com', 'password')"
    )
    os.system(f'python manage.py shell -c "{create_superuser_script}"')

    print("Quickstart complete. To run the development server, navigate to the project root and execute:")
    print("python manage.py runserver")
    print("Admin interface will be at http://127.0.0.1:8000/admin/")

# To run this quickstart, you would execute: run_django_quickstart()

view raw JSON →