Accelerating
This presentation is the property of its rightful owner.
Sponsored Links
1 / 46

Accelerating Information Technology Innovation PowerPoint PPT Presentation


  • 38 Views
  • Uploaded on
  • Presentation posted in: General

Accelerating Information Technology Innovation. http://aiti.mit.edu. Ghana Summer 2013 Django Lecture 5 - Models. How Django works. localhost:8000/blog/detail/4. Templates. urls.py. Models. T itle. Views. Blog Comment. Date. Body.

Download Presentation

Accelerating Information Technology Innovation

An Image/Link below is provided (as is) to download presentation

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.


- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -

Presentation Transcript


Accelerating information technology innovation

Accelerating

Information Technology

Innovation

http://aiti.mit.edu

Ghana Summer 2013

Django Lecture 5 - Models


How django works

How Django works

localhost:8000/blog/detail/4

Templates

urls.py

Models

Title

Views

Blog

Comment

Date

Body

Give me blog post # 4 and all its comments and put it in the detail template

Comment author

Comment

Comment author

Comment


Today views and urls py

Today: Views and urls.py

localhost:8000/blog/detail/4

Templates

urls.py

Models

Title

Views

Blog

Comment

Date

Body

Give me blog post # 4 and all its comments and put it in the detail template

Comment author

Comment

Comment author

Comment


Today s lab search your blog

Today’s lab: search your blog

localhost:8000/search/<term>

Template: search.html

urls.py

Models

Views

Term: term

Matching blog posts:

Some blog post

Some other blog post

Blog

Give me blog posts that match the search term and put them in the search.html template


Views

Views

  • Get data from Model

  • Choose a template to display

  • Return template and data


Views list html

Views: list.html

  • Get list of all blogs

  • Put it in the list.html template

defblog_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))


Data from model

Data from Model

defblog_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))


Choose template

Choose template

defblog_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))


Name variables to be used in template

Name variables to be used in template

defblog_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))


Return template with data

Return template with data

defblog_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))


Getting data from models

Getting data from Models

  • Always start with Blog.objects

  • Get all blogs

    Blog.objects.all()

  • Get a specific blog

    Blog.objects.get(id=4)


Filters

Filters

  • Get only specific data

  • contains: find if a match is contained inside a field

    • Blog.objects.filter(body__contains='cool')

  • icontains: case insensitive contains

    • Blog.objects.filter(author__icontains='smith’)


Filter syntax

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


View to get blogs containing the word aiti

View to get blogs containing the word AITI

defget_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))


Ordering

Ordering

  • Blog.objects.order_by(‘pub_date’)


Getting data from model

Getting data from model

  • detail.html needs a specific blog, and all comments from that blog

  • view for detail.html:

    defblog_detail(request, id):

    blog = Blog.objects.get(id=id)

    comments = Comment.objects.filter(post=blog)


Name variables to be used in templates

Name variables to be used in templates

defblog_detail(request, id):

blog = Blog.objects.get(id=id)

comments = Comment.objects.filter(post=blog)

c = Context({'blog':blog, 'comments':comments})


Lab view for blog search

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

    defblog_search(request, search_term):


Regular expression

Regular Expression

  • Patterns that text strings can match

BALL

BACK

BA _ _

TALL

BABOON


Regular expression1

Regular Expression

  • Patterns that text strings can match

123

“Any number of digits”

314589203

123abc

BABOON


How to define these patterns

How to define these patterns?

  • Special characters


Start and end of line

Start and end of line

  • ^ means start of line

  • $ means end of line

hello

^hello$

hello world


Characters

Characters

  • . matches anything!

8

1

.

a

z


Characters1

Characters

  • . matches anything!

812

193

^ . . . $

a

abcd


Characters2

Characters

  • \d matches digits

8

1

\d

a

z


Characters3

Characters

  • \w matches letters and numbers

8

A

\w

a

%


Accelerating information technology innovation

BALL

BACK

BA _ _

TALL

BABOON


Repetition

Repetition

  • * means zero or more times

a

a*

aaaaaa


Repetition1

Repetition

  • + means one or more times

a

a+

aaaaaa


Repetition2

Repetition

  • ? means exactly 0 or 1 times

a

a?

aaaaaa


Practice which ones match the regex

Practice: which ones match the regex?

a

\d+

1

123


Accelerating information technology innovation

Practice: which ones match the regex?

a

\d+

1

123


Practice which ones match the regex1

Practice: which ones match the regex?

a

1

.*

123abc


Practice which ones match the regex2

Practice: which ones match the regex?

a

1

.*

123abc


Accelerating information technology innovation

  • | means “or”

hello

(hello|bye)

bye

potato


Accelerating information technology innovation

  • ( ) means grouping

hello

(hello)+

hellohello

hellothere


Urls py

urls.py

  • urls.py uses regular expressions to describe urls

  • All start with localhost:8000/blog/

    url(r'^$', 'blog.views.home’)


Urls py1

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’)


Urls py2

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!


Url r blog views home

url(r'^$', 'blog.views.home’)

^$

1

hello

localhost:8000/blog/


Url r list blog views home

url(r'^list/$', 'blog.views.home’)

list/

^list/$

1

hello

localhost:8000/blog/list


Url r detail info d blog views home

url(r'^(detail|info)/(\d+)$', 'blog.views.home’)

detail/123

info/1

^(detail|info)/(\d+)$

detail/

list

localhost:8000/blog/detail/123


Url r search happy blog views blog search

url(r'^search/(happy)$', 'blog.views.blog_search')

search/happy

^search/(happy)$

search/aiti

hello

localhost:8000/search/happy


Url r search happy blog views blog search1

url(r'^search/(happy)$', 'blog.views.blog_search')

Lab: change this regex to match any search term, not just “happy”


Today s lab search your blog1

Today’s lab: search your blog

localhost:8000/search/<term>

Template: search.html

urls.py

Models

Views

Term: term

Matching blog posts:

Some blog post

Some other blog post

Blog

Give me blog posts that match the search term and put them in the search.html template


Accelerating information technology innovation

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.


  • Login