简体   繁体   中英

How to get input from template in view in django

I know this question is too primitive but I'm totally new in web application development and I still haven't found an answer for it. When you want to receives some data from the user like the number of products the user wants to buy in a template like this:

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>

how do you read that input in the view? For example, how can I print that input?

My view is like this:

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

You can access the form data off of request.POST. For example:

request.POST['pid']

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM