Archive for the ‘tips & tricks’ Category.

Another simple but useful Django trick

Say you need the full URL of the current page you're displaying that you need to use on the template of that page, for example, to add links to Facebook, Delicious, etc., and you don't want to have additional JavaScript that you didn't write on your pages. (Why yes, I'm a control freak, shush. :P) Also say that you don't want to use the sites framework to hack it together. The permalink decorator won't work, either, because that just gives you the relative link.

Thankfully, there's a new request object that's been added to 1.0 that will do it.

To use it, include the bolded bit in your file, as part of each view where you want to have the variable available:

return render_to_response('dir/template.html', {'object': obj, 'link': request.build_absolute_uri()}, RequestContext(request))

Then you can use the link variable on the template where you need the URL:

<a href="{{ link|urlencode }}&t={{ title|urlencode }}">

I have yet to figure out how to get it to work with generic views, so feel free to holler if you know how. *vbg* 3.x tricks

1. Styles

You are using the styles dialog (F11 or Format -> Styles and Formatting) to keep all your paragraph/character/page styles straight, right?

2. Headers and footers with text specific to each document section

Say you're doing the layout for a fiction anthology and you need to have the story name and page number on the recto and the author name and page number on the verso for each story. Each story has a different title and is written by a different author.

No, you don't have to set up different page styles for each story/author combination. Custom variables to the rescue!

Insert -> Fields -> Other -> Variables -> Set Variable (or Ctrl+F2)
Insert -> Fields -> Other -> Variables -> Show Variable (or Ctrl+F2)

Use set variable to create your own variables that will be linked to the header (or footer) display. For example, if you create a text field variable named "author" (on the novella's opening page, where you want the author's name to display) and fill it with the name of the author who wrote the first novella in the anthology, you can then use show variable in the verso header and it will correctly display the author's name on the pages for the first story. Ditto for the recto header: create a variable named "title" at the beginning of the story using set variable, then use show variable in the recto header and it will display the correct title.

You'll need to re-set the title and author variables as you get to the title page for each new story, but you won't need to redo header references–they'll update themselves. :)

3. Table of contents

The table of contents setup in OO is a royal pain in the ass. The user interface is not intuitive at all, and there are no instructions on how to do a TOC in the help file buy kamagra oral jelly online uk. (Online yes, but not in the basic user documentation.) Also, every time something changed and the TOC had to be updated to reflect changed page numbers, it erased the custom layout I'd created. Wow, that's helpful. NOT.

So I used references instead.

Insert -> Fields -> Other -> Cross-references -> Set reference (or Ctrl+F2)
Insert -> Fields -> Other -> Cross-references -> Insert reference (or Ctrl+F2)

Highlight the title and author variables you set earlier, and choose set reference. You'll end up with multiple similarly-named references (I did title 1, title 2, author 1, author 2, etc.), but if you only have a few items, it shouldn't get too clunky. Use insert reference on your TOC page (make sure "insert reference to" says "reference" to display the text, or page number to display the, you got it, page number). I also used these references to list the titles and authors on the copyright page for their copyright statements. Simple, yes?