字符串格式化django

Mac*_*hon 2 django

如何访问不同位置的多个变量?%s 在插入点处的外观如何以及最后如何正确插入变量。

谢谢!

这是代码:

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>

            </body>
        </html>''' % ( title, email, image
        )
    return HttpResponse(output)
Run Code Online (Sandbox Code Playgroud)

sec*_*ond 5

不知道你在问什么。你只是想在你的字符串中插入多个值吗?

"value 1 is %s, value 2 is %s." % (value1, value2)
Run Code Online (Sandbox Code Playgroud)