-
Notifications
You must be signed in to change notification settings - Fork 1
/
django.py
91 lines (69 loc) · 2.34 KB
/
django.py
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
86
87
88
89
90
91
#DJANGO CHEATSHEET
""" Django is a Python framework used for web development, it's known
for its easy and fast deployment of web apps. It builds around a MVT
(Model-View-Template) design and it configures the patterns to use
automatically, so you can focus only on building the app. """
#Setting up a virtual environment
1. py -m venv {venv_name} #for windows
2. python -m venv {venv_name} #for linux/macOS
#Activate your virtual environment
1. {venv_name}\Scripts\activate.bat #for windows
2. source {venv_name}/bin/activate #for linux/macOS
#Create a project
django-admin startproject {project_name}
#Create an app
django-admin startapp {app_name}
#Make migrations
python manage.py migrate #go inside your project directory to migrate
#Run server
python manage.py runserver #it runs the server on your localhost http://127.0.0.1:8000/
#URLs
urlpatters = [
path('{template}/', views.{template}, name='template'),
path('{template}/', views.{template}, name='template'),
path('{template}/', views.{template}, name='template')
]
#Views
def {template}(request):
return render(request, '{template}.html', {})
###and/or###
def {template}(request):
action = ###
context = {
'action':action
}
return render(request, '{template}.html', context)
#Create a superuser
django-admin createsuperuser {superuser}
#Display the usage information
django-admin help
#List of available commands
django-admin help --command
#Display the description of a given command
django-admin help {command}
#Determining the version of Django
django-admin --version
#Creating new migrations
django-admin makemigrations
#Sending a test email
django-admin sendtestemail
#Start Python's interactive interpreter
django-admin shell
#Show all the migrations in your project
django-admin showmigrations
#Static Files
1. python manage.py collectstatic
2. python manage.py findstatic
3. python manage.py runserver
#Testing
1. python manage.py test
2. python manage.py testserver
#ORM Queries
## To create a db table go to models.py and type something like this
class Student(models.Model):
username = models.CharField(max_length=20)
first_name = models.CharField(max_length=30)
last_name = models.CharField(max_length=30)
## Type these lines to apply migrations to db
1. python manage.py makemigrations
2. python manage.py migrate