Upload
andy-mckay
View
1.472
Download
2
Tags:
Embed Size (px)
DESCRIPTION
Some suggestions on where to look next when building that next Django site
Citation preview
You’ve done the Django tutorial.What next?
Andy McKayClearwind Consulting
@clearwind
build something
my opinions, likely wrong
1. Setting up2. Writing models3. Middle years4. Avoid5. Tools
Part I: Setting up
app vs project vs...
past caring
app directory
(a). base templates
(b). context processorhttp://docs.djangoproject.com/en/dev/ref/templates/api/#writing-your-own-
context-processors
def context(request): data = {} return data
(c). render shortcutseg: http://www.djangosnippets.org/snippets/1022/
(d). site constants
(e) your middlewarehttp://docs.djangoproject.com/en/1.1/topics/http/middleware/#writing-your-
own-middleware
(f) your auth. backendhttp://docs.djangoproject.com/en/dev/topics/auth/#topics-auth
(g) base classes
model base class
class Base(models.Model): class Meta: abstract = True
form base class
class Form(forms.Form):! pass
class ModelForm(forms.ModelForm):! pass
Part 2: Writing models
(a) fixtures
not just for testing
create data in admin
manage.py dumpdata
(b) migration
during development
reset scripthttp://www.agmweb.ca/blog/andy/2154/
drop db, create db, syncdb, load fixtures, start up server etc...
my workflow
1. create app within project2. define models3. access admin4. smoke test models5. alter, run “reset”6. access admin (back to 4 until happy)7. enter sample data8. dump data to fixtures9. add fixture loading to “reset”10. continue... write tests and retail view
at some point... beta?
Part 3: The middle years
(a) signals
great for decouplinghttp://docs.djangoproject.com/en/dev/ref/signals/
#ref-signals
signals.pre_save.connect(! add_profile, ! sender=User)
custom signalshttp://docs.djangoproject.com/en/dev/topics/signals/#defining-and-sending-
signals
(b) URLs
always use named urlshttp://docs.djangoproject.com/en/dev/topics/http/urls/#url
urlpatterns = patterns('', url(r'^index/$', index_view, name="main-view"), ...)
reverse(“main-view”)
{% url main-view %}
(c) class based viewshttp://www.slideshare.net/simon/
classbased-views-with-django
def boring(request): ... return some_html
class boring: def get(request): ... return some_html
Part 4: Avoid these
generic viewshttp://docs.djangoproject.com/en/dev/ref/generic-views/
push logic into urls,always need rewriting
custom template tags(maybe)
complicated templates
{% div %} {% dl %} {% dt %}Name{% enddt %} {% dd %}Definition{% enddd %} {% enddl %}{% enddiv %}
some of django.contrib...
comments
Part 5: Debug Tools
pdbhttp://docs.python.org/library/pdb.html
pdbhttp://docs.python.org/library/pdb.html
(not optional)
django debug toolbarhttp://github.com/robhudson/django-debug-toolbar
command extensionshttp://code.google.com/p/django-command-extensions/
Fin. Questions?
Andy McKayClearwind Consulting
@clearwind