如何在django中从模板中获取输入

时间:2014-02-12 05:48:49

标签: python django web-applications django-templates django-views

我知道这个问题太原始了,但我在网络应用程序开发方面是全新的,我仍然没有找到答案。如果您希望从用户那里收到一些数据,例如用户想要在这样的模板中购买的产品数量:

form action="/pay/" method="POST">
    {% csrf_token %}

    <label for="pid">Book ID</label>  
    <input type="text" name="pid" value={{pid}} />
    <br/>
    <label for="sid">Seller ID</label>
    <input type="text" name="sid" value={{sid}} />
    <br/>    
    <input type="hidden" name="success_url" value="http://localhost:8000/payment/success" />
    <input type="hidden" name="cancel_url" value="http://localhost:8000/payment/cancel"  />
    <input type="hidden" name="error_url" value="http://localhost:8000/payment/error"  />
    <p>Checksum test: {{checksum}}</p>
    <input type="text" name="checksum" value={{checksum}}/>
    </br>
    <label for="id_amount">Please enter the number of products you would like to buy </label>
    <input type="text" id="id_amount" name="amount" value=""/>
    <input type="submit" value="Accept payment" />
</form>

你如何在视图中阅读该输入?例如,如何打印输入?

我的观点是这样的:

def get_payment_detail(request, pid, sid):    
    checksum = Payment().calc_checksum()   
    return render_to_response('payment/payment.html', RequestContext(request, {'pid':Payment.pid,
                        'sid':Payment.sid,
                        'amount':Payment.amount,
                        'checksum': checksum                     
                        }))

1 个答案:

答案 0 :(得分:2)

您可以从request.POST访问表单数据。例如:

request.POST['pid']