Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3313284
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T22:05:50+00:00 2026-05-17T22:05:50+00:00

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

  • 0

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 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-17T22:05:51+00:00Added an answer on May 17, 2026 at 10:05 pm

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am new to both django and python but I am beginning to get
I'm new to both django and python and currently trying to build a form
I'm fairly new to both Django and Python. This is my first time using
I'm kinda new both to OCR recognition and Python. What I'm trying to achieve
i'm not particularly new at python but i just thought there might be a
Apologies for the noobish question, I am completely new to both Python and Django
I'm new to both Django and Python.. I just started using them a few
Hi I'm new to both Python and Beautiful soup. I'm trying to get the
I am new to objective-c. I've taken classes in both python and c++, but
I am brand new to Python, and have been trying to get the Django

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.