Skip to main content
 首页 » 编程设计

python之如何在django的 View 中从模板获取输入

2024年08月28日25lyhabc

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

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> 

您如何读取 View 中的输入?例如,如何打印该输入?

我的观点是这样的:

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                      
                        })) 

请您参考如下方法:

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

request.POST['pid']