-
Notifications
You must be signed in to change notification settings - Fork 2
/
Lecture 3 - CS50's Web Programming with Python and JavaScript.htm
1629 lines (1295 loc) · 111 KB
/
Lecture 3 - CS50's Web Programming with Python and JavaScript.htm
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<!DOCTYPE html>
<html class="wf-ptsans-n4-active wf-ptsans-n7-active wf-active" lang="en-us"><head>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<meta charset="utf-8">
<meta name="viewport" content="initial-scale=1, width=device-width"><meta property="og:description" content="This course picks up where Harvard University's CS50 leaves off, diving more deeply into the design and implementation of web apps with Python, JavaScript, and SQL using frameworks like Django, React, and Bootstrap. Topics include database design, scalability, security, and user experience. Through hands-on projects, students learn to write and use APIs, create interactive UIs, and leverage cloud services like GitHub and Heroku. By semester’s end, students emerge with knowledge and experience in principles, languages, and tools that empower them to design and deploy applications on the Internet.">
<meta property="og:image" content="https://img.youtube.com/vi/24Kf3v7kZyE/maxresdefault.jpg"><meta property="og:title" content="Lecture 3 - CS50's Web Programming with Python and JavaScript">
<link href="https://cs50.harvard.edu/web/2020/favicon.ico?1645555620" rel="icon">
<!-- https://fonts.google.com/specimen/PT+Sans?query=PT+Sans&selection.family=PT+Sans:ital,wght@0,400;0,700;1,400;1,700 -->
<script src="Lecture%203%20-%20CS50's%20Web%20Programming%20with%20Python%20and%20JavaScript_files/webfont.js"></script>
<!-- http://getbootstrap.com/docs/4.6/getting-started/introduction/ -->
<script src="Lecture%203%20-%20CS50's%20Web%20Programming%20with%20Python%20and%20JavaScript_files/jquery.js"></script>
<script src="Lecture%203%20-%20CS50's%20Web%20Programming%20with%20Python%20and%20JavaScript_files/bootstrap.js"></script>
<!-- https://bootstrap-table.com/docs/getting-started/introduction/ -->
<link href="Lecture%203%20-%20CS50's%20Web%20Programming%20with%20Python%20and%20JavaScript_files/bootstrap-table.css" rel="stylesheet">
<script src="Lecture%203%20-%20CS50's%20Web%20Programming%20with%20Python%20and%20JavaScript_files/bootstrap-table.js"></script>
<script src="Lecture%203%20-%20CS50's%20Web%20Programming%20with%20Python%20and%20JavaScript_files/bootstrap-table-mobile.js"></script>
<!-- https://fontawesome.com/how-to-use/on-the-web/referencing-icons/basic-use -->
<link href="Lecture%203%20-%20CS50's%20Web%20Programming%20with%20Python%20and%20JavaScript_files/all.css" rel="stylesheet">
<!-- https://moment.github.io/luxon/ -->
<script src="Lecture%203%20-%20CS50's%20Web%20Programming%20with%20Python%20and%20JavaScript_files/luxon.js"></script>
<!-- http://docs.mathjax.org/ -->
<!-- https://www.jsdelivr.com/package/npm/mathjax?path=es5 -->
<script crossorigin="anonymous" integrity="sha256-+nfSJ1LiSvANSK3wvi6FjM+oFwoIQvURU1frozp5z6o=" src="Lecture%203%20-%20CS50's%20Web%20Programming%20with%20Python%20and%20JavaScript_files/tex-chtml.js"></script><style type="text/css">.CtxtMenu_InfoClose { top:.2em; right:.2em;}
.CtxtMenu_InfoContent { overflow:auto; text-align:left; font-size:80%; padding:.4em .6em; border:1px inset; margin:1em 0px; max-height:20em; max-width:30em; background-color:#EEEEEE; white-space:normal;}
.CtxtMenu_Info.CtxtMenu_MousePost {outline:none;}
.CtxtMenu_Info { position:fixed; left:50%; width:auto; text-align:center; border:3px outset; padding:1em 2em; background-color:#DDDDDD; color:black; cursor:default; font-family:message-box; font-size:120%; font-style:normal; text-indent:0; text-transform:none; line-height:normal; letter-spacing:normal; word-spacing:normal; word-wrap:normal; white-space:nowrap; float:none; z-index:201; border-radius: 15px; /* Opera 10.5 and IE9 */ -webkit-border-radius:15px; /* Safari and Chrome */ -moz-border-radius:15px; /* Firefox */ -khtml-border-radius:15px; /* Konqueror */ box-shadow:0px 10px 20px #808080; /* Opera 10.5 and IE9 */ -webkit-box-shadow:0px 10px 20px #808080; /* Safari 3 & Chrome */ -moz-box-shadow:0px 10px 20px #808080; /* Forefox 3.5 */ -khtml-box-shadow:0px 10px 20px #808080; /* Konqueror */ filter:progid:DXImageTransform.Microsoft.dropshadow(OffX=2, OffY=2, Color="gray", Positive="true"); /* IE */}
</style><style type="text/css">.CtxtMenu_MenuClose { position:absolute; cursor:pointer; display:inline-block; border:2px solid #AAA; border-radius:18px; -webkit-border-radius: 18px; /* Safari and Chrome */ -moz-border-radius: 18px; /* Firefox */ -khtml-border-radius: 18px; /* Konqueror */ font-family: "Courier New", Courier; font-size:24px; color:#F0F0F0}
.CtxtMenu_MenuClose span { display:block; background-color:#AAA; border:1.5px solid; border-radius:18px; -webkit-border-radius: 18px; /* Safari and Chrome */ -moz-border-radius: 18px; /* Firefox */ -khtml-border-radius: 18px; /* Konqueror */ line-height:0; padding:8px 0 6px /* may need to be browser-specific */}
.CtxtMenu_MenuClose:hover { color:white!important; border:2px solid #CCC!important}
.CtxtMenu_MenuClose:hover span { background-color:#CCC!important}
.CtxtMenu_MenuClose:hover:focus { outline:none}
</style><style type="text/css">.CtxtMenu_Menu { position:absolute; background-color:white; color:black; width:auto; padding:5px 0px; border:1px solid #CCCCCC; margin:0; cursor:default; font: menu; text-align:left; text-indent:0; text-transform:none; line-height:normal; letter-spacing:normal; word-spacing:normal; word-wrap:normal; white-space:nowrap; float:none; z-index:201; border-radius: 5px; /* Opera 10.5 and IE9 */ -webkit-border-radius: 5px; /* Safari and Chrome */ -moz-border-radius: 5px; /* Firefox */ -khtml-border-radius: 5px; /* Konqueror */ box-shadow:0px 10px 20px #808080; /* Opera 10.5 and IE9 */ -webkit-box-shadow:0px 10px 20px #808080; /* Safari 3 & Chrome */ -moz-box-shadow:0px 10px 20px #808080; /* Forefox 3.5 */ -khtml-box-shadow:0px 10px 20px #808080; /* Konqueror */}
.CtxtMenu_MenuItem { padding: 1px 2em; background:transparent;}
.CtxtMenu_MenuArrow { position:absolute; right:.5em; padding-top:.25em; color:#666666; font-family: null; font-size: .75em}
.CtxtMenu_MenuActive .CtxtMenu_MenuArrow {color:white}
.CtxtMenu_MenuArrow.CtxtMenu_RTL {left:.5em; right:auto}
.CtxtMenu_MenuCheck { position:absolute; left:.7em; font-family: null}
.CtxtMenu_MenuCheck.CtxtMenu_RTL { right:.7em; left:auto }
.CtxtMenu_MenuRadioCheck { position:absolute; left: .7em;}
.CtxtMenu_MenuRadioCheck.CtxtMenu_RTL { right: .7em; left:auto}
.CtxtMenu_MenuInputBox { padding-left: 1em; right:.5em; color:#666666; font-family: null;}
.CtxtMenu_MenuInputBox.CtxtMenu_RTL { left: .1em;}
.CtxtMenu_MenuComboBox { left:.1em; padding-bottom:.5em;}
.CtxtMenu_MenuSlider { left: .1em;}
.CtxtMenu_SliderValue { position:absolute; right:.1em; padding-top:.25em; color:#333333; font-size: .75em}
.CtxtMenu_SliderBar { outline: none; background: #d3d3d3}
.CtxtMenu_MenuLabel { padding: 1px 2em 3px 1.33em; font-style:italic}
.CtxtMenu_MenuRule { border-top: 1px solid #DDDDDD; margin: 4px 3px;}
.CtxtMenu_MenuDisabled { color:GrayText}
.CtxtMenu_MenuActive { background-color: #606872; color: white;}
.CtxtMenu_MenuDisabled:focus { background-color: #E8E8E8}
.CtxtMenu_MenuLabel:focus { background-color: #E8E8E8}
.CtxtMenu_ContextMenu:focus { outline:none}
.CtxtMenu_ContextMenu .CtxtMenu_MenuItem:focus { outline:none}
.CtxtMenu_SelectionMenu { position:relative; float:left; border-bottom: none; -webkit-box-shadow:none; -webkit-border-radius:0px; }
.CtxtMenu_SelectionItem { padding-right: 1em;}
.CtxtMenu_Selection { right: 40%; width:50%; }
.CtxtMenu_SelectionBox { padding: 0em; max-height:20em; max-width: none; background-color:#FFFFFF;}
.CtxtMenu_SelectionDivider { clear: both; border-top: 2px solid #000000;}
.CtxtMenu_Menu .CtxtMenu_MenuClose { top:-10px; left:-10px}
</style>
<!-- https://github.com/verlok/vanilla-lazyload -->
<script src="Lecture%203%20-%20CS50's%20Web%20Programming%20with%20Python%20and%20JavaScript_files/intersection-observer.js"></script>
<script src="Lecture%203%20-%20CS50's%20Web%20Programming%20with%20Python%20and%20JavaScript_files/lazyload.js"></script>
<!-- https://github.com/davidjbradshaw/iframe-resizer -->
<!-- https://www.jsdelivr.com/package/npm/iframe-resizer?path=js -->
<script crossorigin="anonymous" integrity="sha256-S8f0Q/V9VcfrqYgWo9EFS9zuDMdPTBMC+CBW0RjxQbs=" src="Lecture%203%20-%20CS50's%20Web%20Programming%20with%20Python%20and%20JavaScript_files/iframeResizer.js"></script>
<!-- https://github.com/scratchblocks/scratchblocks/releases -->
<script src="Lecture%203%20-%20CS50's%20Web%20Programming%20with%20Python%20and%20JavaScript_files/scratchblocks.js"></script><style><![CDATA[ .sb-label {
font-family: Lucida Grande, Verdana, Arial, DejaVu Sans, sans-serif;
font-weight: bold;
fill: #fff;
font-size: 10px;
word-spacing: +1px;
}
.sb-obsolete { fill: #d42828; }
.sb-motion { fill: #4a6cd4; }
.sb-looks { fill: #8a55d7; }
.sb-sound { fill: #bb42c3; }
.sb-pen { fill: #0e9a6c; }
.sb-events { fill: #c88330; }
.sb-control { fill: #e1a91a; }
.sb-sensing { fill: #2ca5e2; }
.sb-operators { fill: #5cb712; }
.sb-variables { fill: #ee7d16; }
.sb-list { fill: #cc5b22 }
.sb-custom { fill: #632d99; }
.sb-custom-arg { fill: #5947b1; }
.sb-extension { fill: #4b4a60; }
.sb-grey { fill: #969696; }
.sb-bevel {
filter: url(#bevelFilter);
}
.sb-input {
filter: url(#inputBevelFilter);
}
.sb-input-number,
.sb-input-string,
.sb-input-number-dropdown {
fill: #fff;
}
.sb-literal-number,
.sb-literal-string,
.sb-literal-number-dropdown,
.sb-literal-dropdown {
font-weight: normal;
font-size: 9px;
word-spacing: 0;
}
.sb-literal-number,
.sb-literal-string,
.sb-literal-number-dropdown {
fill: #000;
}
.sb-darker {
filter: url(#inputDarkFilter);
}
.sb-outline {
stroke: #fff;
stroke-opacity: 0.2;
stroke-width: 2;
fill: none;
}
.sb-define-hat-cap {
stroke: #632d99;
stroke-width: 1;
fill: #8e2ec2;
}
.sb-comment {
fill: #ffffa5;
stroke: #d0d1d2;
stroke-width: 1;
}
.sb-comment-line {
fill: #ffff80;
}
.sb-comment-label {
font-family: Helevetica, Arial, DejaVu Sans, sans-serif;
font-weight: bold;
fill: #5c5d5f;
word-spacing: 0;
font-size: 12px;
}
.sb-diff {
fill: none;
stroke: #000;
}
.sb-diff-ins {
stroke-width: 2px;
}
.sb-diff-del {
stroke-width: 3px;
}
]]></style><style><![CDATA[ .sb3-label {
font: 500 12pt Helevetica Neue, Helvetica, sans-serif;
fill: #fff;
word-spacing: +1pt;
}
.sb3-motion { fill: #4c97ff; stroke: #3373cc; }
.sb3-motion-alt { fill: #4280d7; }
.sb3-motion-dark { fill: #4c97ff; }
.sb3-looks { fill: #9966ff; stroke: #774dcb; }
.sb3-looks-alt { fill: #855cd6; }
.sb3-looks-dark { fill: #bd42bd; }
.sb3-sound { fill: #cf63cf; stroke: #bd42bd; }
.sb3-sound-alt { fill: #c94fc9; }
.sb3-sound-dark { fill: #bd42bd; }
.sb3-control { fill: #ffab19; stroke: #cf8b17; }
.sb3-control-alt { fill: #ec9c13; }
.sb3-control-dark { fill: #cf8b17; }
.sb3-events { fill: #ffbf00; stroke: #cc9900; }
.sb3-events-alt { fill: #e6ac00; }
.sb3-events-dark { fill: #cc9900; }
.sb3-sensing { fill: #5cb1d6; stroke: #2e8eb8; }
.sb3-sensing-alt { fill: #47a8d1; }
.sb3-sensing-dark { fill: #2e8eb8; }
.sb3-operators { fill: #59c059; stroke: #389438; }
.sb3-operators-alt { fill: #46b946; }
.sb3-operators-dark { fill: #389438; }
.sb3-variables { fill: #ff8c1a; stroke: #db6e00; }
.sb3-variables-alt { fill: #ff8000; }
.sb3-variables-dark { fill: #db6e00; }
.sb3-list { fill: #ff661a; stroke: #e64d00; }
.sb3-list-alt { fill: #ff5500; }
.sb3-list-dark { fill: #e64d00; }
.sb3-custom { fill: #ff6680; stroke: #ff3355; }
.sb3-custom-alt { fill: #ff4d6a; }
.sb3-custom-dark { fill: #ff3355; }
.sb3-custom-arg { fill: #ff6680; stroke: #ff3355; }
/* extension blocks, e.g. pen */
.sb3-extension { fill: #0fbd8c; stroke: #0b8e69; }
.sb3-extension-alt { fill: #0da57a; }
.sb3-extension-line { stroke: #0da57a; }
.sb3-extension-dark { fill: #0b8e69; }
/* obsolete colors: chosen by hand, indicates invalid blocks */
.sb3-obsolete { fill: #ed4242; stroke: #ca2b2b; }
.sb3-obsolete-alt { fill: #db3333; }
.sb3-obsolete-dark { fill: #ca2b2b; }
/* grey: special color from the Scratch 3.0 design mockups */
.sb3-grey { fill: #bfbfbf; stroke: #909090; }
.sb3-grey-alt { fill: #b2b2b2; }
.sb3-grey-dark { fill: #909090; }
.sb3-input-color {
stroke: #fff;
}
.sb3-input-number,
.sb3-input-string {
fill: #fff;
}
.sb3-literal-number,
.sb3-literal-string,
.sb3-literal-number-dropdown,
.sb3-literal-dropdown {
word-spacing: 0;
}
.sb3-literal-number,
.sb3-literal-string {
fill: #575e75;
}
.sb3-comment {
fill: #ffffa5;
stroke: #d0d1d2;
stroke-width: 1;
}
.sb3-comment-line {
fill: #ffff80;
}
.sb3-comment-label {
font: 400 12pt Helevetica Neue, Helvetica, sans-serif;
fill: #000;
word-spacing: 0;
}
.sb3-diff {
fill: none;
stroke: #000;
}
.sb3-diff-ins {
stroke-width: 2px;
}
.sb3-diff-del {
stroke-width: 3px;
}
]]></style>
<link href="Lecture%203%20-%20CS50's%20Web%20Programming%20with%20Python%20and%20JavaScript_files/page.css" rel="stylesheet">
<link rel="stylesheet" href="Lecture%203%20-%20CS50's%20Web%20Programming%20with%20Python%20and%20JavaScript_files/css.css" media="all"><script src="Lecture%203%20-%20CS50's%20Web%20Programming%20with%20Python%20and%20JavaScript_files/jekyll-theme-cs50.js"></script>
<script>
window.CS50 = {
local: {"day":"numeric","hour":"numeric","minute":"numeric","month":"long","timeZoneName":"short","weekday":"long","year":"numeric"},
locale: "en",
tz: "America/New_York"
};
</script>
<title>Lecture 3 - CS50's Web Programming with Python and JavaScript</title>
<style id="MJX-CHTML-styles">
mjx-container[jax="CHTML"] {
line-height: 0;
}
mjx-container [space="1"] {
margin-left: .111em;
}
mjx-container [space="2"] {
margin-left: .167em;
}
mjx-container [space="3"] {
margin-left: .222em;
}
mjx-container [space="4"] {
margin-left: .278em;
}
mjx-container [space="5"] {
margin-left: .333em;
}
mjx-container [rspace="1"] {
margin-right: .111em;
}
mjx-container [rspace="2"] {
margin-right: .167em;
}
mjx-container [rspace="3"] {
margin-right: .222em;
}
mjx-container [rspace="4"] {
margin-right: .278em;
}
mjx-container [rspace="5"] {
margin-right: .333em;
}
mjx-container [size="s"] {
font-size: 70.7%;
}
mjx-container [size="ss"] {
font-size: 50%;
}
mjx-container [size="Tn"] {
font-size: 60%;
}
mjx-container [size="sm"] {
font-size: 85%;
}
mjx-container [size="lg"] {
font-size: 120%;
}
mjx-container [size="Lg"] {
font-size: 144%;
}
mjx-container [size="LG"] {
font-size: 173%;
}
mjx-container [size="hg"] {
font-size: 207%;
}
mjx-container [size="HG"] {
font-size: 249%;
}
mjx-container [width="full"] {
width: 100%;
}
mjx-box {
display: inline-block;
}
mjx-block {
display: block;
}
mjx-itable {
display: inline-table;
}
mjx-row {
display: table-row;
}
mjx-row > * {
display: table-cell;
}
mjx-mtext {
display: inline-block;
}
mjx-mstyle {
display: inline-block;
}
mjx-merror {
display: inline-block;
color: red;
background-color: yellow;
}
mjx-mphantom {
visibility: hidden;
}
mjx-assistive-mml {
position: absolute !important;
top: 0px;
left: 0px;
clip: rect(1px, 1px, 1px, 1px);
padding: 1px 0px 0px 0px !important;
border: 0px !important;
display: block !important;
width: auto !important;
overflow: hidden !important;
-webkit-touch-callout: none;
-webkit-user-select: none;
-khtml-user-select: none;
-moz-user-select: none;
-ms-user-select: none;
user-select: none;
}
mjx-assistive-mml[display="block"] {
width: 100% !important;
}
mjx-c::before {
display: block;
width: 0;
}
.MJX-TEX {
font-family: MJXZERO, MJXTEX;
}
.TEX-B {
font-family: MJXZERO, MJXTEX-B;
}
.TEX-I {
font-family: MJXZERO, MJXTEX-I;
}
.TEX-MI {
font-family: MJXZERO, MJXTEX-MI;
}
.TEX-BI {
font-family: MJXZERO, MJXTEX-BI;
}
.TEX-S1 {
font-family: MJXZERO, MJXTEX-S1;
}
.TEX-S2 {
font-family: MJXZERO, MJXTEX-S2;
}
.TEX-S3 {
font-family: MJXZERO, MJXTEX-S3;
}
.TEX-S4 {
font-family: MJXZERO, MJXTEX-S4;
}
.TEX-A {
font-family: MJXZERO, MJXTEX-A;
}
.TEX-C {
font-family: MJXZERO, MJXTEX-C;
}
.TEX-CB {
font-family: MJXZERO, MJXTEX-CB;
}
.TEX-FR {
font-family: MJXZERO, MJXTEX-FR;
}
.TEX-FRB {
font-family: MJXZERO, MJXTEX-FRB;
}
.TEX-SS {
font-family: MJXZERO, MJXTEX-SS;
}
.TEX-SSB {
font-family: MJXZERO, MJXTEX-SSB;
}
.TEX-SSI {
font-family: MJXZERO, MJXTEX-SSI;
}
.TEX-SC {
font-family: MJXZERO, MJXTEX-SC;
}
.TEX-T {
font-family: MJXZERO, MJXTEX-T;
}
.TEX-V {
font-family: MJXZERO, MJXTEX-V;
}
.TEX-VB {
font-family: MJXZERO, MJXTEX-VB;
}
mjx-stretchy-v mjx-c, mjx-stretchy-h mjx-c {
font-family: MJXZERO, MJXTEX-S1, MJXTEX-S4, MJXTEX, MJXTEX-A ! important;
}
@font-face /* 0 */ {
font-family: MJXZERO;
src: url("https://cdn.jsdelivr.net/npm/mathjax@3.1.2/es5/output/chtml/fonts/woff-v2/MathJax_Zero.woff") format("woff");
}
@font-face /* 1 */ {
font-family: MJXTEX;
src: url("https://cdn.jsdelivr.net/npm/mathjax@3.1.2/es5/output/chtml/fonts/woff-v2/MathJax_Main-Regular.woff") format("woff");
}
@font-face /* 2 */ {
font-family: MJXTEX-B;
src: url("https://cdn.jsdelivr.net/npm/mathjax@3.1.2/es5/output/chtml/fonts/woff-v2/MathJax_Main-Bold.woff") format("woff");
}
@font-face /* 3 */ {
font-family: MJXTEX-I;
src: url("https://cdn.jsdelivr.net/npm/mathjax@3.1.2/es5/output/chtml/fonts/woff-v2/MathJax_Math-Italic.woff") format("woff");
}
@font-face /* 4 */ {
font-family: MJXTEX-MI;
src: url("https://cdn.jsdelivr.net/npm/mathjax@3.1.2/es5/output/chtml/fonts/woff-v2/MathJax_Main-Italic.woff") format("woff");
}
@font-face /* 5 */ {
font-family: MJXTEX-BI;
src: url("https://cdn.jsdelivr.net/npm/mathjax@3.1.2/es5/output/chtml/fonts/woff-v2/MathJax_Math-BoldItalic.woff") format("woff");
}
@font-face /* 6 */ {
font-family: MJXTEX-S1;
src: url("https://cdn.jsdelivr.net/npm/mathjax@3.1.2/es5/output/chtml/fonts/woff-v2/MathJax_Size1-Regular.woff") format("woff");
}
@font-face /* 7 */ {
font-family: MJXTEX-S2;
src: url("https://cdn.jsdelivr.net/npm/mathjax@3.1.2/es5/output/chtml/fonts/woff-v2/MathJax_Size2-Regular.woff") format("woff");
}
@font-face /* 8 */ {
font-family: MJXTEX-S3;
src: url("https://cdn.jsdelivr.net/npm/mathjax@3.1.2/es5/output/chtml/fonts/woff-v2/MathJax_Size3-Regular.woff") format("woff");
}
@font-face /* 9 */ {
font-family: MJXTEX-S4;
src: url("https://cdn.jsdelivr.net/npm/mathjax@3.1.2/es5/output/chtml/fonts/woff-v2/MathJax_Size4-Regular.woff") format("woff");
}
@font-face /* 10 */ {
font-family: MJXTEX-A;
src: url("https://cdn.jsdelivr.net/npm/mathjax@3.1.2/es5/output/chtml/fonts/woff-v2/MathJax_AMS-Regular.woff") format("woff");
}
@font-face /* 11 */ {
font-family: MJXTEX-C;
src: url("https://cdn.jsdelivr.net/npm/mathjax@3.1.2/es5/output/chtml/fonts/woff-v2/MathJax_Calligraphic-Regular.woff") format("woff");
}
@font-face /* 12 */ {
font-family: MJXTEX-CB;
src: url("https://cdn.jsdelivr.net/npm/mathjax@3.1.2/es5/output/chtml/fonts/woff-v2/MathJax_Calligraphic-Bold.woff") format("woff");
}
@font-face /* 13 */ {
font-family: MJXTEX-FR;
src: url("https://cdn.jsdelivr.net/npm/mathjax@3.1.2/es5/output/chtml/fonts/woff-v2/MathJax_Fraktur-Regular.woff") format("woff");
}
@font-face /* 14 */ {
font-family: MJXTEX-FRB;
src: url("https://cdn.jsdelivr.net/npm/mathjax@3.1.2/es5/output/chtml/fonts/woff-v2/MathJax_Fraktur-Bold.woff") format("woff");
}
@font-face /* 15 */ {
font-family: MJXTEX-SS;
src: url("https://cdn.jsdelivr.net/npm/mathjax@3.1.2/es5/output/chtml/fonts/woff-v2/MathJax_SansSerif-Regular.woff") format("woff");
}
@font-face /* 16 */ {
font-family: MJXTEX-SSB;
src: url("https://cdn.jsdelivr.net/npm/mathjax@3.1.2/es5/output/chtml/fonts/woff-v2/MathJax_SansSerif-Bold.woff") format("woff");
}
@font-face /* 17 */ {
font-family: MJXTEX-SSI;
src: url("https://cdn.jsdelivr.net/npm/mathjax@3.1.2/es5/output/chtml/fonts/woff-v2/MathJax_SansSerif-Italic.woff") format("woff");
}
@font-face /* 18 */ {
font-family: MJXTEX-SC;
src: url("https://cdn.jsdelivr.net/npm/mathjax@3.1.2/es5/output/chtml/fonts/woff-v2/MathJax_Script-Regular.woff") format("woff");
}
@font-face /* 19 */ {
font-family: MJXTEX-T;
src: url("https://cdn.jsdelivr.net/npm/mathjax@3.1.2/es5/output/chtml/fonts/woff-v2/MathJax_Typewriter-Regular.woff") format("woff");
}
@font-face /* 20 */ {
font-family: MJXTEX-V;
src: url("https://cdn.jsdelivr.net/npm/mathjax@3.1.2/es5/output/chtml/fonts/woff-v2/MathJax_Vector-Regular.woff") format("woff");
}
@font-face /* 21 */ {
font-family: MJXTEX-VB;
src: url("https://cdn.jsdelivr.net/npm/mathjax@3.1.2/es5/output/chtml/fonts/woff-v2/MathJax_Vector-Bold.woff") format("woff");
}
</style></head>
<body class="">
<div class="alert fixed-top mb-0 rounded-0 shadow alert-success alert-dismissible" data-alert="success dismissible" data-hash="edc24fffed7a3a9ad8c189ef5e2aca41" id="alert" role="alert">
<button aria-label="Close" class="btn-close" data-bs-dismiss="alert" type="button"></button><p>Interested in <a href="https://cs50.harvard.edu/web/2020/#how-to-take-this-course" class="alert-link">a verified certificate, a professional certificate, or transfer credit and accreditation</a>?
And get vaccinated (as soon as it’s available to you). 💉 <a href="https://www.who.int/news-room/feature-stories/detail/getting-the-covid-19-vaccine" class="alert-link">Here’s why</a>. <a href="https://www.google.com/search?q=covid-19+vaccine+finder" class="alert-link">Here’s how</a>.</p></div>
<div class="container-fluid">
<div class="row">
<aside class="col-md" style="height: 513.667px; top: 81.3333px;">
<header><h1 data-id="cs50s-web-programming-with-python-and-javascript"><a href="https://cs50.harvard.edu/web/2020/">CS50’s Web Programming with Python and JavaScript</a></h1>
<p>OpenCourseWare</p>
<p><a class="pr-1 small" href="https://cs50.harvard.edu/donate">Donate<i aria-hidden="true" class="fas fa-external-link-alt ps-2"></i></a></p>
<p><a href="https://brianyu.me/">Brian Yu</a><br>
<a href="mailto:brian@cs.harvard.edu">brian@cs.harvard.edu</a></p>
<p><a href="https://cs.harvard.edu/malan/">David J. Malan</a>
<br>
<a href="mailto:malan@harvard.edu">malan@harvard.edu</a>
<br>
<a class="mr-1" href="https://www.facebook.com/dmalan"><i aria-hidden="true" class="fab fa-facebook-f" title="Facebook"></i><span class="sr-only">Facebook</span></a>
<a class="mr-1" href="https://github.com/dmalan"><i aria-hidden="true" class="fab fa-github" title="GitHub"></i><span class="sr-only">GitHub</span></a>
<a class="mr-1" href="https://www.instagram.com/davidjmalan/"><i aria-hidden="true" class="fab fa-instagram" title="Instagram"></i><span class="sr-only">Instagram</span></a>
<a class="mr-1" href="https://www.linkedin.com/in/malan/"><i aria-hidden="true" class="fab fa-linkedin" title="LinkedIn"></i><span class="sr-only">LinkedIn</span></a>
<a class="mr-1" href="https://orcid.org/0000-0001-5338-2522"><i aria-hidden="true" class="fab fa-orcid" title="ORCID"></i><span class="sr-only">ORCID</span></a>
<a class="mr-1" href="https://www.quora.com/profile/David-J-Malan"><i aria-hidden="true" class="fab fa-quora" title="Quora"></i><span class="sr-only">Quora</span></a>
<a class="mr-1" href="https://www.reddit.com/user/davidjmalan"><i aria-hidden="true" class="fab fa-reddit-alien" title="Reddit"></i><span class="sr-only">Reddit</span></a>
<a class="mr-1" href="https://www.tiktok.com/@davidjmalan"><i aria-hidden="true" class="fab fa-tiktok" title="TikTok"></i><span class="sr-only">Twitter</span></a>
<a class="mr-1" href="https://twitter.com/davidjmalan"><i aria-hidden="true" class="fab fa-twitter" title="Twitter"></i><span class="sr-only">Twitter</span></a></p></header>
<button aria-controls="nav" aria-expanded="false" class="btn btn-sm collapsed d-md-none" data-bs-target="aside > nav" data-bs-toggle="collapse">
Menu
</button>
<nav class="collapse d-md-block" id="nav"><hr>
<ul class="fa-ul">
<li data-marker="*"><span class="fa-li"><i class="fas fa-square"></i></span><a href="https://cs50.harvard.edu/x/movie/">🍿 CS50x Movie Night 2022</a></li>
<li data-marker="*"><span class="fa-li"><i class="fas fa-square"></i></span><a href="https://cs50.harvard.edu/x/puzzles/"><i class="fas fa-puzzle-piece pe-2"></i>CS50x Puzzle Day 2022</a></li>
<li data-marker="*"><span class="fa-li"><i class="fas fa-square"></i></span><a href="https://cs50.harvard.edu/x/prepare/"><i class="fas fa-book pe-2"></i>How to Prepare for Technical Interviews</a></li>
<li data-marker="*"><span class="fa-li"><i class="fas fa-square"></i></span><a href="https://cs50.ly/zoom"><i class="fas fa-video pe-2"></i>Zoom Meetings</a></li>
</ul>
<hr>
<ol start="0">
<li><a href="https://cs50.harvard.edu/web/2020/weeks/0/">HTML, CSS</a></li>
<li><a href="https://cs50.harvard.edu/web/2020/weeks/1/">Git</a></li>
<li><a href="https://cs50.harvard.edu/web/2020/weeks/2/">Python</a></li>
<li><a href="https://cs50.harvard.edu/web/2020/weeks/3/">Django</a></li>
<li><a href="https://cs50.harvard.edu/web/2020/weeks/4/">SQL, Models, and Migrations</a></li>
<li><a href="https://cs50.harvard.edu/web/2020/weeks/5/">JavaScript</a></li>
<li><a href="https://cs50.harvard.edu/web/2020/weeks/6/">User Interfaces</a></li>
<li><a href="https://cs50.harvard.edu/web/2020/weeks/7/">Testing, CI/CD</a></li>
<li><a href="https://cs50.harvard.edu/web/2020/weeks/8/">Scalability and Security</a></li>
</ol>
<hr>
<ul class="fa-ul">
<li data-marker="*"><span class="fa-li"><i class="fas fa-square"></i></span><a href="https://cs50.harvard.edu/web/2020/honesty/">Academic Honesty</a></li>
<li data-marker="*"><span class="fa-li"><i class="fas fa-square"></i></span><a href="https://cs50.harvard.edu/web/2020/certificate/">CS50 Certificate</a></li>
<li data-marker="*"><span class="fa-li"><i class="fas fa-square"></i></span><a href="https://cs50.harvard.edu/web/2020/faqs/">FAQs</a></li>
<li data-marker="*"><span class="fa-li"><i class="fas fa-square"></i></span><a href="https://cs50.me/cs50w">Gradebook</a></li>
</ul>
<hr>
<ul class="fa-ul">
<li data-marker="*"><span class="fa-li"><i class="fas fa-square"></i></span><a href="https://cs50.edx.org/ed">Ed Discussion</a> for Q&A</li>
<li data-marker="*" class="small"><span class="fa-li"><i class="fas fa-square"></i></span><a href="https://us.edstem.org/quickstart/ed-discussion.pdf">Quick Start Guide</a></li>
</ul>
<hr>
<ul class="small fa-ul">
<li data-marker="*"><span class="fa-li"><i class="fas fa-square"></i></span><a href="https://cs50.edx.org/web">edX</a></li>
<li data-marker="*"><span class="fa-li"><i class="fas fa-square"></i></span><a href="https://itunes.apple.com/us/course/id1505432709">iTunes U</a></li>
<li data-marker="*"><span class="fa-li"><i class="fas fa-square"></i></span><a href="https://www.youtube.com/playlist?list=PLhQjrBD2T380xvFSUmToMMzERZ3qB5Ueu">YouTube</a></li>
</ul>
<hr>
<ul class="small fa-ul">
<li data-marker="*"><span class="fa-li"><i class="fas fa-square"></i></span><a href="https://cs50.noticeable.news/">Changelog</a></li>
<li data-marker="*"><span class="fa-li"><i class="fas fa-square"></i></span><a href="https://cs50.statuspage.io/">Status Page</a></li>
</ul>
<hr>
<ul class="fa-ul">
<li data-marker="*"><span class="fa-li"><i class="fas fa-square"></i></span><a href="https://cs50.harvard.edu/web/2020/communities/"><strong>Communities</strong></a></li>
<li data-marker="*" class="small"><span class="fa-li"><i class="fas fa-square"></i></span><a href="https://www.clubhouse.com/club/cs50">Clubhouse</a></li>
<li data-marker="*" class="small"><span class="fa-li"><i class="fas fa-square"></i></span><a href="https://discord.gg/cs50">Discord</a> <span class="badge bg-light ms-1 py-1 rounded-pill text-dark">Q&A</span></li>
<li data-marker="*" class="small"><span class="fa-li"><i class="fas fa-square"></i></span><a href="https://cs50.edx.org/ed">Ed</a> <span class="badge bg-light ms-1 py-1 rounded-pill text-dark">Q&A</span></li>
<li data-marker="*" class="small"><span class="fa-li"><i class="fas fa-square"></i></span><a href="https://www.facebook.com/groups/cs50/">Facebook Group</a> <span class="badge bg-light ms-1 py-1 rounded-pill text-dark">Q&A</span></li>
<li data-marker="*" class="small"><span class="fa-li"><i class="fas fa-square"></i></span><a href="https://www.facebook.com/cs50/">Facebook Page</a></li>
<li data-marker="*" class="small"><span class="fa-li"><i class="fas fa-square"></i></span><a href="https://github.com/cs50">GitHub</a></li>
<li data-marker="*" class="small"><span class="fa-li"><i class="fas fa-square"></i></span><a href="https://gitter.im/cs50/x">Gitter</a> <span class="badge bg-light ms-1 py-1 rounded-pill text-dark">Q&A</span></li>
<li data-marker="*" class="small"><span class="fa-li"><i class="fas fa-square"></i></span><a href="https://instagram.com/cs50">Instagram</a></li>
<li data-marker="*" class="small"><span class="fa-li"><i class="fas fa-square"></i></span><a href="https://www.linkedin.com/groups/7437240/">LinkedIn Group</a></li>
<li data-marker="*" class="small"><span class="fa-li"><i class="fas fa-square"></i></span><a href="https://www.linkedin.com/school/CS50/">LinkedIn Page</a></li>
<li data-marker="*" class="small"><span class="fa-li"><i class="fas fa-square"></i></span><a href="https://cs50.medium.com/">Medium</a></li>
<li data-marker="*" class="small"><span class="fa-li"><i class="fas fa-square"></i></span><a href="https://www.quora.com/topic/CS50">Quora</a></li>
<li data-marker="*" class="small"><span class="fa-li"><i class="fas fa-square"></i></span><a href="https://www.reddit.com/r/cs50/">Reddit</a> <span class="badge bg-light ms-1 py-1 rounded-pill text-dark">Q&A</span></li>
<li data-marker="*" class="small"><span class="fa-li"><i class="fas fa-square"></i></span><a href="https://cs50.edx.org/slack">Slack</a> <span class="badge bg-light ms-1 py-1 rounded-pill text-dark">Q&A</span></li>
<li data-marker="*" class="small"><span class="fa-li"><i class="fas fa-square"></i></span><a href="https://www.snapchat.com/add/cs50">Snapchat</a></li>
<li data-marker="*" class="small"><span class="fa-li"><i class="fas fa-square"></i></span><a href="https://soundcloud.com/cs50">SoundCloud</a></li>
<li data-marker="*" class="small"><span class="fa-li"><i class="fas fa-square"></i></span><a href="https://cs50.stackexchange.com/">Stack Exchange</a> <span class="badge bg-light ms-1 py-1 rounded-pill text-dark">Q&A</span></li>
<li data-marker="*" class="small"><span class="fa-li"><i class="fas fa-square"></i></span><a href="https://www.tiktok.com/@cs50">TikTok</a></li>
<li data-marker="*" class="small"><span class="fa-li"><i class="fas fa-square"></i></span><a href="https://twitter.com/cs50">Twitter</a></li>
<li data-marker="*" class="small"><span class="fa-li"><i class="fas fa-square"></i></span><a href="http://www.youtube.com/subscription_center?add_user=cs50tv">YouTube</a></li>
</ul>
<hr>
<p><a href="https://cs50.harvardshop.com/"><img src="Lecture%203%20-%20CS50's%20Web%20Programming%20with%20Python%20and%20JavaScript_files/GGMdcKt.png" alt="Harvard Shop"></a></p>
<hr>
<p><a href="https://cs50.harvard.edu/web/2020/license/" class="small"><i class="fab fa-creative-commons me-1"></i>License</a></p></nav>
<footer></footer>
</aside>
<main class="col-md" style="margin-bottom: 0px; margin-top: 81.3333px;">
<a data-id="" id="lecture-3" style="top: -81.3333px;"></a><h1 class="no_toc"><a data-id="" href="#lecture-3">Lecture 3</a></h1>
<ul id="markdown-toc">
<li><a href="#introduction" id="markdown-toc-introduction">Introduction</a></li>
<li><a href="#web-applications" id="markdown-toc-web-applications">Web Applications</a></li>
<li><a href="#http" id="markdown-toc-http">HTTP</a></li>
<li><a href="#django" id="markdown-toc-django">Django</a></li>
<li><a href="#routes" id="markdown-toc-routes">Routes</a></li>
<li><a href="#templates" id="markdown-toc-templates">Templates</a> <ul>
<li><a href="#conditionals" id="markdown-toc-conditionals">Conditionals:</a></li>
<li><a href="#styling" id="markdown-toc-styling">Styling</a></li>
</ul>
</li>
<li><a href="#tasks" id="markdown-toc-tasks">Tasks</a></li>
<li><a href="#forms" id="markdown-toc-forms">Forms</a> <ul>
<li><a href="#django-forms" id="markdown-toc-django-forms">Django Forms</a></li>
</ul>
</li>
<li><a href="#sessions" id="markdown-toc-sessions">Sessions</a></li>
</ul>
<a data-id="" id="introduction" style="top: -81.3333px;"></a><h2><a data-id="" href="#introduction">Introduction</a></h2>
<ul class="fa-ul">
<li data-marker="*"><span class="fa-li"><i class="fas fa-square"></i></span>So
far, we’ve discussed how to build simple web pages using HTML and CSS,
and how to use Git and GitHub in order to keep track of changes to our
code and collaborate with others. We also familiarized ourselves with
the Python programming language.</li>
<li data-marker="*"><span class="fa-li"><i class="fas fa-square"></i></span>Today, we’ll work on using Python’s <code class="language-plaintext highlighter-rouge">Django</code> framework in order to create dynamic applications.</li>
</ul>
<a data-id="" id="web-applications" style="top: -81.3333px;"></a><h2><a data-id="" href="#web-applications">Web Applications</a></h2>
<p>So far, all of the web applications we’ve written have been <strong>static</strong>.
This means that every single time we open that web page, it looks
exactly the same. Many websites we visit every day, however, change
every time we visit them. If you visit the websites of the <a href="https://www.nytimes.com/">New York Times</a> or <a href="https://www.facebook.com/">Facebook</a>,
for example, you’ll most likely see different things today than you
will tomorrow. For large sites like those, it would be unreasonable for
employees to have to manually edit a large HTML file every time a change
is made, which is where <strong>dynamic</strong> websites can be
extremely useful. A dynamic website is one that takes advantage of a
programming language (such as Python) to dynamically generate HTML and
CSS files. During this lecture, we’ll learn how to create our first
dynamic applications.</p>
<a data-id="" id="http" style="top: -81.3333px;"></a><h2><a data-id="" href="#http">HTTP</a></h2>
<p>HTTP, or HyperText Transfer Protocol, is a widely-accepted protocol
for how messages are transfered back and forth across the internet.
Typically, information online is passed between a client (user) and a
server.
<img src="Lecture%203%20-%20CS50's%20Web%20Programming%20with%20Python%20and%20JavaScript_files/client_server.png" alt="Client and Server"></p>
<p>In this protocol, the client will send a <strong>request</strong> to the server, that might look something like the one below. In the example below, <code class="language-plaintext highlighter-rouge">GET</code> is simply a type of request, one of three we’ll discuss in this course. The <code class="language-plaintext highlighter-rouge">/</code>
typically indicates that we’re looking for the website’s home page, and
the three dots indicate that we could be passing in more information as
well.
<img src="Lecture%203%20-%20CS50's%20Web%20Programming%20with%20Python%20and%20JavaScript_files/request.png" alt="Request"></p>
<p>After receiving a request, a server will then send back an HTTP
response, which might look something like the one below. Such a response
will include the HTTP version, a status code (200 means OK), a
description of the content, and then some additional information.
<img src="Lecture%203%20-%20CS50's%20Web%20Programming%20with%20Python%20and%20JavaScript_files/response.png" alt="Response"></p>
<p>200 is just one of many status codes, some of which you may have seen in the past:
<img src="Lecture%203%20-%20CS50's%20Web%20Programming%20with%20Python%20and%20JavaScript_files/codes.png" alt="Codes"></p>
<a data-id="" id="django" style="top: -81.3333px;"></a><h2><a data-id="" href="#django">Django</a></h2>
<p><a href="https://www.djangoproject.com/">Django</a> is a Python-based
web framework that will allow us to write Python code that dynamically
generates HTML and CSS. The advantage to using a framework like Django
is that a lot of code is already written for us that we can take
advantage of.</p>
<ul class="fa-ul">
<li data-marker="*"><span class="fa-li"><i class="fas fa-square"></i></span>To get started, we’ll have to install Django, which means you’ll also have to <a href="https://pip.pypa.io/en/stable/installing/">install pip</a> if you haven’t already done so.</li>
<li data-marker="*"><span class="fa-li"><i class="fas fa-square"></i></span>Once you have Pip installed, you can run <code class="language-plaintext highlighter-rouge">pip3 install Django</code> in your terminal to install Django.</li>
</ul>
<p>After installing Django, we can go through the steps of creating a new Django project:</p>
<ol>
<li>Run <code class="language-plaintext highlighter-rouge">django-admin startproject PROJECT_NAME</code> to create a number of starter files for our project.</li>
<li>Run <code class="language-plaintext highlighter-rouge">cd PROJECT_NAME</code> to navigate into your new project’s directory.</li>
<li>Open the directory in your text editor of choice. You’ll notice
that some files have been created for you. We won’t need to look at most
of these for now, but there are three that will be very important from
the start:
<ul class="fa-ul">
<li data-marker="*"><span class="fa-li"><i class="fas fa-square"></i></span><code class="language-plaintext highlighter-rouge">manage.py</code> is what we use to execute commands on our terminal. We won’t have to edit it, but we’ll use it often.</li>
<li data-marker="*"><span class="fa-li"><i class="fas fa-square"></i></span><code class="language-plaintext highlighter-rouge">settings.py</code>
contains some important configuration settings for our new project.
There are some default settings, but we may wish to change some of them
from time to time.</li>
<li data-marker="*"><span class="fa-li"><i class="fas fa-square"></i></span><code class="language-plaintext highlighter-rouge">urls.py</code> contains directions for where users should be routed after navigating to a certain URL.</li>
</ul>
</li>
<li>Start the project by running <code class="language-plaintext highlighter-rouge">python manage.py runserver</code>.
This will open a development server, which you can access by visiting
the URL provided. This development server is being run locally on your
machine, meaning other people cannot access your website. This should
bring you to a default landing page:
<img src="Lecture%203%20-%20CS50's%20Web%20Programming%20with%20Python%20and%20JavaScript_files/landing.png" alt="Landing Page"></li>
<li>Next, we’ll have to create an application. Django projects are split into one or more <strong>applications</strong>.
Most of our projects will only require one application, but larger
sites can make use of this ability to split a site into multiple apps.
To create an application, we run <code class="language-plaintext highlighter-rouge">python manage.py startapp APP_NAME</code>. This will create some additional directories and files that will be useful shortly, including <code class="language-plaintext highlighter-rouge">views.py</code>.</li>
<li>Now, we have to install our new app. To do this, we go to <code class="language-plaintext highlighter-rouge">settings.py</code>, scroll down to the list of <code class="language-plaintext highlighter-rouge">INSTALLED_APPS</code>, and add the name of our new application to this list.
<img src="Lecture%203%20-%20CS50's%20Web%20Programming%20with%20Python%20and%20JavaScript_files/installed.png" alt="installed apps"></li>
</ol>
<a data-id="" id="routes" style="top: -81.3333px;"></a><h2><a data-id="" href="#routes">Routes</a></h2>
<p>Now, in order to get started with our application:</p>
<ol>
<li>
<p>Next, we’ll navigate to <code class="language-plaintext highlighter-rouge">views.py</code>.
This file will contain a number of different views, and we can think of
a view for now as one page the user might like to see. To create our
first view, we’ll write a function that takes in a <code class="language-plaintext highlighter-rouge">request</code>. For now, we’ll simply return an <code class="language-plaintext highlighter-rouge">HttpResponse</code>
(A very simple response that includes a response code of 200 and a
string of text that can be displayed in a web browser) of “Hello,
World”. In order to do this, we have include <code class="language-plaintext highlighter-rouge">from django.http import HttpResponse</code>. Our file now looks like:</p>
<div class="language-python highlighter-rouge"><div class="highlight"><pre class="highlight"><code> <span class="kn">from</span> <span class="nn">django.shortcuts</span> <span class="kn">import</span> <span class="n">render</span>
<span class="kn">from</span> <span class="nn">django.http</span> <span class="kn">import</span> <span class="n">HttpResponse</span>
<span class="c1"># Create your views here.
</span>
<span class="k">def</span> <span class="nf">index</span><span class="p">(</span><span class="n">request</span><span class="p">):</span>
<span class="k">return</span> <span class="n">HttpResponse</span><span class="p">(</span><span class="s">"Hello, world!"</span><span class="p">)</span>
</code></pre></div> </div>
</li>
<li>Now, we need to somehow associate this view we have just created
with a specific URL. To do this, we’ll create another file called <code class="language-plaintext highlighter-rouge">urls.py</code> in the same directory as <code class="language-plaintext highlighter-rouge">views.py</code>. We already have a <code class="language-plaintext highlighter-rouge">urls.py</code> file for the whole project, but it is best to have a separate one for each individual app.</li>
<li>Inside our new <code class="language-plaintext highlighter-rouge">urls.py</code>, we’ll create a list of url patterns that a user might visit while using our website. In order to do this:
<ol>
<li>We have to make some imports: <code class="language-plaintext highlighter-rouge">from django.urls import path</code> will give us the ability to reroute URLSs, and <code class="language-plaintext highlighter-rouge">from . import views</code> will import any functions we’ve created in <code class="language-plaintext highlighter-rouge">views.py</code>.</li>
<li>Create a list called <code class="language-plaintext highlighter-rouge">urlpatterns</code></li>
<li>For each desired URL, add an item to the <code class="language-plaintext highlighter-rouge">urlpatterns</code> list that contains a call to the <code class="language-plaintext highlighter-rouge">path</code> function with two or three arguments: A string representing the URL path, a function from <code class="language-plaintext highlighter-rouge">views.py</code> that we wish to call when that URL is visited, and (optionally) a name for that path, in the format <code class="language-plaintext highlighter-rouge">name="something"</code>. For example, here’s what our simple app looks like now:</li>
</ol>
<div class="language-python highlighter-rouge"><div class="highlight"><pre class="highlight"><code> <span class="kn">from</span> <span class="nn">django.urls</span> <span class="kn">import</span> <span class="n">path</span>
<span class="kn">from</span> <span class="nn">.</span> <span class="kn">import</span> <span class="n">views</span>
<span class="n">urlpatterns</span> <span class="o">=</span> <span class="p">[</span>
<span class="n">path</span><span class="p">(</span><span class="s">""</span><span class="p">,</span> <span class="n">views</span><span class="p">.</span><span class="n">index</span><span class="p">,</span> <span class="n">name</span><span class="o">=</span><span class="s">"index"</span><span class="p">)</span>
<span class="p">]</span>
</code></pre></div> </div>
</li>
<li>
<p>Now, we’ve created a <code class="language-plaintext highlighter-rouge">urls.py</code> for this specific application, and it’s time to edit the <code class="language-plaintext highlighter-rouge">urls.py</code> created for us for the entire project. When you open this file, you should see that there’s already a path called <code class="language-plaintext highlighter-rouge">admin</code> which we’ll go over in later lectures. We want to add another path for our new app, so we’ll add an item to the <code class="language-plaintext highlighter-rouge">urlpatterns</code> list. This follows the same pattern as our earlier paths, except instead of adding a function from <code class="language-plaintext highlighter-rouge">views.py</code> as our second argument, we want to be able to include <em>all</em> of the paths from the <code class="language-plaintext highlighter-rouge">urls.py</code> file within our application. To do this, we write: <code class="language-plaintext highlighter-rouge">include("APP_NAME.urls")</code>, where <code class="language-plaintext highlighter-rouge">include</code> is a function we gain access to by also importing <code class="language-plaintext highlighter-rouge">include</code> from <code class="language-plaintext highlighter-rouge">django.urls</code> as shown in the <code class="language-plaintext highlighter-rouge">urls.py</code> below:</p>
<div class="language-python highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="kn">from</span> <span class="nn">django.contrib</span> <span class="kn">import</span> <span class="n">admin</span>
<span class="kn">from</span> <span class="nn">django.urls</span> <span class="kn">import</span> <span class="n">path</span><span class="p">,</span> <span class="n">include</span>
<span class="n">urlpatterns</span> <span class="o">=</span> <span class="p">[</span>
<span class="n">path</span><span class="p">(</span><span class="s">'admin/'</span><span class="p">,</span> <span class="n">admin</span><span class="p">.</span><span class="n">site</span><span class="p">.</span><span class="n">urls</span><span class="p">),</span>
<span class="n">path</span><span class="p">(</span><span class="s">'hello/'</span><span class="p">,</span> <span class="n">include</span><span class="p">(</span><span class="s">"hello.urls"</span><span class="p">))</span>
<span class="p">]</span>
</code></pre></div> </div>
</li>
<li>By doing this, we’ve specified that when a user visits our site, and then in the search bar adds <code class="language-plaintext highlighter-rouge">/hello</code> to the URL, they’ll be redirected to the paths inside of our new application.</li>
</ol>
<p>Now, when I start my application using <code class="language-plaintext highlighter-rouge">python manage.py runserver</code> and visit the url provided, I’m met with this screen:
<img src="Lecture%203%20-%20CS50's%20Web%20Programming%20with%20Python%20and%20JavaScript_files/404.png" alt="wrong url">
But this is because we have only defined the URL <code class="language-plaintext highlighter-rouge">localhost:8000/hello</code>, but we haven’t defined the URL <code class="language-plaintext highlighter-rouge">localhost:8000</code> with nothing added to the end. So, when I add <code class="language-plaintext highlighter-rouge">/hello</code> to the URL in my search bar:
<img src="Lecture%203%20-%20CS50's%20Web%20Programming%20with%20Python%20and%20JavaScript_files/helloworld.png" alt="Hello, world">
Now that we’ve had some success, let’s go over what just happened to get us to that point:</p>
<ol>
<li>When we accessed the URL <code class="language-plaintext highlighter-rouge">localhost:8000/hello/</code>, Django looked at what came after the base URL (<code class="language-plaintext highlighter-rouge">localhost:8000/</code>) and went to our project’s <code class="language-plaintext highlighter-rouge">urls.py</code> file and searched for a pattern that matched <code class="language-plaintext highlighter-rouge">hello</code>.</li>
<li>It found that extension because we defined it, and saw that when met with that extension, it should <code class="language-plaintext highlighter-rouge">include</code> our <code class="language-plaintext highlighter-rouge">urls.py</code> file from within our application.</li>
<li>Then, Django ignored the parts of the URL it has already used in rerouting (<code class="language-plaintext highlighter-rouge">localhost:8000/hello/</code>, or all of it) and looked inside our other <code class="language-plaintext highlighter-rouge">urls.py</code> file for a pattern that matches the remaining part of the URL.</li>
<li>It found that our only path so far (<code class="language-plaintext highlighter-rouge">""</code>) matched what was left of the URL, and so it directed us to the function from <code class="language-plaintext highlighter-rouge">views.py</code> associated with that path.</li>
<li>Finally, Django ran that function within <code class="language-plaintext highlighter-rouge">views.py</code>, and returned the result (<code class="language-plaintext highlighter-rouge">HttpResponse("Hello, world!")</code>) to our web browser.</li>
</ol>
<p>Now, if we want to, we can change the <code class="language-plaintext highlighter-rouge">index</code> function within <code class="language-plaintext highlighter-rouge">views.py</code>
to return anything we want it to! We could even keep track of variables
and do calculations within the function before eventually returning
something.</p>
<p>Now, let’s take a look at how we can add more than one view to our
application. We can follow many of the same steps within our application
to create pages that say hello to Brian and David.</p>
<p>Inside <code class="language-plaintext highlighter-rouge">views.py</code>:</p>
<div class="language-python highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="kn">from</span> <span class="nn">django.shortcuts</span> <span class="kn">import</span> <span class="n">render</span>
<span class="kn">from</span> <span class="nn">django.http</span> <span class="kn">import</span> <span class="n">HttpResponse</span>
<span class="c1"># Create your views here.
</span>
<span class="k">def</span> <span class="nf">index</span><span class="p">(</span><span class="n">request</span><span class="p">):</span>
<span class="k">return</span> <span class="n">HttpResponse</span><span class="p">(</span><span class="s">"Hello, world!"</span><span class="p">)</span>
<span class="k">def</span> <span class="nf">brian</span><span class="p">(</span><span class="n">request</span><span class="p">):</span>
<span class="k">return</span> <span class="n">HttpResponse</span><span class="p">(</span><span class="s">"Hello, Brian!"</span><span class="p">)</span>
<span class="k">def</span> <span class="nf">david</span><span class="p">(</span><span class="n">request</span><span class="p">):</span>
<span class="k">return</span> <span class="n">HttpResponse</span><span class="p">(</span><span class="s">"Hello, David!"</span><span class="p">)</span>
</code></pre></div></div>
<p>Inside <code class="language-plaintext highlighter-rouge">urls.py</code> (within our application)</p>
<div class="language-python highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="kn">from</span> <span class="nn">django.urls</span> <span class="kn">import</span> <span class="n">path</span>
<span class="kn">from</span> <span class="nn">.</span> <span class="kn">import</span> <span class="n">views</span>
<span class="n">urlpatterns</span> <span class="o">=</span> <span class="p">[</span>
<span class="n">path</span><span class="p">(</span><span class="s">""</span><span class="p">,</span> <span class="n">views</span><span class="p">.</span><span class="n">index</span><span class="p">,</span> <span class="n">name</span><span class="o">=</span><span class="s">"index"</span><span class="p">),</span>
<span class="n">path</span><span class="p">(</span><span class="s">"brian"</span><span class="p">,</span> <span class="n">views</span><span class="p">.</span><span class="n">brian</span><span class="p">,</span> <span class="n">name</span><span class="o">=</span><span class="s">"brian"</span><span class="p">),</span>
<span class="n">path</span><span class="p">(</span><span class="s">"david"</span><span class="p">,</span> <span class="n">views</span><span class="p">.</span><span class="n">david</span><span class="p">,</span> <span class="n">name</span><span class="o">=</span><span class="s">"david"</span><span class="p">)</span>
<span class="p">]</span>
</code></pre></div></div>
<p>Now, our site remains unchanged when we visit <code class="language-plaintext highlighter-rouge">localhost:8000/hello</code>, but we get different pages when we add <code class="language-plaintext highlighter-rouge">brian</code> or <code class="language-plaintext highlighter-rouge">david</code> to the URL:
<img src="Lecture%203%20-%20CS50's%20Web%20Programming%20with%20Python%20and%20JavaScript_files/brian.png" alt="Brian">
<img src="Lecture%203%20-%20CS50's%20Web%20Programming%20with%20Python%20and%20JavaScript_files/david.png" alt="David"></p>
<p>Many sites are parameterized by items included in the URL. For example, going to <a href="https://twitter.com/cs50">www.twitter.com/cs50</a> will show you all of CS50’s tweets, and going to <a href="https://github.com/cs50">www.github.com/cs50</a> will bring you to CS50’s GitHub page. You can even find your own public GitHub repositories by navigating to <code class="language-plaintext highlighter-rouge">www.github.com/YOUR_USERNAME</code>!</p>
<p>In thinking about how this is implemented, it seems impossible that
sites like GitHub and Twitter would have an individual URL path for each