tags:

views:

56

answers:

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)
A: 

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.

Matthew Schinckel
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.
MacPython