Skip to content

Django cheatsheet

Christian edited this page Jun 18, 2017 · 1 revision

Django

Setup virtual env

python2.7

python -m virtualenv remotingEnv source remotingEnv/bin/activate

exit virtualenv

deactivate

virtual ENV PYTHON3

this is python3 -m venv remotingVirtualEnv

source remotingVirtualEnv/bin/activate

1.setup project: $ django-admin startproject projectName 2.run it $ python manage.py runserver 3.add app $ python manage.py startapp appName 4.add app to project view In project/settings.py in INSTALLED_APPS = [] make sure to include your app 'polls.apps.PollsConfig' # 'name.apps.configName' 5.setup db In project/settings.py put the respective dict() a.create tables $ python manage.py migrate b. Inform django project you've made changes to models $ python manage.py makemigrations polls #create the migrations c. Now actually run the migrations on db $ python manage.py sqlmigrate polls #### d. Since we changed the init setup, call migrate $ python manage.py migrate #apply changes to db ^ this will take all unapplied migrations and run them against the db; syncing changes 6. run tests pyhton manage.py test appName

  1. Create Project $ django-admin startproject projectName This gets created projectName/ manage.py projectName/ init.py settings.py urls.py wsgi.py

=====what is init?=====
init?=> the file init.py allows structuring python's module namespace by using dotted module names. So A.B => Submodule B in package A Example: main/ [all folders contain their individual init.py] mode/maintain.py mount/control.py So now I can do something like import mode.maintain mode.maintain.someMethod() #when using you would do or from mode import maintain maintain.someMethod() # better =====what is init?=====

  1. Create App $ python manage.py startapp appName this creates: appName/ init.py admin.py apps.py migrations/ __init.py models.py tests.py views.py

  2. Make Sure Your Project Knows Your App Exists In project/settings.py in INSTALLED_APPS = [] make sure to include your app 'polls.apps.PollsConfig' 'name.apps.configName'

==== How is this interacting? ====

In order to get a view, we need to map a url to that view in urls.py url(regex , view, kwargs, name)

  1. Database setup This will be in the PROJECT settings.py Depending on your db, this might be different, but for pg it's:

============================================================ DATABASES = { 'default': { 'ENGINE': 'django.db.backends.postgresql', 'NAME': 'djangoProject', 'USER': 'postgres', 'PASSWORD' : '', # empty string => localhost 'HOST' : '', 'PORT' : '5432' } }

  1. Create the tables in the db $ python manage.py migrate

  2. Create your models These are with respect to your app

  3. GENERIC VIEWS ============================================================ from django.views import generic

class InndexView(generic.ListView)

auto defualts : template_name = app_name/model_name_detail.html

auto defualts : conext_object_name = object_list

if acting upon a model provide its name (model = Question)

get_queryset(self): generic uses this as its

Returns the queryset that will be used to retrieve the object that this view will display.

===============