django表单不会将数据传播到服务器端

时间:2015-05-21 10:17:17

标签: javascript python html django

我的Django项目中有一个模板,它使用HTML和javascript的组合。该模板由两个日期和一个文本字段组成。我试图将其发送到服务器端,但收效甚微。有人可以提供建议。

模板:

<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>jQuery UI Datepicker - Default functionality</title>
<link rel="stylesheet" href="//code.jquery.com/ui/1.11.4/themes/smoothness/jquery-ui.css">
<script src="//code.jquery.com/jquery-1.10.2.js"></script>
<script src="//code.jquery.com/ui/1.11.4/jquery-ui.js"></script>
<link rel="stylesheet" href="/resources/demos/style.css">
<script type="text/javascript">
   $(function() {
   $( "#datepicker" ).datepicker();
   });
   $(function() {
   $( "#datepicker2" ).datepicker();
    });

   $(document).ready(
   function()
   {
        $('#myform').ajaxForm({
                    beforeSubmit: function(arr, $form, options)
                    {
                        var field=$('#datepicker').val();
                        if (field!=null)
                        {
                            $('#my_form').ajaxStart( function()
                            { alert('loading'); });
                            return true;
                        }
                        else
                        {
                            alert
                            return false;
                        }
                    },
                    success: function(response)
                    {                            
                    }
                });
  }
);
</script>
</head>
<body>
    <p>Date: <input type="text" id="datepicker"></p>
    <p>Date: <input type="text" id="datepicker2"></p>
    <p>Date: <input type="text" id="my_form"></p>
    <form action="" method="post" id="myform">{% csrf_token %}
    {{ myform.as_p }}
    <input type="submit" name="submit" value="SUBMIT" id="submit" />
    </form>

</body>
</html>

视图:

def index(request):
    template = 'my_site/index.html'
    if request.is_ajax():

    if request.method == 'POST':
        post = request.POST 
        my_form = GraphForm(request.POST)
        if my_form.is_valid():
            new = my_form.save(commit=True)
            response = { 'status':True, }
        else:
            response = {'status': str(my_form) }
        json = simplejson.dumps(response, ensure_ascii=False)
        return HttpResponse(json, content_type="application/json")

    context = {'my_form':my_form}
    return render_to_response(template, context, context_instance=RequestContext(request))
else:
    form = GraphForm()
    return render(request,"my_site/index.html", { 'form' : form } )

形式:

class GraphForm(forms.Form):
    my_form = forms.CharField(label = 'my_form',max_length=100)
    start_date = forms.DateField(required=True)
    end_date = forms.DateField(required=True)

3 个答案:

答案 0 :(得分:0)

您的输入字段:

<p>Date: <input type="text" id="datepicker"></p>
<p>Date: <input type="text" id="datepicker2"></p>
<p>Date: <input type="text" id="my_form"></p>

不在形式,因此在提交表单时,这些不包括在内。此外,如果您使用的是django表单,则无需再次定义字段。您可以在django字段上调用datepicker。 id将是id_start_date和id_end_date。你可以在那上面应用datepicker $(“#id_start_date”)。datepicker();和$(“#id_date_end”)。datepicker();

答案 1 :(得分:0)

您的表单应如下所示

<body>
    <form action="" method="post" id="myform">{% csrf_token %}
        {{ myform.as_p }}
        <p>Date: <input type="text" id="datepicker"></p>
        <p>Date: <input type="text" id="datepicker2"></p>
        <p>Date: <input type="text" id="my_form"></p>

        <input type="submit" name="submit" value="SUBMIT" id="submit" />
    </form>

</body>

答案 2 :(得分:0)

未设置您的操作网址。 POST数据将去哪里?

<body>
    <form action="." method="post" id="myform">
        {% csrf_token %}
        {{ myform.as_p }}
        <p>Date: <input type="text" id="datepicker"></p>
        <p>Date: <input type="text" id="datepicker2"></p>
        <p>Date: <input type="text" id="my_form"></p>

        <input type="submit" name="submit" value="SUBMIT" id="submit" />
    </form>
</body>