[Documentation] [TitleIndex] [WordIndex

DJANGO

videos

Useful videos:

sentdex videos

heroku deployment

Start Project

1. start virtual environment in your favourite folder or make a new one

virtualenv .

2. activate virtual environment

source bin/activate

3. if you haven't installed django

sudo pip install django==1.8

4. create a djano project, you can put any name

django-admin.py startproject trydjango18

for sake of easiness rename it to src (which means source code) 5. got to src and runserver

python manage.py runserver

Migrations

1. migrations is change how we store data

python manage.py migrate

Admin & Superuser

1. migrate and create admin (syncdb is not there in 1.9)

python manage.py syncdb

2. to directly create superuser after migrate

python manage.py createsuperuser

3. go and add /admin to the url, where you can login as admin

Apps

apps in django are little functions that build up your entire django webapp (project). These are like which does one function at its best.

1. create new app

python manage.py startapp newsletter

First view and URL routing

1. in configfolder/urls.py, r is root url, 2nd arg is which views.py should be executed

url(r'^$','newsletter.views.home', name='home'),  #set a root page view
url(r'^newsletter','newsletter.views.home', name='home'), #if requested with/newsletter views.home will be called and that template will be viewed

2. defining that view in newsletter/views.py

def home(request):
        return render(request,"home.html",{})
#if got a request return home.html template, as simple as that

Django Settings Overview

1. goto configfolder/setting.py

import os # helps us to work with any os, operating system independent
BASE_DIR #is directory of project where manage.py is, this is root of project
SECURITY_KEY #security thing
DEBUG #security thing
ALLOWED_HOSTS # Production stuff like yourdomain.com
INSTALLED_APPS #
MIDDLEWARE_CLASSES # middleware is like b/w request and response
ROOT_URL_CONF # this tells what urls to work with which points url.py
TEMPLATES #
WSGI_APPLICATION# points to wsgi.py
DATABASES # we r using sqlite3 others mysql and postgresql

LANGUAGE_CODE #can be changed
TIME_ZONE
STATIC_URL # since images and css cant be used directly we use static directory

Configuration for Templates

1.in configfolder/settings.py

INSTALLED APPS=(
...
'newsletter'
)

2. Create templates folder in newsletter and home.html in templates then run the server. you will get blank page.

thats it it loads apps if you put installed_apps

(or)

1. create templates folder in src or root, and home.html in root

2. in the template directories define the path

TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [os.path.join(BASE_DIR, "templates")],# see in base/templates
...
    },
]

Models

1. Models are classes we define where the a model is like an action getting the data and it will transfer it to database. Eg: signup model

class SignUp (models.Model):
        email=models.EmailField()
        full_name=models.CharField(max_length=40,blank=True,null=True)
        timestamp=models.DateTimeField(auto_now_add=True, auto_now=False)
        updated=models.DateTimeField(auto_now_add=False, auto_now=True)

        def __unicode__ (self): #__str__ in python3.3
                return self.email

2.Add the app name in the installed apps in settings.py of configfolder

INSTALLED APPS=(
...
'newsletter'
)

3. run the migration, run this when ever models.py changed

python manage.py makemigrations

Admin

1. add the following snippet to admin.py in the app

from .models import SignUp

admin.site.register(SignUp)

so that, it will be registered, and admin can view the database tables

2. add the following snippet to admin.py in the app, list_display is to view all kind of details you will be able to watch

# Register your models here.
from .models import SignUp
class SignUpAdmin(admin.ModelAdmin):
        list_display=["__unicode__","full_name","updated"]
        class Meta:
                model=SignUp
admin.site.register(SignUp,SignUpAdmin)

2024-11-16 14:45