2010-08-04 185 views
1

如何在不同位置訪問多個變量? %s如何看起來像是在殺手點,以及如何在最後正確插入變量。字符串格式化django

謝謝!

下面是代碼:

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) 
+0

您沒有使用模板的任何特定原因? – second 2010-08-04 09:37:51

+0

是的,因爲我想再次瞭解基礎知識並遵循教程。你知道答案嗎? – MacPython 2010-08-04 09:40:42

回答

3

不知道你是問。你只是想在你的字符串中插入多個值嗎?

"value 1 is %s, value 2 is %s." % (value1, value2) 
+0

就是這樣。謝謝! – MacPython 2010-08-04 09:54:51