2016-09-20 1 views
1

テンプレートでそれを行うことが実現可能でないことが判明しているため、ビュー内にテーブルを作成してそのビューのテンプレートにプッシュしようとしました。私はそれに正しい情報を持っている文字列を生成することができますが、そのテンプレート変数がブラウザで評価されると、テーブルが正しく生成されないように見える余分な引用符とスペースがあります。ここでDjangoが文字列をテンプレートにプッシュするときに空白と引用符を追加する

は、ビューからコードされています。しかし、これは、DOMに表示何その私のすべて

for unique_activity in uniquelist: 
     currentrow = '<tr data-activityID="' + str(unique_activity[0])+ '">' + '<td>' + str(unique_activity[1]) + '</td>' 
    for visit in visits_list: 
     for activity in visit.activity_set.all(): 
      if activity.id == unique_activity[0]: 
       currentrow = currentrow + '<td>' + 'Reps:'+ str(activity.Repetitions) + '</td>' 
      else: 
       noact = True 
     if noact == True: 
      currentrow = currentrow + '<td></td>' 
    currentrow = currentrow + '</tr>\n' 
    tablerows.append(currentrow) 

    table = '<table>' 
    for row in tablerows: 
     table = table + row 
    table = table + '</table>' 
    table = str(table) 

出力は、それがために必要なもの..です例<table><tr data-activityID="1"><td>Initial Evaluation</td><td>Reps:None</td><td></td><td></td></tr> <tr data-activityID="3"><td>Cold Pack</td><td></td><td>Reps:None</td><td></td></tr> <tr data-activityID="6"><td>Recumbent Exercise Bike</td><td>Reps:12</td><td></td><td></td></tr> <tr data-activityID="7"><td>Leg Press</td><td></td><td></td></tr> <tr data-activityID="8"><td>Shoulder Ladder</td><td></td><td></td></tr> </table>

ですテンプレートへの出力は、単に{{テーブル}}であり、これは、DOM情報にチュにおける

enter image description here

、結果を出力を得るものですstはテーブルではなく文字列を表示します。ここで

テンプレートスニペットは、私が何であるか見当がつかない

<body> 
{% if activity_list %} 
    <table> 
    {% for activity in activity_list %} 
     <tr> 
      <td>{{ activity.ActivityType.Name }}</td> 
     </tr> 
    {% endfor %} 
    </table> 
{% endif %} 
{{ table }} 
</body> 

です...

+0

は、テンプレートに '' {{テーブル}}のあなたの含有を示すスニペットを提供してもらえますか? – tredzko

+0

コメントしていただきありがとうございます。しかし、それは死んでいるバニラですが、違いがあります。上のテーブル。 – Gradatc

答えて

1

あなたがベーステンプレート(または他の場所)内にautoescapeを有することができるようにこれが聞こえます。変数をHTMLとして組み込み、テキストに安全にエスケープしない場合は、autoescapeをオフにするか、safe template filterとマークする必要があります。試して説明するテンプレートの例を次に示します。あなたが提供するスニペットを使用して

<p>The following table should be rendered as escaped safe text.</p> 
{{ table }} 

<p>But with the safe filter, it will be rendered as html!</p> 
{{ table|safe }} 

<p>And maybe, I want to do this to a few things or have a block of safe html, so I'll just turn the autoescape off for a bit!</p> 
{% autoescape off %} 
{{ table }} 
{{ some_other_safe_html }} 
{% endautoescape %} 

は、ここでは安全な逃避とのコードです:

<body> 
{% if activity_list %} 
    <table> 
    {% for activity in activity_list %} 
     <tr> 
      <td>{{ activity.ActivityType.Name }}</td> 
     </tr> 
    {% endfor %} 
    </table> 
{% endif %} 
{{ table|safe }} 
</body> 
+1

ありがとうございます。それがオプションであることは決して知りませんでした。迅速で正確な対応をありがとう。 – Gradatc

+0

@Gradatc問題ありません!お役に立てて嬉しいです。 – tredzko

関連する問題