Django表单呈现不传递原始HTTP请求对象

时间:2015-05-08 23:15:47

标签: django django-forms

Django / Python / stackexchange的新手......

我有一个Jquery Datatable,我很难将表格的一个单元格(称为email_id)的值作为处理新表单页面时使用的HTTP parm传递。

视图最初是根据调试输出正确接收请求:

WSGIRequest: GET '/main_app/makeTask/?csrfmiddlewaretoken=gDeTwaNfGNLO7cdMk1
B9gsdpcGYpKAyL&email_id=14d2a002852e1738' 

它在第一次调用时成功提取了带有request.GET.get()的email_id(然后继续构造表单并呈现它),但在呈现表单时它会被删除。即,在GET上正确提取email_msg_id,但在后续POST中没有提取。

这是提取代码和render语句。问题是当我渲染表单时'email_id'没有传播,所以当它处理表单输入时,它不能被视图使用。

email_msg_id = request.GET.get('email_id', "GET - no email_id")
...
return render(request, 'main_app/makeTask.html', {'form': form, 'email_id': email_msg_id})

调试消息:

<WSGIRequest: GET '/main_app/makeTask.html'>

以下是urls.py的相关部分:

url(r'^makeTask', views.make_task, name='makeTask'),

我的Jquery调用(它似乎正在正确地传递/传递email_ID:

$('#make_task').click( function () {
        alert( table.cell('.selected',4).data()+' Converted to Task');
        // alert(table.cell('.selected',0).data()+' Make Task selected:');
        $.ajax({
            type: "GET",
            url: "/main_app/makeTask/",
            data: { 
                'csrfmiddlewaretoken': '{{ csrf_token }}',
                'email_id' : table.cell('.selected',4).data(),
            },
            success: makeTaskSuccess,
            dataType: 'html'
        }); 
        function makeTaskSuccess(data)
        {
            alert('Convert email to task ');
        }
    } );    

以下是视图(删除了不相关的内容):

def make_task(request):
    if request.method == "GET":
        email_msg_id = request.GET.get('email_id', "GET - no email_id")     # from the post dictionary of the request object passed in

    else:
        email_msg_id = request.POST.get('email_id', "POST - no email_id")

    print "EMAIL_MSG_ID: ", email_msg_id, "METHOD: ", request.method

    if request.method == 'POST':
        form = MakeTaskForm(request.POST or None)       # Create a form instance and populate it with the data

        if form.is_valid():
            # process and save to db
            #...

            return HttpResponseRedirect(reverse('main_app.views.index'))

        else:
            print form.errors
        return
    else:
        form = MakeTaskForm()

        return render(request, 'main_app/makeTask.html', {'form': form, 'email_id': email_msg_id})
        #return render(request, 'main_app/makeTask.html', context_dict)

更新:添加了相关的模板代码:

<form id="make_task_form" method="post" action="/main_app/makeTask/", method="Post">
    {% csrf_token %}                                                                                
    <div class="row">
        <div class="col-lg-8 col-xs-12">
            <div class="form-group">
                <label>Due Date: &nbsp</label>
                <label class="radio-inline">
                    <input type="radio" name="due_selection" id="optionsRadiosInline1" value="TODAY" {{ form.due_selection }} Today
                </label>
           </div>
        </div>
        <div class="col-lg-4 col-xs-12">                                
            <p>Specific Date: <input type="text" id="datepicker" {{ form.due_date }}</p>
        </div>
    </div>
    <div class="row">
        <div class="col-lg-4 col-xs-12">
            <button type="submit" class="btn btn-info btn-med" id="btn-make-task">
                Make Task
            </button>
            <a href="#"><i class="fa fa-trash-o fa-2x pull-right"></i></a>
        </div>
    </div>
</form>

1 个答案:

答案 0 :(得分:0)

根据您发布的代码,我假设您在表单中使用GET表示AJAX,并使用POST

您发布的表单代码有误,请在您的模板中访问您的表单字段(也可以删除method中重复的<form>属性):

<form id="make_task_form" method="post" action="/main_app/makeTask/">
    {% csrf_token %}                                                                                
    <div class="row">
        <div class="col-lg-8 col-xs-12">
            {{ form.due_selection }}
        </div>
        <div class="col-lg-4 col-xs-12">                                
            {{ form.due_date }}
        </div>
    </div>
    <div class="row">
        <div class="col-lg-4 col-xs-12">
            <button type="submit" class="btn btn-info btn-med" id="btn-make-task">
                Make Task
            </button>
            <a href="#"><i class="fa fa-trash-o fa-2x pull-right"></i></a>
        </div>
    </div>
</form>

要对字段<input type="radio">使用due_selection,您应在ModelForm类中指定RadioSelect小部件:

CHOICES = (('1', 'First',), ('2', 'Second',))
due_selection = forms.ChoiceField(widget=forms.RadioSelect, choices=CHOICES)