help_text line continuation

56 views
Skip to first unread message

Mike Dewhirst

unread,
Nov 14, 2011, 1:27:33 AM11/14/11
to django...@googlegroups.com
I have been using the Python line continuation symbol +\ in my models
help_text when my text goes beyond column 80 in my editor.

I just accidentally omitted it for a continued line and discovered it
doesn't seem to be needed!!!

Is that a feature of Django's admin app or a trap for the unwary?

Thanks

Mike

Karen Tracey

unread,
Nov 14, 2011, 7:26:17 AM11/14/11
to django...@googlegroups.com

Tim Chase

unread,
Nov 14, 2011, 7:35:54 AM11/14/11
to django...@googlegroups.com, Mike Dewhirst

It's a python feature.

http://docs.python.org/reference/lexical_analysis.html#string-literal-concatenation

Outside Django, you can see it in the following:

>>> x = ("stuff"
... "morestuff"
... "yetmorestuff"
... )
>>> print x
stuffmorestuffyetmorestuff
>>> {"hello"
... "world": "value"
... "rest of value"
... }
{'helloworld': 'valuerest of value'}
>>> ["list"
... "with"
... "one"
... "element"
... ]
['listwithoneelement']


Strings that abut inside a continuation context (parens, brackets
or braces) are combined together at parsing time. The only thing
to watch out for is the obvious lack of characters inserted, so
you'll want to make sure any spaces/newlines you want get put in
the strings:

>>> s = ("space after this " # you can even include comments
... "so that this reads right")
>>> print s
space after this so that it reads right

(as opposed to "space after thisso that it reads right")

So what you're seeing is the continuation context of parens used
in the call to a *Field object:

class MyModel(Model):
foo = CharField(
...,
help_text="Some help here "
"that continues on the next line"
)

Perfectly reliable and quite helpful at times.

-tim


Markus Gattol

unread,
Nov 14, 2011, 11:29:26 AM11/14/11
to django...@googlegroups.com
have a look at http://www.markus-gattol.name/ws/python.html#multi-line_strings_expressions ...it's the same for statements, just put stuff inside ( and )
Reply all
Reply to author
Forward
0 new messages