如何在不使用Django,阿贾克斯,jQuery的清爽页面提交表单? [英] How to submit form without refreshing page using Django, Ajax, jQuery?

查看:147
本文介绍了如何在不使用Django,阿贾克斯,jQuery的清爽页面提交表单?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我与Django的新手工作。我需要简单的例子。 如何在不使用Django,阿贾克斯,清爽的页面提交表单(POST)jQuery的?

这是我的表,视图和模板:

views.py

 从django.shortcuts导入*
从django.template进口的RequestContext
从linki.forms导入*

高清广告(要求):
    如果request.method ==POST:
        表= AdvertForm(request.POST)

        如果(form.is_valid()):
            打印(request.POST ['标题'])
            消息= request.POST ['标题']

        其他:
            消息=不对!


        返回render_to_response('触点/ advert.html',
                {消息:消息},
            context_instance = RequestContext的(要求))

    其他:
        返回render_to_response('触点/ advert.html',
                {'形式':AdvertForm()},
            context_instance = RequestContext的(要求))
 

forms.py(使用的ModelForm的形式)

 从Django的进口形式
从django.forms进口的ModelForm
从linki.models导入广告


类AdvertForm(的ModelForm):
    类元:
        模型=广告
 

模板(HTML形式code)

 < HTML>
< HEAD>

< /头>
    <身体GT;
    < H1>发表建议此处< / H1>
        {如果%消息%}
            {{ 信息 }}
        {%ENDIF%}
        < D​​IV>
            <形式的行动=的方法=邮报> {%csrf_token%}
                {{form.as_p}}
                <输入类型=提交值=提交反馈/>
            < /形式GT;
        < / DIV>
    < /身体GT;
< / HTML>
 

解决方案

如果你打算使用一个Ajax提交与jQuery你不应该从你的观点返回HTML ..我建议你,而不是这样做:

HTML:

 < HTML>
< HEAD>
< /头>
<身体GT;
    < H1>发表建议此处< / H1>
        < D​​IV CLASS =消息>< / DIV>
        < D​​IV>
            <形式的行动=的方法=邮报> {%csrf_token%}
                {{form.as_p}}
                <输入类型=提交值=提交反馈/>
            < /形式GT;
        < / DIV>
< /身体GT;
< / HTML>
 

在JS

  $('#形式)。递交(函数(五){
    .post的$('/ URL /',$(本).serialize(),功能(数据){...
       $('消息'),HTML(data.message)。
       //你当然可以做一些更看中你的respone
    });
    即preventDefault();
});
 

在views.py

 进口JSON
从django.shortcuts导入*
从django.template进口的RequestContext
从linki.forms导入*

高清广告(要求):
    如果request.method ==POST:
        表= AdvertForm(request.POST)

        消息=不对!
        如果(form.is_valid()):
            打印(request.POST ['标题'])
            消息= request.POST ['标题']

        返回的Htt presponse(json.dumps({消息:消息}))

    返回render_to_response('触点/ advert.html',
            {'形式':AdvertForm()},RequestContext的(要求))
 

这样一来你把的消息 DIV的响应。而不是返回纯HTML您应该返回JSON。

I am newbie working with django. I need simple example. How to submit form (post) without refreshing page using Django, Ajax, jQuery?

This is my form, views and template:

views.py

from django.shortcuts import *
from django.template import RequestContext
from linki.forms import *

def advert(request):
    if request.method == "POST":
        form = AdvertForm(request.POST)

        if(form.is_valid()):
            print(request.POST['title'])
            message = request.POST['title']

        else:
            message = 'something wrong!'


        return render_to_response('contact/advert.html',
                {'message':message},
            context_instance=RequestContext(request))

    else:
        return render_to_response('contact/advert.html',
                {'form':AdvertForm()},
            context_instance=RequestContext(request))

forms.py (form using "ModelForm")

from django import forms
from django.forms import ModelForm
from linki.models import Advert


class AdvertForm(ModelForm):
    class Meta:
        model = Advert

TEMPLATES (form html code)

<html>
<head>

</head>
    <body>
    <h1>Leave a Suggestion Here</h1>
        {% if message %}
            {{ message }}
        {% endif %}
        <div>
            <form action="" method="post">{% csrf_token %}
                {{ form.as_p }}
                <input type="submit" value="Submit Feedback" />
            </form>
        </div>
    </body>
</html>

解决方案

if you are planning to use an ajax submit with jquery you should not return html from your view.. I propose you to do this instead:

html:

<html>
<head>
</head>
<body>
    <h1>Leave a Suggestion Here</h1>
        <div class="message"></div>
        <div>
            <form action="" method="post">{% csrf_token %}
                {{ form.as_p }}
                <input type="submit" value="Submit Feedback" />
            </form>
        </div>
</body>
</html>

the js

$('#form').submit(function(e){
    $.post('/url/', $(this).serialize(), function(data){ ... 
       $('.message').html(data.message);
       // of course you can do something more fancy with your respone
    });
    e.preventDefault();
});

the views.py

import json
from django.shortcuts import *
from django.template import RequestContext
from linki.forms import *

def advert(request):
    if request.method == "POST":
        form = AdvertForm(request.POST)

        message = 'something wrong!'
        if(form.is_valid()):
            print(request.POST['title'])
            message = request.POST['title']

        return HttpResponse(json.dumps({'message': message}))

    return render_to_response('contact/advert.html',
            {'form':AdvertForm()}, RequestContext(request))

so that way you put the response in the message div. instead of returning plain html you should return json.

这篇关于如何在不使用Django,阿贾克斯,jQuery的清爽页面提交表单?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆