Django的阿贾克斯的ModelForm提交一个空的形式request.POST形式、阿贾克斯、Django、ModelForm

2023-09-11 22:32:46 作者:清酒话桑麻

我有点新手,当涉及到的Django,阿贾克斯,和jQuery,所以我的时间提前道歉,如果我的问题脱落是微不足道的。

I'm a bit of a newbie when it comes to django, ajax, and jquery, so i apologize ahead of time if my question comes off being trivial.

我一直在抓我的头在这一个有一段时间了,但我想使用AJAX和jQuery通过一个模态对话框窗口的jQuery UI提供提交一个Django的ModelForm形式。我遇到的问题是,它似乎是表单数据没有被传递到请求或在我的views.py文件,我的表单对象是有问题解析reqest.POST对象。无论如何,当我提交我的形式,form.is_valid()失败和审查的形式错误,我得到一个此字段是必需的为形式的每个输入。所以不知道我做错了,有问题的形式工作我试图模态与阿贾克斯IZE它之前。

I've been scratching my head on this one for some time now, but I am trying to use ajax and jquery to submit a Django ModelForm form via a modal dialog window provide by jQuery UI. The problem I am having is that it seems that the form data isn't being passed into the request or that my form object in my views.py file is having a problem parsing the reqest.POST object. Regardless when I submit my form, the form.is_valid() fails and reviewing the form errors I get a "This field is required" for every input of the form. So not sure what I am doing wrong, the form in question worked prior to me trying to "modal"ize it with ajax.

这是我的code,也许更有经验的眼睛能看到什么,我错过了。

here's my code, maybe a more seasoned eye can see what I missed.

forms.py

from django.forms import ModelForm
from artists.views import Artist

class RegistrationForm(ModelForm):
    username = forms.CharField(label=(u'User Name'))
    first_name = forms.CharField(label=(u'First Name'))
    last_name = forms.CharField(label=(u'Last Name'))
    email = forms.EmailField(label=(u'Email'))
    password = forms.CharField(label=(u'Password'),
                               widget=forms.PasswordInput(render_value=False))
    password = forms.CharField(label=(u'Verify Password'),
                               widget=forms.PasswordInput(render_value=False))

    class Meta:
        model = Artist
        exlude = (user, slug, work_email)

views.py

views.py

from django.http import HttpResponse
from django.contrib.auth.models import User
from django.shortcut import render
from django.template.defaultfilters import slugify
from django.utils.functional import Promise
from django.utils.encoding import force_text
from django.shortcuts import render

from artists.forms import RegistrationForm
from artists.models import Artist
import json

class LazyEncoder(json.JSONEncoder):
    def default(self, obj):
    if isinstance(obj, Promise):
        return force_text(obj)
    return super(LazyEncoder, self).default(obj)

def ValidateEmail(email):
    from django.core.validators import validate_email
    from django.core.exception import ValidationError
    try:
        validate_email(email)
        return True
    except ValidationError:
        return False

def ArtistRegistration(request):
    form = False

    if request.method == 'POST' and request.is_ajax():
        form = RegistrationForm(request.POST)
        if form.is_Valid():
            is_valid = True
            username = form.cleaned_data['username']
            password = form.cleaned_data['password']
            password1 = form.cleaned_data['password1']
            email = form.cleaned_data['email']
            first_name = form.cleaned_data['first_name'].title()
            last_name = form.cleaned_data['last_name'].title()

            if ValidateEmail(email) is False:
                is_valid = False
                message = "Email entry is invalid."

            if User.objects.filter(username=username).exists():
                is_valid = False
                message = "Username already exists."

            if password != password1:
                is_valid = False
                message = "Passwords do not match."

            if is_valid is False:
                response_dict = {"type": "error"}
                response_dict["message"] = message
                result = json.dumps(response_dict, cls=LazyEncoder)
                return HttpResponse(result, mimetype='application/json')
            else:
                user = User.objects.create_user)
                    username = username,
                    email = email,
                    password = password,
                    first_name = first_name,
                    last_name = last_name_
                user.save()
                artist = Artist(
                    user = user,
                    first_name = first_name,
                    last_name = last_name,
                    work_email = email,
                    slug = slugify('%s %s' % (first_name, last_name)),
                    department=form.cleaned_data['department'])
                artist.save()

                response_dict = {'status':1, 'type':'success'}
                result = json.dumps(response_dict, cls=LazyEncoder)
                return HttpResponse(result, mimetype='application/json')
        else:
            response_dict = {'type': 'error'}
            response_dict['message'] = 'form is invalid'
            response_dict['errors'] = json.dumps(form.errors)
            result = json.dumps(response_dict, cls=LazyEncoder)
            return HttpResponse(result, mimetype='application/json')

    else:
        form = RegistrationForm()
        context = {'form' : form}
        return render(request, 'register-modal.html', context)

下面是HTML,我的模态对话框电话: 寄存器modal.html

here is the html that my modal dialog calls: register-modal.html

<form action="{% url 'modal_registration' %}" method="post" id="register_form" name="register_form">
   {% csrf_token %}
   <table>
      <tbody>
         <tr>
            <th><label for="first_name">First Name:</label></th>
            <td>{{ form.first_name }}</td>
         </tr>
         <tr>
            <th><label for="last_name">Last Name:</label></th>
            <td>{{ form.last_name }}</td>
         </tr>
         <tr>
            <th><label for="email">Email:</label></th>
            <td>{{ form.email }}</td>
         </tr>
         <tr>
            <th><label for="username">Username:</label></th>
            <td>{{ form.username }}</td>
         </tr>
         <tr>
            <th><label for="password">Password:</label></th>
            <td>{{ form.password }}</td>
         </tr>
         <tr>
            <th><label for="password1">Verify Pswd:</label></th>
            <td>{{ form.password1 }}</td>
         </tr>
         <tr>
            <th><label for="department">Department:</label></th>
            <td>{{ form.department }}</td>
         </tr>
      </tbody>
   </table>
</form>
<span id="registration_error" name="registration_error" style="color: #FF0000;"></span>

最后,这里的js文件,使所有的jQuery和Ajax调用。 我省略了CSRF部分,因为它从Django文档逐字复制。

Finally here is the js file that makes all the jquery and ajax calls. I've omitted the csrf part because its copied verbatim from the Django docs.

register.js

register.js

$(document).ready(function () {
    $.ajaxSetup({traditional: true});

    var $register_dialog = $('#modal_register_div').dialog({
        autoOpen: false,
        title: "User Registration",
        closeOnEscape: true,
        draggable: false, 
        resizable: false,
        modal: true,
        width: 450,
        buttons: {
            "Register": function () {
                var $this = $(this);
                var frm = document.form['register_form'];
                $.ajax({
                    type: frm.method,
                    url: frm.action,
                    data: $('#register_form').serialize(),
                    contentType: "application/json;charset=utf-8",
                    dataType: 'json',
                    success: function(response){
                        if(response['type'] == 'success') {
                            //any extra success functionality goes here
                            $this.dialog("close");
                        } else {
                            $("#registration_error").html(response['message']);
                            console.log(response['errors']);
                        }
                    },
                    error: function(xhr, ajaxOptions, thrownError) {
                        alert(thrownError + '\n' + xhr.status + '\n' + ajaxOptions)
                    }
                });
                return false;
            }
        }
    });

    $('#register_artist').live("click", function () {
        $.ajax({
            type: 'get',
            dataType: 'html',
            url: '/register/'
            data: {},
            success: function(response){
                $register_dialog.empty().html(response).dialog('open');
            }
        });
    });
});

模态对话框绑定到一个隐藏分区的基本模板页面上为我的Django的页面,而JS脚本是在同一页上加载。叫通过点击一个空的href链路对话框。 anyrate,如果任何人能看到我在做什么错了,你的投入将真正成为AP preciated。

the modal dialog is tied to a hidden div on the base template page for my django page, and the js script is loaded on the same page. the dialog is called by clicking on a null href link. anyrate, if anyone can see what I am doing wrong, your input would truly be appreciated.

谢谢!

推荐答案

想通了,我的愚蠢了。在我的ajax调用我定义我的内容类型: 的contentType:应用/ JSON的;字符集= UTF-8

Figured out what my folly was. In my ajax call I was defining my content type as: contentType: 'application/json;charset=utf-8'

当它应该是以下几点: 的contentType:应用程序/ x-WWW的形式urlen codeD;字符集= UTF-8

when it should have been the following: contentType: 'application/x-www-form-urlencoded;charset=utf-8'

我意识到在看我的登录表单这是工作相比,我的登记表格的标题数据之后,是在我的登录表单中的数据正在通过Form_Data传递到请求,而我的登记表正在传递作为Request_Payload。进行更改后,一切工作就像一个魅力。

I realized after looking at the header data of my login form which was working compared to my register form, was that in my login form the data was being passed onto the request via "Form_Data" whereas my register form was being passed on as a "Request_Payload". After making the change, everything worked like a charm.

 
精彩推荐
图片推荐