1

If I want to insert a variable %s into this

<p><img src= %s alt="tetris"/></p>

I have the problem that if I use "%s", it wont recognize it as the placholder it is. But if just use %s it wont link to my image.

Is there a way around this? I tried to insert the url that is inserted there like this in the database ''/url/''. But that wont do the trick either. Any suggestions?

@thomas:

from django.http import HttpResponse
from django.contrib.auth.models import User
from favorites.models import *

def main_page_favorites(request):
    title = Favorite.objects.get(id=1).title.upper()
    email = User.objects.get(username='Me').email
    image = Hyperlink.objects.get(id=3).url
    output = '''
        <html>
            <head>
                <title>
                    Connecting to the model
                </title>
            </head>
            <body>
                <h1>
                Connecting to the model
                </h1>
                We will use this model to connect to the model!

                <p>Here is the title of the first favorite: %s</p>
                                <p>Here is your email: %s </p>
                                <p>Here is the url: %s </p>
                                <p>Here is the url embedded in an image: <p><img src= %s alt="tetris"/></p>

            </body>
        </html>''' % ( 
           title, email, image, image
        )
    return HttpResponse(output)
3
  • Can you give example code? '<p><img src="%s" alt="tetris"/></p>' % "foo.jpg" works good. Commented Aug 4, 2010 at 10:07
  • Sorry. Forgot to say I retrieve that variable from a database. so %s is replaced by image. And image is definded like this: image = Hyperlink.objects.get(id=3).url Commented Aug 4, 2010 at 10:38
  • 1
    Still should not be any problem. Can you paste exact code from your script? Commented Aug 4, 2010 at 10:44

1 Answer 1

1

Maybe you should look into the templates that are included with django. They make for much more easily maintained code.

from django.shortcuts import render_to_response
from django.template import RequestContext

def view(request):
    user = request.user
    return render_to_response('template.html', {
        'user':user,
    }, context_instance=RequestContext(request)

And then in your template.html file in your templates directory:

<html>
  <!-- snip -->
  <body>
    Here is your email: {{ user.email }}
  </body>
</html>

Bonus: your text editor will probably highlight your HTML syntax.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks! I know the template system. But since I am learning how to write to a database, it is better to start with the basics.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.