Django Notes:

default

 
Install virtualenvwrapper
-------------------------
pacman -S python-virtualenvwrapper
export WORKON_HOME=~/.virtualenvs
source /usr/bin/virtualenvwrapper.sh

mkvirtualenv -p /usr/bin/python2 my_env
virtualenv --no-site-packages -p /usr/bin/python2 udemy-py-django-scratch
Create a virtualenv with python2

workon my_env
source my_env
Activate the virtualenv

(my_env)$ pip install django
(my_env)$ easy_install django
Install django in the virtualenv

(my_env)$ deactivate
Leave the virutalenv

django-admin.py startproject django_test
Start a new project by creating startup files

python2 manage.py runserver
Test the project with webserver

python manage.py runserver 8080
Start the development server on another port

python2 manage.py startapp article
Start a new app (module) called article

./manage.py makemigrations
Makes new migrations (updates)

python2 manage.py syncdb
Updates database with new data

python2 manage.py reset article
Removes a table schema and data from the database

python2 manage.py collectstatic
Grabs all static directories and info and registers them


----------------------
python manage.py shell
Allows you to import local modules from the current app you are
using as if they are part of python.

Then you can run:

from article.models import Article
Article.objects.all()
To show all articles

from django.utils import timezone
Pull timezone from django settings (used below)

a = Article(title="Added from shell", body="oh yeah!", pub_date=timezone.now(), likes=0)
Add info from the manage.py shell

a.save()
Save info just added

a.id
Show id of article just added

Article.objects.all()
Show all articles in shell
----------------------



default