django post 与get请求理解

同页面form表单中get请求与post请求的理解

views.py

def game_update(request):
    try:
        gameid=request.GET.get('id','')
        game=Game.objects.get(id=gameid)
    except Exception as e:
        return '不存在id'
        print('异常……%e'%(e))
    if request.method=='GET':
        
        return  render(request,'games/update.html',locals())


    elif request.method=='POST':
        
       #注意gname获取不到,否则会报错 str没有get属性
        config_type=request.POST['config_type']
        game.config_type=config_type
        game.save()
        return  redirect('/game/index')  

update.html

body>
    <h3>更新游戏</h3>
    <div>
        <form action="/game/update/?id={{game.id}}" method="POST">
        {% csrf_token %}
        游戏名:<input type="text" name="gname" value={{game.gname}} disabled>
        游戏配置类型:<input type="text" name="config_type" value="{{game.config_type}}">
        <input type="submit" value='更新'>
        
        </form>
    </div>
</body>
  

posted @ 2021-08-24 18:00  yescarf  阅读(104)  评论(0编辑  收藏  举报