accelerating information technology innovation ghana summer 2013 django lecture 5 - models
TRANSCRIPT
![Page 1: Accelerating Information Technology Innovation Ghana Summer 2013 Django Lecture 5 - Models](https://reader030.vdocuments.site/reader030/viewer/2022032517/56649cab5503460f9496cdcb/html5/thumbnails/1.jpg)
Accelerating Information Technology
Innovationhttp://aiti.mit.edu
Ghana Summer 2013Django Lecture 5 - Models
![Page 2: Accelerating Information Technology Innovation Ghana Summer 2013 Django Lecture 5 - Models](https://reader030.vdocuments.site/reader030/viewer/2022032517/56649cab5503460f9496cdcb/html5/thumbnails/2.jpg)
How Django works
BlogComment
Templates
Give me blog post # 4 and all its comments
and put it in the detail template
Title
Body
Comment
Comment
Date
Comment author
Comment author
ModelsViews
localhost:8000/blog/detail/4
urls.
py
![Page 3: Accelerating Information Technology Innovation Ghana Summer 2013 Django Lecture 5 - Models](https://reader030.vdocuments.site/reader030/viewer/2022032517/56649cab5503460f9496cdcb/html5/thumbnails/3.jpg)
Today: Views and urls.py
BlogComment
Templates
Give me blog post # 4 and all its comments
and put it in the detail template
Title
Body
Comment
Comment
Date
Comment author
Comment author
ModelsViews
localhost:8000/blog/detail/4
urls.
py
![Page 4: Accelerating Information Technology Innovation Ghana Summer 2013 Django Lecture 5 - Models](https://reader030.vdocuments.site/reader030/viewer/2022032517/56649cab5503460f9496cdcb/html5/thumbnails/4.jpg)
Today’s lab: search your blog
Blog
Template: search.html
Give me blog posts that match the search term and put them in the search.html
template
Term: term
Matching blog posts:Some blog postSome other blog post
ModelsViews
localhost:8000/search/<term>
urls.
py
![Page 5: Accelerating Information Technology Innovation Ghana Summer 2013 Django Lecture 5 - Models](https://reader030.vdocuments.site/reader030/viewer/2022032517/56649cab5503460f9496cdcb/html5/thumbnails/5.jpg)
Views
• Get data from Model• Choose a template to display• Return template and data
![Page 6: Accelerating Information Technology Innovation Ghana Summer 2013 Django Lecture 5 - Models](https://reader030.vdocuments.site/reader030/viewer/2022032517/56649cab5503460f9496cdcb/html5/thumbnails/6.jpg)
Views: list.html
def blog_list(request): blog_list = Blog.objects.all() t = loader.get_template('blog/list.html') c = Context({'blog_list':blog_list}) return HttpResponse(t.render(c))
• Get list of all blogs• Put it in the list.html template
![Page 7: Accelerating Information Technology Innovation Ghana Summer 2013 Django Lecture 5 - Models](https://reader030.vdocuments.site/reader030/viewer/2022032517/56649cab5503460f9496cdcb/html5/thumbnails/7.jpg)
Data from Model
def blog_list(request): blog_list = Blog.objects.all() t = loader.get_template('blog/list.html') c = Context({'blog_list':blog_list}) return HttpResponse(t.render(c))
![Page 8: Accelerating Information Technology Innovation Ghana Summer 2013 Django Lecture 5 - Models](https://reader030.vdocuments.site/reader030/viewer/2022032517/56649cab5503460f9496cdcb/html5/thumbnails/8.jpg)
Choose template
def blog_list(request): blog_list = Blog.objects.all() t = loader.get_template('blog/list.html') c = Context({'blog_list':blog_list}) return HttpResponse(t.render(c))
![Page 9: Accelerating Information Technology Innovation Ghana Summer 2013 Django Lecture 5 - Models](https://reader030.vdocuments.site/reader030/viewer/2022032517/56649cab5503460f9496cdcb/html5/thumbnails/9.jpg)
Name variables to be used in template
def blog_list(request): blog_list = Blog.objects.all() t = loader.get_template('blog/list.html') c = Context({'blog_list':blog_list}) return HttpResponse(t.render(c))
![Page 10: Accelerating Information Technology Innovation Ghana Summer 2013 Django Lecture 5 - Models](https://reader030.vdocuments.site/reader030/viewer/2022032517/56649cab5503460f9496cdcb/html5/thumbnails/10.jpg)
Return template with data
def blog_list(request): blog_list = Blog.objects.all() t = loader.get_template('blog/list.html') c = Context({'blog_list':blog_list}) return HttpResponse(t.render(c))
![Page 11: Accelerating Information Technology Innovation Ghana Summer 2013 Django Lecture 5 - Models](https://reader030.vdocuments.site/reader030/viewer/2022032517/56649cab5503460f9496cdcb/html5/thumbnails/11.jpg)
Getting data from Models
• Always start with Blog.objects
• Get all blogsBlog.objects.all()
• Get a specific blogBlog.objects.get(id=4)
![Page 12: Accelerating Information Technology Innovation Ghana Summer 2013 Django Lecture 5 - Models](https://reader030.vdocuments.site/reader030/viewer/2022032517/56649cab5503460f9496cdcb/html5/thumbnails/12.jpg)
Filters
• Get only specific data
contains: find if a match is contained inside a fieldBlog.objects.filter(body__contains='cool')
icontains: case insensitive containsBlog.objects.filter(author__icontains='smith’)
![Page 13: Accelerating Information Technology Innovation Ghana Summer 2013 Django Lecture 5 - Models](https://reader030.vdocuments.site/reader030/viewer/2022032517/56649cab5503460f9496cdcb/html5/thumbnails/13.jpg)
Filter syntax
• Blog.objects.filter(body__contains='cool’)
• which field you’re searching (body, title, author)
• 2 underscores• filter: contains/icontains• =• text of search term
![Page 14: Accelerating Information Technology Innovation Ghana Summer 2013 Django Lecture 5 - Models](https://reader030.vdocuments.site/reader030/viewer/2022032517/56649cab5503460f9496cdcb/html5/thumbnails/14.jpg)
View to get blogs containing the word AITI
def get_blogs_about_AITI(request): blog_list = Blog.objects.filter(body__contains=“AITI”)
t = loader.get_template('blog/list.html') c = Context({'blog_list':blog_list}) return HttpResponse(t.render(c))
![Page 15: Accelerating Information Technology Innovation Ghana Summer 2013 Django Lecture 5 - Models](https://reader030.vdocuments.site/reader030/viewer/2022032517/56649cab5503460f9496cdcb/html5/thumbnails/15.jpg)
Ordering
• Blog.objects.order_by(‘pub_date’)
![Page 16: Accelerating Information Technology Innovation Ghana Summer 2013 Django Lecture 5 - Models](https://reader030.vdocuments.site/reader030/viewer/2022032517/56649cab5503460f9496cdcb/html5/thumbnails/16.jpg)
Getting data from model
• detail.html needs a specific blog, and all comments from that blog
• view for detail.html:
def blog_detail(request, id):blog = Blog.objects.get(id=id)comments =
Comment.objects.filter(post=blog)
![Page 17: Accelerating Information Technology Innovation Ghana Summer 2013 Django Lecture 5 - Models](https://reader030.vdocuments.site/reader030/viewer/2022032517/56649cab5503460f9496cdcb/html5/thumbnails/17.jpg)
Name variables to be used in templates
def blog_detail(request, id):blog = Blog.objects.get(id=id)comments = Comment.objects.filter(post=blog)c = Context({'blog':blog, 'comments':comments})
![Page 18: Accelerating Information Technology Innovation Ghana Summer 2013 Django Lecture 5 - Models](https://reader030.vdocuments.site/reader030/viewer/2022032517/56649cab5503460f9496cdcb/html5/thumbnails/18.jpg)
Lab: view for blog_search
• Select all blogs containing the search term• Choose template to load it into• Name variables to be used in template
def blog_search(request, search_term):
![Page 19: Accelerating Information Technology Innovation Ghana Summer 2013 Django Lecture 5 - Models](https://reader030.vdocuments.site/reader030/viewer/2022032517/56649cab5503460f9496cdcb/html5/thumbnails/19.jpg)
Regular Expression• Patterns that text strings can match
B A _ _
BALL
BACK
TALL
BABOON
![Page 20: Accelerating Information Technology Innovation Ghana Summer 2013 Django Lecture 5 - Models](https://reader030.vdocuments.site/reader030/viewer/2022032517/56649cab5503460f9496cdcb/html5/thumbnails/20.jpg)
Regular Expression• Patterns that text strings can match
“Any number of digits”
123
314589203
123abc
BABOON
![Page 21: Accelerating Information Technology Innovation Ghana Summer 2013 Django Lecture 5 - Models](https://reader030.vdocuments.site/reader030/viewer/2022032517/56649cab5503460f9496cdcb/html5/thumbnails/21.jpg)
How to define these patterns?
• Special characters
![Page 22: Accelerating Information Technology Innovation Ghana Summer 2013 Django Lecture 5 - Models](https://reader030.vdocuments.site/reader030/viewer/2022032517/56649cab5503460f9496cdcb/html5/thumbnails/22.jpg)
Start and end of line
• ^ means start of line• $ means end of line
^hello$hello
hello world
![Page 23: Accelerating Information Technology Innovation Ghana Summer 2013 Django Lecture 5 - Models](https://reader030.vdocuments.site/reader030/viewer/2022032517/56649cab5503460f9496cdcb/html5/thumbnails/23.jpg)
Characters
• . matches anything!
.1
a
8
z
![Page 24: Accelerating Information Technology Innovation Ghana Summer 2013 Django Lecture 5 - Models](https://reader030.vdocuments.site/reader030/viewer/2022032517/56649cab5503460f9496cdcb/html5/thumbnails/24.jpg)
Characters
• . matches anything!
^ . . . $193
a
812
abcd
![Page 25: Accelerating Information Technology Innovation Ghana Summer 2013 Django Lecture 5 - Models](https://reader030.vdocuments.site/reader030/viewer/2022032517/56649cab5503460f9496cdcb/html5/thumbnails/25.jpg)
Characters
• \d matches digits
\d1
a
8
z
![Page 26: Accelerating Information Technology Innovation Ghana Summer 2013 Django Lecture 5 - Models](https://reader030.vdocuments.site/reader030/viewer/2022032517/56649cab5503460f9496cdcb/html5/thumbnails/26.jpg)
Characters
• \w matches letters and numbers
\wA
a
8
%
![Page 27: Accelerating Information Technology Innovation Ghana Summer 2013 Django Lecture 5 - Models](https://reader030.vdocuments.site/reader030/viewer/2022032517/56649cab5503460f9496cdcb/html5/thumbnails/27.jpg)
B A _ _
BALL
BACK
TALL
BABOON
![Page 28: Accelerating Information Technology Innovation Ghana Summer 2013 Django Lecture 5 - Models](https://reader030.vdocuments.site/reader030/viewer/2022032517/56649cab5503460f9496cdcb/html5/thumbnails/28.jpg)
Repetition
• * means zero or more times
a*
a
aaaaaa
![Page 29: Accelerating Information Technology Innovation Ghana Summer 2013 Django Lecture 5 - Models](https://reader030.vdocuments.site/reader030/viewer/2022032517/56649cab5503460f9496cdcb/html5/thumbnails/29.jpg)
Repetition
• + means one or more times
a+
a
aaaaaa
![Page 30: Accelerating Information Technology Innovation Ghana Summer 2013 Django Lecture 5 - Models](https://reader030.vdocuments.site/reader030/viewer/2022032517/56649cab5503460f9496cdcb/html5/thumbnails/30.jpg)
Repetition
• ? means exactly 0 or 1 times
a?
a
aaaaaa
![Page 31: Accelerating Information Technology Innovation Ghana Summer 2013 Django Lecture 5 - Models](https://reader030.vdocuments.site/reader030/viewer/2022032517/56649cab5503460f9496cdcb/html5/thumbnails/31.jpg)
Practice: which ones match the regex?
\d+
a
1
123
![Page 32: Accelerating Information Technology Innovation Ghana Summer 2013 Django Lecture 5 - Models](https://reader030.vdocuments.site/reader030/viewer/2022032517/56649cab5503460f9496cdcb/html5/thumbnails/32.jpg)
\d+
a
1
123
Practice: which ones match the regex?
![Page 33: Accelerating Information Technology Innovation Ghana Summer 2013 Django Lecture 5 - Models](https://reader030.vdocuments.site/reader030/viewer/2022032517/56649cab5503460f9496cdcb/html5/thumbnails/33.jpg)
Practice: which ones match the regex?
.*
a
1
123abc
![Page 34: Accelerating Information Technology Innovation Ghana Summer 2013 Django Lecture 5 - Models](https://reader030.vdocuments.site/reader030/viewer/2022032517/56649cab5503460f9496cdcb/html5/thumbnails/34.jpg)
Practice: which ones match the regex?
.*
a
1
123abc
![Page 35: Accelerating Information Technology Innovation Ghana Summer 2013 Django Lecture 5 - Models](https://reader030.vdocuments.site/reader030/viewer/2022032517/56649cab5503460f9496cdcb/html5/thumbnails/35.jpg)
• | means “or”
(hello|bye)
hello
bye
potato
![Page 36: Accelerating Information Technology Innovation Ghana Summer 2013 Django Lecture 5 - Models](https://reader030.vdocuments.site/reader030/viewer/2022032517/56649cab5503460f9496cdcb/html5/thumbnails/36.jpg)
• ( ) means grouping
(hello)+
hello
hellohello
hellothere
![Page 37: Accelerating Information Technology Innovation Ghana Summer 2013 Django Lecture 5 - Models](https://reader030.vdocuments.site/reader030/viewer/2022032517/56649cab5503460f9496cdcb/html5/thumbnails/37.jpg)
urls.py
• urls.py uses regular expressions to describe urls
• All start with localhost:8000/blog/
url(r'^$', 'blog.views.home’)
![Page 38: Accelerating Information Technology Innovation Ghana Summer 2013 Django Lecture 5 - Models](https://reader030.vdocuments.site/reader030/viewer/2022032517/56649cab5503460f9496cdcb/html5/thumbnails/38.jpg)
urls.py
• urls.py uses regular expressions to describe urls
• All urls start with localhost:8000/blog/
• Format: url(r'regex', ’view’)• Example: url(r'^$', 'blog.views.home’)
![Page 39: Accelerating Information Technology Innovation Ghana Summer 2013 Django Lecture 5 - Models](https://reader030.vdocuments.site/reader030/viewer/2022032517/56649cab5503460f9496cdcb/html5/thumbnails/39.jpg)
urls.py
url(r'^$', 'blog.views.home’)url(r'^list/$', 'blog.views.blog_list'),url(r'^(detail|info)/(\d+)$', 'blog.views.blog_detail'),
Things in parentheses are passed into the views function!
![Page 40: Accelerating Information Technology Innovation Ghana Summer 2013 Django Lecture 5 - Models](https://reader030.vdocuments.site/reader030/viewer/2022032517/56649cab5503460f9496cdcb/html5/thumbnails/40.jpg)
url(r'^$', 'blog.views.home’)
^$ 1
hello
localhost:8000/blog/
![Page 41: Accelerating Information Technology Innovation Ghana Summer 2013 Django Lecture 5 - Models](https://reader030.vdocuments.site/reader030/viewer/2022032517/56649cab5503460f9496cdcb/html5/thumbnails/41.jpg)
url(r'^list/$', 'blog.views.home’)
^list/$
list/
1
hello
localhost:8000/blog/list
![Page 42: Accelerating Information Technology Innovation Ghana Summer 2013 Django Lecture 5 - Models](https://reader030.vdocuments.site/reader030/viewer/2022032517/56649cab5503460f9496cdcb/html5/thumbnails/42.jpg)
url(r'^(detail|info)/(\d+)$', 'blog.views.home’)
^(detail|info)/(\d+)$
info/1
detail/
list
localhost:8000/blog/detail/123
detail/123
![Page 43: Accelerating Information Technology Innovation Ghana Summer 2013 Django Lecture 5 - Models](https://reader030.vdocuments.site/reader030/viewer/2022032517/56649cab5503460f9496cdcb/html5/thumbnails/43.jpg)
url(r'^search/(happy)$', 'blog.views.blog_search')
^search/(happy)$
search/happy
search/aiti
hello
localhost:8000/search/happy
![Page 44: Accelerating Information Technology Innovation Ghana Summer 2013 Django Lecture 5 - Models](https://reader030.vdocuments.site/reader030/viewer/2022032517/56649cab5503460f9496cdcb/html5/thumbnails/44.jpg)
url(r'^search/(happy)$', 'blog.views.blog_search')
Lab: change this regex to match any search term, not just “happy”
![Page 45: Accelerating Information Technology Innovation Ghana Summer 2013 Django Lecture 5 - Models](https://reader030.vdocuments.site/reader030/viewer/2022032517/56649cab5503460f9496cdcb/html5/thumbnails/45.jpg)
Today’s lab: search your blog
Blog
Template: search.html
Give me blog posts that match the search term and put them in the search.html
template
Term: term
Matching blog posts:Some blog postSome other blog post
ModelsViews
localhost:8000/search/<term>
urls.
py
![Page 46: Accelerating Information Technology Innovation Ghana Summer 2013 Django Lecture 5 - Models](https://reader030.vdocuments.site/reader030/viewer/2022032517/56649cab5503460f9496cdcb/html5/thumbnails/46.jpg)
Lab• Create a way to search for text in your blog
posts• view• urls• template
• Read all instructions before starting the lab!• If you haven’t finished the last lab, still move on
to this lab.