django GET POST


django需要讀取客戶端get和post請求的值。讀取處理方法和異常記錄於此。

參考鏈接:

  http://stackoverflow.com/questions/12518517/request-post-getsth-vs-request-poststh-difference

  • request.GET['sth']如果'sth'不存在,將會觸發KeyError異常。

  • request.GET.get('sth')如果'sth'不存在,將會得到None。

POST使用的時候也是一樣。

example

def index(request):
    count = 0
    if request.method == 'GET':
        try:
            t = time.objects.get(id=1)
            count = request.GET['onoff']  
            t.count = int(count)
            t.save()
        except KeyError:
            t = time.objects.get(id=1)
            count = str(t.count)
            print 'hello' 
        return render(request, 'index.html', { 'onoff': count})
    return render(request, 'index.html')

Tony Liu

2016-11-5, Shenzhen


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM