tags:

views:

43

answers:

2

I am new to both Python (and django) - but not to programming.

I am having no end of problems with identation in my view. I am trying to generate my html dynamically, so that means a lot of string manipulation. Obviously - I cant have my entire HTML page in one line - so what is required in order to be able to dynamically build an html string, i.e. mixing strings and other variables?

For example, using PHP, the following trivial example demonstrates generating an HTML doc containing a table

<?php 
$output = '<html><head><title>Getting worked up over Python indentations</title></head><body>';

output .= '<table><tbody>'
for($i=0; $i< 10; $i++){
   output .= '<tr class="'.(($i%2) ? 'even' : 'odd').'"><td>Row: '.$i;
}
$output .= '</tbody></table></body></html>'

echo $output;

I am trying to do something similar in Python (in my views.py), and I get errors like:

EOL while scanning string literal (views.py, line 21)

When I put everything in a single line, it gets rid of the error.

Could someone show how the little php script above will be written in python?, so I can use that as a template to fix my view.

[Edit]

My python code looks something like this:

def just_frigging_doit(request):
    html = '<html>
                <head><title>What the funk<title></head>
                <body>'

    # try to start builing dynamic HTML from this point onward...
    # but server barfs even further up, on the html var declaration line.

[Edit2]

I have added triple quotes like suggested by Ned and S.Lott, and that works fine if I want to print out static text. If I want to create dynamic html (for example a row number), I get an exception - cannot concatenate 'str' and 'int' objects.

+1  A: 
  1. In Python, a string can span lines if you use triple-quoting:

    """
    This is a 
    multiline
    string
    """
    
  2. You probably want to use Django templates to create your HTML. Read a Django tutorial to see how it's done.

  3. Python is strongly typed, meaning it won't automatically convert types for you to make your expressions work out, the way PHP will. So you can't concatenate strings and numbers like this: "hello" + num.

Ned Batchelder
+1 You probably want to use Django templates.
S.Lott
+4  A: 

I am trying to generate my html dynamically, so that means a lot of string manipulation.

Don't do this.

  1. Use Django's templates. They work really, really well. If you can't figure out how to apply them, do this. Ask a question showing what you want to do. Don't ask how to make dynamic HTML. Ask about how to create whatever page feature you're trying to create. 80% of the time, a simple {%if%} or {%for%} does everything you need. The rest of the time you need to know how filters and the built-in tags work.

  2. Use string.Template if you must fall back to "dynamic" HTML. http://docs.python.org/library/string.html#template-strings Once you try this, you'll find Django's is better.

Do not do string manipulation to create HTML.


cannot concatenate 'str' and 'int' objects.

Correct. You cannot.

You have three choices.

  1. Convert the int to a string. Use the str() function. This doesn't scale well. You have lots of ad-hoc conversions and stuff. Unpleasant.

  2. Use the format() method of a string to insert values into the string. This is slightly better than complex string manipulation. After doing this for a while, you figure out why templates are a good idea.

  3. Use a template. You can try string.Template. After a while, you figure out why Django's are a good idea.


my_template.html

<html><head><title>Getting worked up over Python indentations</title></head><body>
<table><tbody>
{%for object in objects%}
    <tr class="{%cycle 'even' 'odd'%}"><td>Row: {{object}}</td></tr>
{%endfor%}
</tbody></table></body></html>

views.py

def myview( request ):
    render_to_response( 'my_template.html',
         { 'objects':range(10) }
    )

I think that's all you'd need for a mockup.

S.Lott
Hmmm, I suppose there is no "easy mockup" with django - I suppose I have been spoilt using php for web development. I just wanted to put something together to look at statistical data in some tables. I really was not looking forward to possibly spending a whole sunday afternoon on reading up on the template framework just to put together a quick page to look at some data. Still, messing around with raw strings is even more frigging annoying. I guess I'll hyust do it in PHP for now and when I have the time, I will read up on the templating stuff. Raw string manip is DEFO not the way to do it.
skyeagle
I thought I could create a quick mockup using the example at: http://www.djangobook.com/en/2.0/chapter03/. I guess I was wrong
skyeagle
Page mockup in Django is trivial. Use a template to mock it up. Just write the HTML. Once you have it looking right, use a `{%for%}` loop to populate data and you're done. The Django template looks a lot like the PHP code. A real lot. Your view function does the DB fetches and provides the queryset to the template. The template does presentation of the query set.
S.Lott