-
Notifications
You must be signed in to change notification settings - Fork 14
Django cheatsheet
Django
python -m virtualenv remotingEnv source remotingEnv/bin/activate
deactivate
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
- 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?=====
-
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
-
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)
- 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' } }
-
Create the tables in the db $ python manage.py migrate
-
Create your models These are with respect to your app
-
GENERIC VIEWS ============================================================ from django.views import generic
class InndexView(generic.ListView)
Returns the queryset that will be used to retrieve the object that this view will display.
===============