-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathDjango General Notes.txt
86 lines (62 loc) · 1.76 KB
/
Django General Notes.txt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# have pip for python3 installed
sudo apt-get install python3-pip
sudo pip3 install --upgrade pip
# install pipenv
pip install --user pipenv
# prepare folder
pipenv --three
# install django
pipenv install Django
# check if django is installed
pipenv run python -m django --version
# create a new project
pipenv run django-admin startproject mysite
# run applicaito
pipenv run python mysite/manage.py runserver
# migrate database
python3 manage.py migrate
# create django admin user
python3 manage.py createsuperuser
# create a new app in django project
python3 manage.py startapp polls
# add app in top level settings.py
INSTALLED_APPS = [
'polls.apps.PollsConfig',
]
# make migrations for app
python3 manage.py makemigrations polls
# peek database migrations
python3 manage.py sqlmigrate polls 0001
# create a sample view
def index(request):
return HttpResponse("Hello, world. You're at the polls index.")
# create urls file (urls.py)
urlpatterns = [
path('', views.index),
]
# add top level url to project
path('polls/', include('polls.urls'))
# define models
class Question(models.Model):
question_text = models.CharField(max_length=200)
pub_date = models.DateTimeField('date published')
class Choice(models.Model):
question = models.ForeignKey(Question, on_delete=models.CASCADE)
choice_text = models.CharField(max_length=200)
votes = models.IntegerField(default=0)
# make model modifiable in admin
# admin.py
admin.site.register(Question)
# create tostring effect
class Question(models.Model):
# ...
def __str__(self):
return self.question_text
# create a new instance of question
q = Question(question_text="What's new?", pub_date=timezone.now())
q.save()
q.id
# get all objects
Question.objects.all()
# delete object
c.delete()