无法从Django表单获取POST数据

时间:2015-05-22 00:52:47

标签: django forms post

我有一个仅涉及两个textarea的表单,我试图发布它们。但是,我在尝试从我的视图中获取POST数据时遇到了困难。这是我目前在模板中的内容:

{% block title %}
<form method="post">
    <strong>Title:</strong>
    <div>
        <textarea contenteditable id="id-title" name="title"></textarea>
    </div>
{% endblock title %}

{% block description %}
    <strong>Description:</strong>
    <div>
        <textarea contenteditable id="id-description" name="description"></textarea>
    </div>
</form>
{% endblock description %}

<button class="btn-primary" id="ok" url="{% url 'publish' id=id %}" type="submit">Publish</button>

视图:

@ajax
@csrf_exempt
@admin_required
def publish(request, id):
    title = request.POST.get('title')
    desc = request.POST.get('description')
    print title
    print desc
    ...

titledesc原来是None。我的publish函数也是一个ajax函数,在按下按钮时会执行其他操作。

1 个答案:

答案 0 :(得分:0)

尝试使用action更改您的表单,并在表单中移动您的提交,例如:

<form method="post" action="{% url 'publish' id=id %}">                                             
{% block title %}                                                                                   
<strong>Title:</strong>                                                                         
<div>                                                                                           
    <textarea contenteditable id="id-title" name="title"></textarea>                            
</div>                                                                                          
{% endblock title %}                                                                                

{% block description %}                                                                             
<strong>Description:</strong>                                                                   
<div>                                                                                           
    <textarea contenteditable id="id-description" name="description"></textarea>                
</div>                                                                                          
{% endblock description %}                                                                          
<input id="ok" type="submit">                                                                   
</form>