weixin_33691598 2016-03-30 13:44 采纳率: 0%
浏览 56

在Django中获取POST值

I had an ajax submit (GET query). But it makes change in my database, so smart people told me I should use POST instead with {% csfr_token %}.

GET query:

$(document).on('submit','#follow', function(e){
    var $button = $(this).find('button');
    e.preventDefault();
    $.ajax({
        url:'/event/{{ event.id }}/',
        data: "add={{ event.id }}",
        success:function(){
             $('#follow').hide();
             $('#unfollow').show();
        }
    })
});

views.py

...
event = get_object_or_404(Event, id=event_id)
user = request.user
    if request.GET.get('add'):
        event.users.add(user)
        event.save()
    if request.GET.get('remove'):
        event.users.remove(user)
        event.save()
...

So I added type:"post", and csrfmiddlewaretoken:$('input[name=csrfmiddlewaretoken]) to data but I don't know what to use instead of if request.GET.get('add'): and if request.GET.get('add'):. I tried if request.POST.get('add'): but it doesn't work. So how to use that if with POST values?

UPD.

Ok, what i have now... template:

<form id="unfollow" {% if user not in event.users.all %}style="display:none;"{% endif %}>
    <input type="hidden" value="{{ event.id }}" name="remove">
    <button type="submit" class="btn btn-warning btn-block">{% trans "Unfollow"%}</button>
</form>

...

$(document).on('submit','#unfollow', function(e){
    var $button = $(this).find('button');
    e.preventDefault();
    $.ajax({
        type:"post",
        url:'/event/{{ event.id }}/',
        data: {
            'action': 'remove'
        },
        success:function(){
            $('#unfollow').hide();
            $('#follow').show();
        }
    })
});

views.py:

def show_event(request, event_id):
    event = get_object_or_404(Event, id=event_id)
    user = request.user
    if 'action' in request.POST:
        if 'action' == 'add':
            event.users.add(user)
            event.save()
        elif 'action' == 'remove':
            event.users.remove(user)
            event.save()
    return render(request, 'events/event.html', {'event':event, 'user':user}

No errors, but it doesn't work. success:function() works fine but there are no changes in database. Any advice?

I also tried if request.POST('action') == 'add': but it didn't help

  • 写回答

2条回答 默认 最新

  • weixin_33681778 2016-03-30 13:50
    关注

    Use an object for the data {add: {{ event.id }}} instead of encoding it as a string "add={{ event.id }}"

    Then you should be able to fetch the value from request.POST with either of the following:

    request.POST['add']
    request.POST.get('add', 'default')  # use default if key doesn't exist
    

    You already have event_id in the url, so you don't really need to set add={{event.id}}. It might be better to do something like"

    {'action': 'add'}
    

    Then in your view you can do something like:

    if 'action' in request.POST:
        if request.POST['action'] == 'add':
            # do something
        elif request.POST['action'] == 'remove':
            # do something else
    
    评论

报告相同问题?

悬赏问题

  • ¥15 Centos7 / PETGEM
  • ¥15 csmar数据进行spss描述性统计分析
  • ¥15 各位请问平行检验趋势图这样要怎么调整?说标准差差异太大了
  • ¥15 delphi webbrowser组件网页下拉菜单自动选择问题
  • ¥15 wpf界面一直接收PLC给过来的信号,导致UI界面操作起来会卡顿
  • ¥15 init i2c:2 freq:100000[MAIXPY]: find ov2640[MAIXPY]: find ov sensor是main文件哪里有问题吗
  • ¥15 运动想象脑电信号数据集.vhdr
  • ¥15 三因素重复测量数据R语句编写,不存在交互作用
  • ¥15 微信会员卡等级和折扣规则
  • ¥15 微信公众平台自制会员卡可以通过收款码收款码收款进行自动积分吗