将模板html文件中的请求发送到views.py

时间:2012-12-04 05:34:33

标签: python google-app-engine django-views jinja2

在html文件中我有这个代码,它打印出golfelement的信息,如golfname,changenamefee,url_1net,url_golfagora,url_juchi。我还通过{{golfelement.key().id()}}获得了golfelement ID。有一个按钮1net,如果我点击按钮,它会请求myapp/affiliate

{% for golfelement in displaygolf %}
          <tr>
              <td></td>
              <td>{{ golfelement.golfname }}</td>
              <td>{{ golfelement.changenamefee }}</td>
              <td>
                  <a href="{{golfelement.url_1net}}">1net | </a>
                  <a href="{{golfelement.url_golfagora}}">Golfagora | </a>
                  <a href="{{golfelement.url_juchi}}">Juchi</a>
                  {{golfelement.key().id()}}
                  <form method="LINK" action="{{url_for('myapp/affiliate')}}">
                      <input type="submit" value="1net">
                  </form>
              </td>

          </tr>
          {% endfor %}

当我点击按钮时,我想将{{golfelement.key().id()}}发送到views.py中的函数affiliate(request, idnum)

def affiliate(request, idnum):
    golfe = Golf.get_by_id(idnum)
    return render_to_response('myapp/affiliate.html',{'affiliate': golfe})    

请帮我修改html文件以发送参数{{golfelement.key().id()}}

1 个答案:

答案 0 :(得分:1)

<form method="GET" action="{{ url_for('myapp.affiliate') }}">
    <input type="hidden" name="idnum" value="{{ golfelement.key().id() }} />
    <input type="submit" value="1net" />
</form>

然后你的观点:

def affiliate(request, idnum=None):
    idnum = int(idnum or request.args.get('idnum'))
    golfe = Golf.get_by_id(idnum)
    return render_to_response('myapp/affiliate.html',{'affiliate': golfe})