Show Menu
Cheatography

Django Basics Cheat Sheet by

Django basic project and app setup

Start a Project

$ django-admin.py startproject tango_my_django

Add a static content folder (Javas­cri­pt/CSS)

$ mkdir -p tango_my_django/static/css
$ mkdir -p tango_my_django/static/js


tango_my_django/tango_my_django/settings.py
STATIC_PATH = os.path.join(PROJECT_PATH,'static')
STATIC_URL = '/static/'
STATICFILES_DIRS = (
    STATIC_PATH,
)

Add a Templates Folder

$ mkdir -p tango_my_django/templates/rango

tango_my_django/tango_my_django/settings.py
TEMPLATE_PATH = os.path.join(PROJECT_PATH, 'templates')
TEMPLATE_DIRS = (TEMPLATE_PATH,)

Create a Template

tango_my_django/templates/rango/index.html
<!DOCTYPE html>
{% load static %}

<html>
     <head>
     ....

Start a App

$ tango_my_django/manage.py startapp tango_my_django/rango

Add App to Project

tango_my_django/settings.py

INSTALLED_APPS = ( ..., ..., 'rango', )

Route from Project to App

tango_my_django/tango_my_django/urls.py

# Include tango_my_django/rango/urls.py
urlpatterns = patterns('', url(r'^mainApp/', include('mainApp.urls')),)

Route from App to View

tango_my_django/rango/urls.py

from django.conf.urls import url, patterns
from mainApp import views

urlpatterns = patterns('', url(r'^$', views.index, name='index'),)

Create a view

tango_my_django/rango/views.py

from django.conf.http import HttpResponse

def index(request):
    return HttpResponse("hello rango")

Use Template inside a View

tango_my_django/rango/views.py

from django.shortcuts import render_to_response
from django.template import RequestContext

def index(request):
    context = RequestContext(request)
    context_dict = {'boldmessage' : 'I come from context_dict'}
    return render_to_response('mainApp/index.html', context_dict, context)
               

Help Us Go Positive!

We offset our carbon usage with Ecologi. Click the link below to help us!

We offset our carbon footprint via Ecologi
 

Comments

No comments yet. Add yours below!

Add a Comment

Your Comment

Please enter your name.

    Please enter your email address

      Please enter your Comment.

          Related Cheat Sheets

          Selenium WebDriver Cheat Sheet Cheat Sheet
          ISTQB Test Automation Engineering Cheat Sheet
          Cypressio Cheat Sheet