2017-11-06 2 views
0

これは、データベースのすべての提案を表示するHTMLテンプレートです(辞書パラメータのリストとしてviews.pyに渡されます)。私は、データベース内のすべての提案を通過し、その属性を表示するには、forループ神社を使用しています。HTML/Django/Jinja/Python:固定値を返す方法

どのように私は戻って私のPythonコードに{{proposal.idを}}ポスト要求できる「詳細はこちら」ボタンをクリックしたときに?私は他のhtmlテンプレートに対応する値を表示するために必要です。

申し訳ありませんが、これは基本的な質問であれば、私は高校生とDjangoが非常に新たなんです!ありがとう!

{% block body %} 
    {% for proposal in proposals %} 
    <div class="jumbotron"> 

    <h2> Proposal : {{ proposal.title }} </h2> 
    <h4> Status : {{ proposal.status }} </h4> 
    <h4> Out of --- Votes: </h4> 
     <div class="progress"> 
      <div class="progress-bar progress-bar-success" style="width: {{ proposal.votes_for }}%"> 
      <span class="sr-only">35% Complete (success)</span> 
      {{ proposal.votes_for }}% For 
      </div> 
      <div class="progress-bar progress-bar-danger" style="width: {{ proposal.votes_against }}%"> 
      <span class="sr-only">10% Complete (danger)</span> 
      {{ proposal.votes_against }}% Against 
      </div> 
     </div> 

     <p><a class="btn btn-primary btn-lg" href="#" role="button">Learn more</a></p> 
    </div> 

答えて

0

あなたがちょうどあなたは間違いなくクラスベースDetailViewになっているはずですProposal詳細に行きたい場合。

AJAXリクエストで作成するか、フォームで作成することができます。タイプの両方のために、あなたはそれをキャッチするViewを持っている必要があります。

HTMLフォーム

:あなたが持っている必要があり、あなたのテンプレートで

<form id="formId" method="post" action="{% url 'catch-proposal' %}"> 
    {% csrf_token %} 
    <input type="hidden" name="proposal_id" value="{{ proposal.id }}"/> 
    <p><button type="submit" class="btn btn-primary btn-lg">Learn more</a></p> 
    <!-- <input type="submit" class="btn btn-primary btn-lg" value="Learn more"/> --> 
</form> 

をそれはurls.pyからあなたViewに行きます:

url(r'^post/for/proposal/$', catch_proposal, name='catch-proposal'), 
# if your view class-based 
# url(r'^post/for/proposal/$', CatchProposal.as_view(), name='catch-proposal') 

は、その後、あなたのビューであなたがキャッチしますPOSTデータ:

def catch_proposal(request): 
    if request.method == "POST": 
     print(request.POST) # have a look for your post params 
    return reverse_lazy('index') # your response, you can make it on your own 

AJAX:

それをチェック! AJAX and Django

ページには、任意のHTMLフォームページがAJAX経由でPOSTリクエストを行う

せずにAJAXを使用し、ページが必要なCSRFのクッキーがあることを引き起こすcsrf_tokenとHTMLのフォームを持っていません送信されます。

解決策:ページを送信するビューでensure_csrf_cookie()を使用してください。スクリプトで

定義:あなたのLearn Moreボタンの

function sendPost(proposalId) { 
    $.ajax({ 
     url: '{% url 'catch-proposal' %}', // or just /ajax/catch/proposal/ 
     method : "POST", 
     data: { 
      // your data to send key => val 
      'id': proposalId 
     }, 
     dataType: 'json', // it can be xml, json, script, html 
     success: function (result) { 
      // Do something if your request was successful (code=200) 
      // All response data stored in result 
      console.log(result) 
     }, 
     error : function(xhr,errmsg,err) { 
      // Error case 
      console.log(xhr.status + ": " + xhr.responseText); 
     } 
    }); 
} 

を:

<p><button class="btn btn-primary btn-lg" role="button" onclick="sendPost({{ proposal.id }})">Learn more</button></p> 

そして、あなたはあなたのViewでそれをキャッチします:

@ensure_csrf_cookie # Since you sending POST request without form tag 
def catch_proposal(request): 
    response_data = {} # your response 
    if request.method == 'POST': 
     # your post request 
     if 'id' not in request.POST: # check the param from POST 
      # Provide error message 
      response_data['error_message'] = "Can't find ID in POST params..." 
     else: 
      # Do whatever 
      proposal_id = int(request.POST.get('id')) 
      try: 
       proposal = Proposal.objects.get(id=transport_id) 
       response_data['success'] = True 
      except Proposal.DoesNotExist: 
       response_data['success'] = False 
     return JsonResponse(response_data) 
    else: 
     response_data = { 
      'error_message': 'Something is going very strange and wrong...' 
     } 
     return JsonResponse(response_data) 

作成を追加するViewurls.py

from .views import catch_proposal # or yourapp.views 
.... 
url(r'^ajax/catch/proposal/$', catch_proposal, name='catch_proposal'), 
.... 
関連する問題