2010-08-04 8 views
1

나는 내가 "%s"를 사용하는 경우, 그것은 습관이있는 placholder로 인식하는 문제가이장고 문자열 형식

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

에 변수 % s을 (를) 삽입합니다. 그러나 단지 %s을 사용하면 내 이미지에 연결되지 않습니다.

이 방법이 있습니까? 데이터베이스에 삽입 된 URL을 삽입하려고했습니다 (''/url/''). 그러나 그 트릭도하지 않을 것입니다. 제안 사항이 있으십니까?

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

예제 코드를 제공해 줄 수 있습니까? ' '

tetris

'% "foo.jpg"는 효과가 있습니다. –

+0

죄송합니다. 데이터베이스에서 해당 변수를 검색한다고 말한 것을 잊었습니다. 그래서 % s이 (가) image로 대체되었습니다. 이미지가 다음과 같이 정의됩니다. image = Hyperlink.objects.get (id = 3) .url – MacPython

+1

여전히 문제가 아닙니다. 스크립트의 정확한 코드를 붙여 넣을 수 있습니까? –

답변

1

은 어쩌면 당신은 장고에 포함 된 템플릿으로 보일 것입니다. 그들은 훨씬 더 쉽게 유지 관리되는 코드를 만듭니다.

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) 

다음 templates 디렉토리에 template.html 파일에

:

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

보너스 : 텍스트 편집기는 아마 당신의 HTML 구문을 강조 할 것이다.

+0

감사! 나는 템플릿 시스템을 안다. 그러나 데이터베이스에 쓰는 방법을 배우기 때문에 기초부터 시작하는 것이 좋습니다. – MacPython