# (pound sign) for Python comments, 13
% extends % tag, 99
?P construct, 66
__import__() function, 117
__unicode_ () method, 34, 62, 198
_default_manager, 120
+= (plus equal) in Link model, 85
% free_comment_form % tag, 126
% if_bookmarked % template tag, 191–192
% load % tag, 128
% url % template tag, 102, 232
500 Internal Server Error, 78
abstract inheritance, 124
activate script, 211
add() function, arguments for, 79
add_snippet view, 178–179, 184
admin application
adding new flat page to, 15–16
admin documentation system, 22
admin form
for adding a category, 51
for search keywords, 36
admin interface, adding categories to, 51
admin/ URL pattern, 13
admin/change_form.html template, 25
admin/flatpages/change_form.html template, 25
admin/flatpages/flatpage/change_form.html template, 25
administrative interface
home page, 13
templates/ directory use by, 19
admin.py files, 156
ADMINS setting, 136
aggregate method, 199
aggregate queries, 160
Akismet API key, 131
Akismet class
importing, 132
methods in, 132
annotate method, 161
APIs, Django, 233
applications
building for flexibility, 228
reasons to make separate, 228
recognizing need to spin off new, 227
techniques for developing reusable, 224
tightly focused, 225
archive_day, 71
archive_index, 71
archive_month, 71
archive_year, 71
arguments, supplying default value for new, 230
as_li() method, 183
as_p() method, 182
as_ul() method, 182
associative arrays, 27
AUTHORS/AUTHORS.txt file, 237
base_entries.html template, 103
base_links.html template, 104
base_tags.html template, 104
BaseCommentAbstractModel, 123
base.html template, 98
bisecting feature (VCS), 221
Bitbucket, 208
blank fields vs. null fields, 54
block.super variable, 99
blog
adding block in body tag, 102
blog_url keyword argument, 132
filling in header for, 101
sidebar block, 109
Bookmark model
querying for user's bookmarks, 188
running queries on, 188
bookmark_set attribute, 188
bookmarking
basic bookmark views, 188
deleting bookmarks, 189
BooleanField, 56
braces, using in template tags, 126
brochureware sites, 9
bytecode, Python storage of, 8
cab/models.py, 187
cab/snippet_form.html template, 184
cab/templatetags/snippets.py, 192
cab/urls/popular.py, 163
cab/urls/snippets.py
adding new URL pattern to, 182
changing import line in, 184
cab/views/popular.py, 163
cab/views/snippets.py file
adding imports for editing snippets, 183
finished, 185
categories
Category model, 95
Category object, 85
CategoryAdmin class, 50
considerations for showing, 108
looping over, 108
categorization for Link model, 77
categorized feeds
adding items to import statements, 144
problems associated with, 144
writing feed class for, 144
change_form.html template, 25
CHANGELOG/CHANGELOG.txt files, 237
CharField, 57
choices option, 57
clean() method, 171
clean() validation method, 168
clean_username() method, 167
cleaned_data dictionary, 171
CMS project
choosing template directory for, 19–20
customizing simple, 23
cms subdirectory, creating, 6
code
coupled to projects, 234
form for adding snippets, 174–176
version control systems to track, 205–209
coltrane_tags.py file, 114
coltrane/entry_archive_year.html template, 105
coltrane/entry_archive.html template, 104
coltrane/entry_detail.html template
adding comment form header to, 125
creating, 68
editing, 99
coltrane/link_detail.html template, 99, 111
commas, trailing, 20
Comment model, 123
comment moderator, 130
comment_check() method, 133
in akismet module, 132
arguments expected by, 132
commenting, 123
comment-moderation function, 134
comment-moderation system, features of, 138–140
CommentModerator class, 233
comments
allowing and disallowing, 56
comments tag library, 125
comment-submission system, 129
e-mail notification of, 135
moderating, 129
Python, 13
retrieving and displaying, 127
commit=False on forms, 182
compilation function
changing to retrieve model class, 118
error checking, 119
problems with changing, 118
writing for % if_bookmarked % tag, 193
concrete inheritance, 124
connect method, 130
contact_form view
argument, rewriting, 232
effect of passing right arguments to, 231–232
contact-form application
building for flexibility, 228–229
flexible post-form processing, 231–232
simple view for processing, 229
Context class, 27
context processor function, 111, 196
conversion programs, HTML, 60
count() method (QuerySet), 40
create() method, 176
create_user() method, 169
cross-site request forgery (CSRF), 189–190
custom tags
creating simple, 113
registering new, 114
writing compilation function for, 113
databases
using different, 10
date filter for weblog application, 66
date-based archives, 70
date-based constraints supported by Django, 55
datetime class, 53
datetime module, 154
DateTimeField, 53
.db file extension, 10
DEBUG setting (Django), 218–219
decorators, 74
def keyword, 28
default.html file, 20
del.icio.us, 77
development process, Django
relative paths in settings, 217–218
settings changing with environments, 218–219
unit-testing applications, 219–221
distinct() method, 38
distributing Django applications, 234
distutils module
for distributing Django applications, 234
writing setup.py script with, 235–236
Django
accessing settings file, 80
APIs, 233
building first site, 9
built-in management script, 6
contrib applications in, 12
database lookup syntax, 29
default site object created by, 15
development of, 2
DJANGO_SETTINGS_MODULE environment variable, 215–216
django.contrib, 123
django.contrib.admin application, 12
django.contrib.comments, 123, 126, 127
django.contrib.comments.moderation. CommentModerator, 138
django.contrib.flatpages application, 12
django.contrib.syndication application, 123
django.contrib.syndication.feeds.Feed class, 141
django.core.mail, 137
django.core.urlresolvers.reverse() function, 232
django.db.models.Count, 160
django.db.models.get_model() function, 117
django.db.models.permalink(), 232
django.db.models.Sum filter, 198
django.shortcuts.render_to_response function, 31
django-tagging application, 59
django.template module, 18
django.template.Context class, 196
django.template.loader.select_template, 25
django.template.Node, 114
django.template.Template class, 112
django.utils.encoding.smart_str() function, 80, 133
django.views.generic.date_based module, 70
django.views.generic.list_detail module, 86
documentation displayed within, 238–239
flat page example, 20
handling of database queries, 94
introduction to, 5
looking ahead, 8
packaged releases vs. development code, 4
process for loading templates, 112
server error page, 17
use of customized extensions to reST syntax, 240
Django applications
distributing, 234
what to document in, 239
writing, 44
Django project
changing address and port, 6
creating, 5
exploring, 8
Django templates
for categories, 109
parsing ahead in, 192
workings of, 112
documentation
for distributable applications, 237
links, 22
editing
edit_snippet view, 197
sending from within Django, 136
verifying settings for, 136
entries
adding list of latest, 115
categorizing and tagging, 58
entries.py file, breaking down, 91
entry detail, 107
entry templates, 72
entry_detail template, 97
entry_detail view, 67
entry_detail.html template, 127–128
entry_info_dict variable, 69
Entry.HIDDEN_STATUS, 58
Entry.LIVE_STATUS, 58
Entry.objects.all(), 70
querying with status field set to Live, 93
types of, 57
writing without HTML, 60
error checking, 117
excerpt field, 54
feature creep, 224
feed class example, 142
feed GUID, 142
feeds
adding to weblog application, 140
directory, creating files in, 143
fields
classes in django.newforms module, 165
core needed for Link model, 77–78
filter() method
in QuerySet, 40
using for entries, 93
:filter shortcut, 241
fixtures (files), 221
FlatPage object, 21
flatpages/default.html template, 30
flatpage.title variable, 21
Foord, Michael, 132
ForeignKey field, 55
for/endfor tags, 30
Form class
adding fields to, 175
in django.newforms module, 165
forms
for adding code snippets, 174
adding custom _init_() method to, 174
fields, requirement for, 168
form and /form tags, 182
form-handling code in django.newforms module, 165
forms.py, 175
generating from model definition, 179–182
processing in code-sharing application, 165
rendering into different types of HTML, 182
simplifying templates that display, 182–183
full_clean() method, 171
functions vs. return values, 55
get_absolute_url() method
adding to admin interface, 62
adding to model, 52
defining, 151
on Entry model, 66
rewriting on Entry model, 74
get_comment_list tag, 137
get_content_object() method, 131
get_object() method, 144
get_object_or_404() function, 68
GitHub, 208
Google Project Hosting, 208
GUIDs (globally unique identifiers), 141–142
hash tables, 27
help_text
adding to admin interface fields, 62
argument, adding to field in model, 51
hidden option, 58
highlighted code, styling, 158
History button on flat page, 16
hosting options (VCS), 208
HTML, adding fields for storing, 60
HTTP (HyperText Transfer Protocol)
headers, 134
HttpResponse class, 27
HttpResponseForbidden class, 183
idempotent HTTP methods, 190
IfRatedNode, 200
import statement, changing for Rating model, 200
include() directives, 90
include() function, 72
index page, items listed on, 15
inheritance
abstract, 124
concrete, 124
INSTALL or INSTALL.txt file, 237
INSTALLED_APPS setting
adding coltrane application to, 48
changing, 11
instance argument, 131
IntegerField, 57
is_public field, 131
item_pubdate() method, 141
adding to the feed class, 141
changing for categorized feeds, 145
jscripts/ directory, 23
keyword arguments
in Python, 68
unique constraint generated by, 78
keywords
improving CMS search function with, 33
keyword field in Django data model, 34
keyword_results[0].get_absolute_url() method, 40
LatestContentNode, writing, 119–120
LatestEntriesFeed, setting up, 140
lexers in pygments download, 151
LICENSE/LICENSE.txt file, 236
linebreaks filter, 127
Link model
adding customized save() method to, 79
adding foreign key to, 78
adding more patterns to, 88
basic core fields for, 77
defining dictionary for generic views, 83
full model definition, 81
installing database table for, 81
link_detail template, 98
link-aggregation service, 78
template used for generic view, 97
using _unicode_() method with, 79
loader module, 27
login_required decorator, 177–178
login/logout views, 178
magic numbers, 58
mail_managers() function, 136–137
manage.py file, 7
manage.py startapp command, 45
manage.py syncdb
database tables created by, 13
installing Category model table with, 48
running, 12
running to install model into database, 188
managers
in Django model system, 94
Manager, writing subclass of, 94
MANAGERS setting, 136
managers.py file, 161
many-to-many relationships
commit=False and, 182
how they work, 59
markdown filter, 127
media files, 24
Mercurial: The Definitive Guide, 209
Meta class, 50
metadata, 78
mod_wsgi module, 211
ModelForm class
adding URL pattern for, 184
customization supported by, 180
telling to edit existing object, 184
using, 180
models
designing for weblog application, 47–52
ModelChoiceField, 175
ModelForm helper, 233
retrieving content from, 117–119
models.py file
adding category to, 50
creating Django data model in, 33–34
partial for weblog application, 63
moderate_comment function, 134
moderation rules, setting up, 233
moderation system for screening incoming comments, 129
monthly archive template, 107
monthly/daily archives, 106
multiple applications, developing, 226–228
MyModel.object_fetcher.all() method, 94
naming style in Python, 28
newforms package, 186
NodeList, 194
null fields vs. blank fields, 54
object_detail generic view, 86, 97
object_detail view, 72
object_list generic view, 86, 158
object-relational mapper (ORM), 22
objects attribute (django.db.models.Manager class), 94
ORDER BY title ASC, 52
order of URL patterns, 17
order_by method, 161
os.path module (Python), 217
?P construct, 66
packaging tools, Python, 234–235
page field (Django data model), 34
Page not found error, 17
page/paginator variables (Snippet model), 158
parser argument, 193
passwords
PasswordInput widget, 167
validating, 168
patterns() function, 85
permission errors, 6
placeholders, writing templates with, 98
plain attributes vs. methods of feeds, 145
post_save signal, 130
pre_save signal, 131
prepopulated_fields argument, 50
preview.html template, 126
primary keys, 35
projects
vs. applications in Django, 44–45
creating Django, 5
pub_date field
adding default ordering for, 79
providing default value for, 55
showing for blog, 107
py_modules argument, 236
.pyc extension, 8
pydelicious module, 79
pygments
highlight function, 154
pygments.lexers.get_lexer_by_name() method, 152
Python library, 150
Python
admonition about learning, 3
decorator syntax, 75
importance of reading tutorial, 9
interactive interpreter, 3
isolated environments for software management, 209, 211
Markdown module, importing, 154
naming applications, 45
naming style, 28
python setup.py install, 236
python setup.py sdist, 236
regular-expression syntax, 66–67
stopping the server, 7
understanding function arguments, 68
Python modules
giving docstrings to, 238
installing third-party, 79
Python package
Index, 150
standard files to include in, 236–237
Python path
changing, 46
putting code in directory on, 46
q variable, 32
queries
aggregate, 160
execution (Django), 40
QuerySet
class (Django), 40
object, methods in, 233
queryset argument, 70
queryset_or_model argument, 88
Rating model/object, 199
README/README.txt file (Python), 236
recursive-include statements, 237
regular expressions, 14
Reinhardt, Django, 2
related_name argument, 188
relative paths in settings, 217–218
render_to_response function, 31
reproducible builds, 212
RequestContext
vs. Context, 196
importing, 196
populating template variables with, 197
for template rendering, 232
using repetitively, 197
writing shortcut for, 197
request.GET.get(‘q’, ‘’) method, 32
resolve() method, 194
reStructuredText (reST)
learning, 240
syntax, 240
return values vs. functions, 55
rich-text editors (RTEs), 23–26
safe HTTP methods, 190
SalesInquiryForm example, 230
save() method
adding code for Link model to, 80–81
creating a User object with, 169
in Model class, 61
reason not to highlight in, 154
saving new User object with, 169
writing for Snippet model, 154–155
scope creep, 224
search/ directory, 27
search systems
adding to CMS project, 26
writing search view for, 27
search view
adding HttpResponseRedirect to, 39–40
adding keyword_results to, 38–39
adding keywords support in, 38
improving in CMS project, 31–33
rewriting to display empty search form, 32
working without adding to INSTALLED_APPS, 35
security considerations in web applications, 33
SELECT COUNT method (QuerySet), 40
self.id, checking for, 81
self.post_elsewhere+, 81
send method, 130
server error page (Django), 17
settings
changing with environments, 218–219
file, accessing (Django), 80
settings.py file, 8
setup.py script
for continuous packaging, 235
to generate distribution package, 235
writing with distutils, 235–236
setuptools system (Python packages), 235
sidebars
adding explanations in, 102
adding lines to, 128
rewriting in base.html template, 121
signals
class, 130
and Django dispatcher, 129–130
Signup form, 170
signup.html template, 173
Site object, 132
site-packages directories, 46
slug fields
changing definition of, 50
field type, 47
slugs
adding to Link model, 78
and normalization, 50
Snippet model
adding custom manager to definition of, 162
building out basic fields, 153
extra variables for snippets, 157
fields in, 153
filling in author field, 174
finished form for, 176
setting up templates for, 157
snippet_list template, 158
snippets
automatically generating form for adding, 182
bookmarking favorites, 187–188
and languages, 156
logical ordering for, 153
rating, 199
SnippetManager, 199
Snippets application
splitting up, 228
testing, 156
social code-sharing site
building in Django, 149
building initial models, 150
feature checklist, 149
setting up application, 150
software development
general rule for staying on track with, 224
importance of staying focused, 224–225
scope creep in, 224
writing reusable applications in Django, 223
spam, filtering comments for, 129
split_contents method, 116
SQL injection attacks, 33
SQLite, 10
stack trace, 18
standalone and coupled applications, 45
startproject command (django-admin.py), 7
statistical spam analysis, 131–135
stop words in slug fields, 50
str() vs. smart_str() function (Django), 80
strings (Python)
formatting, 52
types of, 34
strptime function (Python), 67
style guide (Python), 62
Subversion, 208
success_url argument, 231
super() method
calling, 175
using, 61
syncdb command, 156
tagging application, 87
tagging.views.tagged_object_list view, 88
tags
adding to Link model, 78
applying to models, 59
entry detail template for, 110
extending template system with custom, 111
provided by Django template system, 19
registering and using new, 120–122
tag() method, 114
Tag model, 87
:tag shortcut, 241
TagField, importing into Snippet model, 153
tagging.views.tagged_object_list view, 88
tags.py file, 92
writing compilation function for, 116–117
writing more flexible with arguments, 115–116
templates
calling object's methods in, 30
chaining inherited, 100
choosing from multiple, 25
creating to generate HTML, 29–30
defining base for blog, 100
for displaying entries, 104
displaying forms with, 182–183
filters, applying, 60
flexible handling of, 230
how names are determined, 71
for Link model generic views, 84
loaders, 19
for other types of content, 110
shortcut, 241
tagging.views.tagged_object_list view, 88
TEMPLATE_CONTEXT_PROCESSORS set, 196
TEMPLATE_DIRS, 19
TemplateSyntaxError, 193
templatetags directory, 192
templating system in Django, 18–22
variables, 196
testing (unit-testing) applications, 27, 219, 221
text-to-HTML converter
Markdown as, 61
save() method to apply, 78
third-party Python modules, installing, 79
TIME_ZONE setting, 11
timedelta class instance, 131
TinyMCE, 23
title elements, adding blocks for, 101
title templates, rendering for feed items, 143–144
tools, VCS, 208
top_user view, rewriting, 162
tracking code with VCS, 205–209
trailing commas, 20
trans tag, 127
truncatewords_html filter, 66
tuples
representing sequences of items with, 20
used by Python for version number, 5
uncommenting code, 13
unique_for_date constraint
supported by Django, 54
used on slug field, 68
unique_for_year constraint, 55
unit-testing applications, 27, 219–221
URL patterns
adding new to Link model, 83–84
changing to specify different templates, 230–231
order of, 17
replacing, 73
setting up for Rating model, 200
URLConf module
cleaning up, 93
making change to weblog, 74
provided by Django application, 44
pulling individual bits into, 93
URLs (Uniform Resource Locators)
configuration, 14
directory, 157
pattern naming, 152
setting up for adding and deleting bookmarks, 190
setting up for LatestEntriesFeed, 143
URLConf file (urls.py), 124
urls/snippets.py, 157
wiring up, 192
urls.py file
copying import statements and URL spatterns into, 72–73
fixing, 17
rewriting to use generic views for entries, 70
setting up in cms directory, 66
users
creating new, 168
not specifying current as default, 56
and passwords, 169
User model, importing into Snippet model, 153
User.DoesNotExist exception, 167
username field, validating, 167–168
username/password in del.icio.us, 80
variable, 196
validation
custom for registration forms, 166
forms, displaying/processing, 172–174
ValidationError exception, 168
variables
provided by Django template system, 19
Variable class, 194
verbose_name options, 50
verify_key() method, 132
version control systems (VCS)
tools and hosting options, 208
version control with Subversion, 209
view function, 28
View on Site button, 16
views
adding new arguments to (Django), 229–230
adding top_languages to Snippet model, 163
creating file for rating snippets, 199–200
disadvantages of changing handwritten, 196
handling project specific, 234
for HttpResponseForbidden class, 183–184
improving of top authors, 161–162
for listing current user bookmarks, 191
login/logout, 178
querying for most-bookmarked snippets, 191
setting up for categories, 84–85
setting up for Snippet model, 158
specifying prefixes for, 73
starting with simple index, 65
using coltrane/category_detail, 85
using generic (Django), 69, 86–87
writing to process form, 177–179
web applications, security and, 33
web framework
web pages, Django vs. hand-written, 19
web servers, launching to see administrative interface, 13
web sites, for downloading
Fabric software, 214
GitHub, 208
pip, 212
virtualenv tool, 210
zc.buildout, 212
web sites, for further information
Apress Source Code/Download area, 163
content types framework documentation, 163
Django authentication system documentation, 178
Django database API documentation, 41, 94
Django installation instructions, 10
Django settings documentation, 10
django.contrib.syndication application, 141
django.newforms documentation, 186
django-tagging application, 59
docutils module, 240
for downloading Django, 4
for downloading Python, 3
Google Project Hosting, 208
IETF RFC 4151 standard, 141
Mercurial, 208
pydelicious, 79
pygments Python library, 150
Python documentation, 52
Python style guide online, 28
setuptools system information, 235
snippets application, 149
TinyMCE RTE, 23
weblog application
building model for entries, 52
creating templates for each view, 71–72
creating urls directory in, 90
Django-powered, 43
generic view tasks, 69
installing django.contrib.comments, 124–125
new field types in, 47
section templates for, 103–104
templates for, 97
viewing index of all entries created in, 66
writing the first views for, 65–69
widget classes (django.newforms module), 165
Windows, time zones in, 11
zc.buildout software, 212
zoneinfo format, 11