开发者

Django jquery form submissions returning 500

I am trying to do a modelform submission is Django with jquery and I am getting a 500 server response and not sure how to proceed.

This is my js:

function addUpdate(e) {
    e.preventDefault();
    var form = jQuery(e.target);
    jQuery.ajax({
        url: form.attr('action'),
        type: form.attr('method'),
        data: form.serialize(),
        dataType: 'json',
        success: function(){
            $('<p>Been Added</p>').insertBefore("div.tr-list");
        }
    });
};

jQuery("form#tr-form").submit(function(e){
    addUpdate(e);
});

This is my form:

<input class="nidden" type="button" id="tr-trigger" value="Add Resource" />

    <form class="absolute" id="tr-form" action="{% url topic_resource_create topic.person.user topic.slug %}" method="POST">{% csrf_token %}
    <div id="tr-wrapper">
        {{ tr_form.as_p }}

        <input id="tr-submit" type="submit" value="Submit" />
        <input type="reset" value="Reset" />
    </div>
    </form>

This is my view:

def tr_create_xhr(request, slug):
    if request.method == "POST":
        form = TopicResourceForm(request.POST)
        try:
            r = Resource.objects.get(url=form.cleaned_data['resource'])
        except Resource.DoesNotExist:
            r = Resource.objects.create(url=form.cleaned_data['resource'], rtype=form.cleaned_data['rtype'])
            r.save()
        form.resource = r
        topic = Topic.objects.get(person__user=request.user, slug__iexact=slug)
        form.topic = topic
        if form.is_valid():
            form.save()
        response = serializers.serialize('json', form)

    if request.is_ajax():
        return HttpResponse(response, content_type="application/javascript")
    else:
        return HttpRespons开发者_JAVA技巧eRedirect("../..")

I am not sure if they view logic is correct because I get a 500 server error everytime i try and post to the url. I have a couple other similar form submissions that are giving me the same error.


Firebug should show you the actual error. However, in this case it seems likely that the problem is in your serialization code. You can't call serializers.serialize on a form - that makes no sense at all. The serializers work on a queryset.

What you need to do is get the object returned from saving the form, wrap that in a list, and then serialize it:

obj = form.save()
response = serializers.serialize('json', [obj])

However you also have some issues with the flow through your view - there's no object if the form is not valid, for example.


usually means can't find jquery files. look at the source of your html header. should be something like this and if you follow the link you should see the jquery script.

<script type="text/javascript" src="/static/js/jquery-1.6.2.js"></script>

your base template would have something like this

<script type="text/javascript" src="{{ STATIC_URL }}js/jquery-1.6.2.js"></script>

You may also find the documentation on static files useful.


Maybe it is because of csrf-protection? Symptoms are very similar. https://docs.djangoproject.com/en/1.3/ref/contrib/csrf/

So you can try to add {% csrf_token %} somewhere between <form> and </form> tags.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜