Querysets are cheap and queries (db hits) are expensive.
Models, Views, Urls and Templates, all you need to know to get started.
Views
Where you gather all the “stuff” that you want to display.
Smugmug exploit.
Your URL is a part of you application, you should design it. URLs are user interface. Some frameworks try to hide URLs, but URLs are part of the web and part of the UI.
URLs are “hackable”. You can edit a URL so you can move up a directory, etc. This used to be a “expert” feature, but nowadays people see a logic to it. People chop URLs to get there. Users are getting mor savvy.
Django doesn’t lock you in to a particular URL structure.
Object mapping
- Unlike Rails which will by default follow your controllers and build URLs automatically.
- Relationships defined
- URIs should be opaque. They should define a resource but not what’s behind it.
ROOT_URLCONF is where Django starts.
import * is evil unless the package is designed for it.
from django.conf.urls.defaults import *
urlpatters = patterns('', ...)
Regular expressions, full re power is there, but you probably won’t need it.
Tips:
- If you’re repeating yourself in patterns then you probably should move that out into a separate url file i.e. include(‘cheeserater.packages.urls’). Just don’t anchor the pattern with $.
- You can get a little performance increase to move common URLs to top of patterns list.
Dissecting a request /documentation/url_dispatch/
- GET /packages/Unipath/
- ROOT_URLCONF
- cheeserater.urls (walking down the patterns trying to match against the path)
- (r’^packages/’,…)
- include (‘cheeserater.packages.urls’)
- …
- call the view function with the first arg as request
A view function is a python function that takes a request object and returns a response object.
MVC differences
- Your view is responsible for collecting and collating data but does not include presentation data.
- Visual representation is in a template.
Using render_to_response() [view code example]
get_object_or_404(SomeModel, **kwargs)
try:
return SomeModel.objects.get(**kwargs)
except SomeModel.DoesNotExist:
raise Http404(...)
Templates
Glorified chunk of HTML with some variables.
- PHP is a template language.
- Ruby uses RHTML.
- Python has dozens.
- Django ships with one because we like it, you can use something else if you like. You can use a different one quite easily.
[showing example template]
Django template language has been designed for someone who is used to writing HTML. i.e. a Designer
The magic dot
- p[“name”]
- p.name
- p.name()
You shouldn’t have to tell your template designers if something is a function or method … cheetah template engine idea.
- Template filters
- filter chaining with | built-in filters
- Tags
- usually do something with flow and logic, i.e. for, include, if then … same tags require end tags and others don’t
Template inheritance “big whoop idea / feature”
- solves the problem of common code
- base template
- blocks / block tags
- holes or places that will get filled in later
- “the news hole” “i need three hundred words to fill this news hole”
- putting a title block in the base template and let the child define it’s own if it wants to.
[block.super example]
- Thought django template language was the stupidest idea when he first started at LJ world.
- Most people are used to includes, while you can do that, it’s not designed for that.
- [ inheritance example]
- base template (absolute smallest piece of design, stuff that never changes)
- might have base templates for each type of content.
- requires {% extends %} to be right at the beginning.
- typically 3 or 4 levels of inheritance
Why?
- Lawrence world was first site built with Django.
- It was one template before they rebuilt it.
- matching divs with includes sucks.
- every designer he’s worked with thinks template inheritance is brilliant.
Inheritance Tips
- {% extends %} must be the first thing in your template.
- More {% block %}s are better. Throw empty blocks in your template.
- i.e. {% block extrahead %}
- If you’re duplicating content then you’re missing a block.
- {{ block.super }} keep it in mind
- /documentation/templates/
- /documentation/templates_python/
Croyden (postneo.com) friend
- Wrote a good post about how LJ does mobile sites.
Generic Views
Saves time in doing common tasks.
What’s a generic view?
- early on they realized same stuff was being repeating.
- when copy and posting code, you know something is wrong.
- only writing parts of views that differ.
- pagination
- getting that right once and not thinking about it again.
- what does a list of objects do?
- takes a request and a queryset. (and has lots of options)
- generic views are just plain old python functions, just views.
- nothing that special about them other than just used alot and tested.
- [view code example]
- package_list(request, package_name):
- return list_detail.object_list(…)
- [template code example]
- include makes sense for pagination widget.
URLconf revisisted
(pattern, callback, extra)
Testing
Apology for being preachy.
“Tests are the Programmer’s stone, transmitting fear into boredom.” — Kent Beck
A good code base vs bade code base = quality of unit testing
Book: Test Driven Development (TDD)
“Never write any new code unless you have a failed test.” Drives Jacob crazy, TOO BORING.
“I didn’t do test driven developement. I do stupidity driven testing … I wait until I do something stupid, and then write tests to aoid doing it again.” — Titus Brown
“Never let one test break.” — Jacob
“Whatever happens, don’t let your test suite break, you’ll regret it …”
Testing Django
- load sample data
- deal with HTTP
- setup and teardown
- until recently was thought to be too hard
- rail shipped with a test framework
- stubs out test for you when you start a new app
- testing isn’t THAT hard
- inspired by rails except more pythonic
Doctest
Unit tests
- Fixtures
- loading data in a reliable manner
Test client
Email capture
- allows email to be captured so you don’t actually send out email
Doctests
- String inside triple quotes that looks like an interactive session.
- Both tests and documentation.
- Or create tests.py file.
- /documentation/testing/
- Siege (test requests/sec)
What else ya got?
ref: Report Lab (integrating PDF)
-
advocatedise liked this
-
dollopfebril liked this
-
erosionretro liked this
-
latexcutelesbian liked this
-
mandric posted this