The document discusses the Model-View-Controller (MVC) pattern and its core components - the model, view, and controller. It provides examples of how each component works together in a web application. The model handles the application's data and business logic. The view displays the data to the user. The controller links the model and view by handling user input and calling the model and view functions. The document also discusses related concepts like middleware that process data between components, caching for performance, and how MVC is applied in different frameworks and applications.
2. What is MVC?
● An amazing pattern that can be found
throughout just about any software
● Controller -> input
● Model -> throughput
● View -> output
● MVP/MVA/etc. are all MVC, just with
different names
● Not a design pattern
5. Model
● Raw data comes in, raw data goes out
● App specific code
● Not just storage, all business logic goes in
the model
● Can be reused across any controller
● Represents what computers want to give to
users
6. Controller
● Data transport
● Non app-specific, common across any app
● Http / request / response / headers / ip
address / cookies / etc.
● Represents link between human and
computer
7. Views
● Takes in raw data, returns 'visualized' data
● HTML, XML, JSON, JPEG
● Represents what humans want from the
cmputer
8. Controller -> model -> view ->
controller
def new_blog(request):
# {'author': 'bob', 'title': 'Title', 'body': 'Body'}
data = request.POST
blog = Blog.objects.create(**data)
html = render({'blog': blog}, 'blog.html')
return HttpResponse(html)
9. curl -d "title='my blog'&author=bob&body='my blog body'" http://myblog.com/new_blog
HTML minify
HttpRedirect("http://myblog.com/view/%d" % blog.id))
Blog.objects.create()
"http://myblog.com/view/%d" % blog.id
python manage.py new_blog --title='My blog' --author="bob" --body="my blog body"
10. Input Middleware
● Mix between Controller and Model
● Processes the data stream from controller
● Examples: Authentication middleware
● View hs no idea about Input Middleware
12. Output Middleware
● Modifies the data stream from the controller
after the view
● Examples: HTTP minify middleware
● Completely hidden from the model
13. HTML Minifier
def minify_middleware(response):
if 'html' in response.mimetype:
minified = response.content.replace(' ', '')
response.content = minified
return response
else:
return response
14. Cache
● Short circuit of Model and View
● Performance improvement when you can get
away with returning pre-rendered data+html
● Primary usage of key/value stores
● Performance comes with a price, cache
invalidation is tough
15. Cache
def view_blog(request, id):
key = "html_blog_%s" % id
html = memcache.get(key)
if html:
return html
else:
blog = Blog.objects.get(id=id)
html = render('blog.html', blog)
return memcache.set(html, key)
17. MVC is everywhere!
Blog.objects.get
def get(self, id):
table_name = 'blog'
engine = settings.DB_ENGINE
q = "select * from %s where id=%s" % (
table_name, id
)
data = engine.execute(q)
return Blog(**data)
18. I mean, everywhere!
render()
def render(template, context, unicode=True)
t = open(template).read()
html = do_render(t, context)
if unicode:
return unicode(html)
else:
return html
19. Try not to mix colors
● Keep model code isolated from controller
and view code
● Coupling is bad
● Reusability, discoverability, and predictability
● If you have to mix colors, its OK! Just
remember to fix it later!
20. Giotto
● Application framework that does away with
controllers
● Not at all like django/pyramid/flask
● Completely decoupled models and views
● https://github.com/priestc/giotto