2017-09-08 2 views
-1

나는 데카르트 제품을 계산하는 데 사용하는 몇 가지 목록이 :플라스크에서 Jinja 템플릿에 파이썬 목록을 어떻게 인쇄합니까?

python.py :

@app.route('/output', methods = ['GET','POST']) 
def output(): 
    a = ['one','two'] 
    b = ['three','four'] 
    c = ['five'] 
    d = ['six','seven','eight'] 
    e = ['nine','ten','eleven'] 

    cpl = list(itertools.product(a,b,c,d,e)) 
    return render_template('output.html',cpl = cpl) 

가 output.html 그러나

{% for cp in cpl %} 
    <p>{{ cp }} </p> 
{% endfor %} 

, 나는 빈 화면이 반환하고 .

Jupyter에서 동일한 파이썬 코드를 실행할 때 목록이 반환됩니다.

어디에서 문제가 생길 수 있습니까?

+1

것은 그냥있을 수 있습니다 현재 설정에 문제가 있습니다. 로컬에서 코드를 실행했는데 예상되는 결과를 얻었습니다. –

답변

1

cpl은 튜플 목록을 반환하지만 단일 값은 아닙니다. 아마 그것은 Jinja를 혼란스럽게 할 것입니다. 중첩 된 for 루프를 만들거나 템플릿을 렌더링하기 전에 해당 튜플을 문자열로 캐스팅 할 수 있습니다.

예를 들어,이다 일

strings = [str(c) for c in cpl] 
return render_template("output.html", cpl=strings) 
+0

의심 할 것없이. 설치가 잘 작동하여 튜플 문자열 표현을 생성합니다. – alecxe

+0

변환을 시도했지만 도움이되지 않았습니다. 파이썬이나 진자로 중첩 된 루프는 어디에서 만들까요? –

+0

설치가 @alecxe에서 제대로 작동하고 변환이 도움이되지 않으면 다른 모든 항목을 확인해 보겠습니다. – minterm

1

솔루션 추가하려고 :

python.py을

@app.route('/output', methods = ['GET','POST']) 
def output(): 
    a = ['one','two'] 
    b = ['three','four'] 
    c = ['five'] 
    d = ['six','seven','eight'] 
    e = ['nine','ten','eleven'] 
    newArray = [] 
    newArray = [a, b, c, d, e] 
    cpl = list(itertools.product(*[i for i in newArray if i != []])) 
    return render_template('output.html',cpl = cpl) 

output.html :

{% for cp in cpl %} 
<p> {{ cp }} </p> 
{% endfor %} 
+0

다행 이군요. 이것을 허용 된 대답으로 만드십시오. – minterm

관련 문제