Django

Setting Up Django-Useful steps and commands

Setting Up Django-Useful steps and commands

Steps to setting up a Django application. All neatly written for easy copying.

Installation

Use the package manager pip to install foobar.

pip install django

Creating Project

django-admin startproject projectname

Create a Django App

python manage.py startapp appname

Run Development Server

python manage.py runserver

Create Superuser

python manage.py createsuperuser

Create Migrations

python manage.py makemigrations

Apply Migrations

python manage.py migrate

Django Shell (Interactive Console)

python manage.py shell

Start Django Project with Virtual Environment

python3 -m venv venv

On Linux/Mac

source venv/bin/activate

On Windows

venv\Scripts\activate

Django Models

class MyModel(models.Model):
    field_name = models.CharField(max_length=255)

Django Views

from django.shortcuts import render

def my_view(request):
    return render(request, 'template_name.html')

Django URLs

from django.urls import path
 from . import views
urlpatterns = [
     path('my-view/', views.my_view, name='my-view'), 
]

Django Templates

<!DOCTYPE html>
<html>
<head>
    <title>{{ page_title }}</title>
</head>
<body>
    <h1>{{ heading }}</h1>
</body>
</html>

Static Files

STATIC_URL = '/static/'

Django Forms

 class MyForm(forms.Form):
     my_field = forms.CharField()

Django Admin

from django.contrib import admin
from .models import MyModel

admin.site.register(MyModel)

Crafted by GlenH with 🔥 using  React     NextJS,     MDX, Tailwind CSS     & ContentLayer. Hosted on Netlify  

© GlenH.me Contents from 2018 - 2024. Open Source