Skip to content

Commit 600fc41

Browse files
Taking notes from chapter 1 + 2 of django intro tutorial
0 parents  commit 600fc41

File tree

1 file changed

+146
-0
lines changed

1 file changed

+146
-0
lines changed

django.md

Lines changed: 146 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,146 @@
1+
# Django Notes
2+
3+
Abbreviation | Meaning
4+
--- | ---
5+
CM | Command
6+
PN | Project name
7+
AN | Application name
8+
EN | Virtualenv name
9+
PR | Project root folder
10+
AR | Application root folder
11+
12+
### Command line
13+
14+
command | description
15+
--- | ---
16+
`django-admin <CM>` | These commands can be run anywhere, as long as the `virtualenv` is active.
17+
`python manage.py <CM>` | You must be in the same directory as this file to run it (app root)
18+
19+
combine with one of the commands below:
20+
21+
command | description
22+
---|----
23+
startapp | creates a new app (inside the project)
24+
createsuperuser| this user has been granted all permissions
25+
runserver | runs the development web server
26+
shell_plus | runs an enhanced python shell
27+
migrate | applies migrations to the database
28+
makemigrations | makes db migration files
29+
dbshell | connects to the database |
30+
cleanup| cleans up the database
31+
32+
### shell_plus commands
33+
command|description
34+
--- | ---
35+
<MN>.objects.all() | Queries all model names and returns the results
36+
<MN>.objects.filter(id=#) | Filter an object's based on an attribute
37+
<MN>.objects.get(pk=#) | get a specific item
38+
q = <MN>(<model_field>="",) | Creates a new model which can be saved using `q.save()`
39+
`import datetime` | Useful time management library
40+
`from django.utils import timezone` | Useful timezone conversion library
41+
42+
### Creating an app
43+
44+
Command | What it does
45+
--- | ---
46+
`./manage.py startapp <AN>` | creates an app
47+
`project_root/settings.py` | add the app to `INSTALLED_APPLICATIONS`
48+
`project_root/urls.py` | add the app urls
49+
50+
### Starting a project
51+
52+
In Pycharm, create a new project and set the interpreter to a new `virtualenv`.
53+
54+
If you're going to use a terminal:
55+
56+
Command | What it does
57+
--- | ---
58+
`brew install python3` | Homebrew is a MacOS packager manager
59+
`pip install virtualenv virtualenvwrapper` | installs environment managing tool
60+
`mkvirtualenv <EN>` | create a virtual environment
61+
`workon <EN>` | boot up virtualenv
62+
`pip install django` | django in the virtualenv
63+
`django-admin startproject <PN>` | creates the base project
64+
`./manage-py runserver` | check if the server runs (must be in project root)
65+
66+
## Application folder
67+
68+
### models.py
69+
70+
````
71+
from django.db import models
72+
73+
74+
class <ClassName>(models.Model):
75+
var = models.CharField(max_length=5, default='aaa')
76+
foreign_key = models.ForeignKey(<target_model>, on_delete=cascade)
77+
78+
__str__(self)
79+
return self.var
80+
81+
def some_function(self):
82+
return self.somevar == timezone.now()
83+
````
84+
85+
### urls.py
86+
87+
````
88+
from django.conf.urls import url
89+
90+
from . import views
91+
92+
urlpatterns = [
93+
url(r'^$', views.index, name='index'),
94+
]
95+
````
96+
97+
### views.py
98+
99+
````
100+
from django.http import HttpResponse
101+
102+
def index(request):
103+
return HttpResponse("Hello.")
104+
````
105+
106+
## Project folder
107+
108+
### urls.py
109+
110+
````
111+
from django.conf.urls import url, include
112+
from django.contrib import admin
113+
from django.http import HttpResponseRedirect
114+
115+
urlpatterns = [
116+
# e.g. myapp/
117+
url(r'^<AN>/', include(<AN>.urls')),
118+
# e.g. admin/
119+
url(r'^admin/', admin.site.urls),
120+
# e.g. /
121+
url(r'^$', lambda r: HttpResponseRedirect('<AN>/')),
122+
]
123+
````
124+
125+
### settings.py
126+
127+
````
128+
INSTALLED_APPS = [
129+
'django_extensions',
130+
'<AN>.apps.<AN>Config',
131+
'django.contrib.admin',
132+
'django.contrib.auth',
133+
'django.contrib.contenttypes',
134+
'django.contrib.sessions',
135+
'django.contrib.messages',
136+
'django.contrib.staticfiles',
137+
]
138+
````
139+
140+
141+
## Webstorm
142+
143+
### Setting up a server script
144+
`Languages > Frameworks > Django` - check enable django support
145+
The settings file is in `<PR>/<PF>/settings.py`
146+
You can edit your configurations in the top right. I recommend giving them names and only allowing single instances to run.

0 commit comments

Comments
 (0)