0% found this document useful (0 votes)
80 views

Django Cheatsheet

Uploaded by

gowthamjunkmail
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
80 views

Django Cheatsheet

Uploaded by

gowthamjunkmail
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 1

WarnerPyCoding Django Commands Cheat Sheet

Action Command
Starting Commands
Install Django pip install django
Create a New Django Project django-admin startproject projectname
Run the Development Server python manage.py runserver
Create a New Django Application python manage.py startapp appname
Models and Migrations
Create Models and Generate Migrations python manage.py makemigrations
Apply Migrations to Databases python manage.py migrate
Create Super User for Admin Access python manage.py createsuperuser
Troubleshoot: Override Superuser Creation winpty python manage.py createsuperuser
Views
Create Django Views (Option #1) from django.http import HttpResponse
def my_view(request):
return HttpResponse("Hello World")
Create Django Views (Option #2) from django.shortcuts import render
def my_view(request):
return render(request, 'page.html', {context}
Create URL Patterns from django.urls import path
from . import views
urlpatterns = [
path('my-view', views.my_view, name='my-view'
]
Admin
Generate Admin Panel for Models from django.contrib import admin
from .models import ModelName
admin.site.register(ModelName)
Access Django Admin http://localhost:8000/admin
Customize Django Admin from django.contrib import admin
from .models import ModelName
@admin.register(ModelName)
class ModelNameAdmin(admin.ModelAdmin):
list_display = ['field1', 'field2']
Create Custom Actions from .models import ModelName
def custom_action(modeladmin, request, queryset):
#Custom Action Logic
custom_action.short_description = "Custom Action"
Django Testing
Run Tests python manage.py test
Create Test Cases (In Appname/tests.py) from django.test import TestCase
class MyModelTestCase(TestCase):
def setUp(self):
#Setup Data for Tests
def test_my_model_method(self):
#write test logic
Run Individual Test Case python manage.py test appname.tests.MyModelTestCase.test_my_model_method
Forms
Create Django Forms from django import forms
class MyForm(fomrs.Form):
#Form Fields
Database Queries
Query all objects from a database table variable = Model.objects.all()
Filter and Query specific data from a database table variable = Model.objects.filter(field=filter_by)

www.WarnerPyCoding.codes [email protected]

You might also like