Django的产生时,POST数据发送和模型进行访问空白页 [英] Django produces blank pages when POST data is sent and Models are accessed

查看:206
本文介绍了Django的产生时,POST数据发送和模型进行访问空白页的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我拉我的头发了这一点。我正在开发在Django 1.3 web应用程序(从1.2.5的微弱希望这可以解决我的错误最近升级),我已经遇到一个错误的bizzare在我的意见之一。

I am pulling my hair out over this. I'm developing a webapp in Django 1.3 (recently upgraded from 1.2.5 in a feeble hope that this might solve my error) and I've come across a bizzare bug in one of my views.

视图被称为从客户端AJAX请求。如果我调用视图作为一个GET请求,一切都很好,很正常。如果我把它称为是没有数据的POST请求,一切都很好,很正常。但是,如果我把它称为POST和实际包含数据(无论这些数据是什么)的Django会返回一个空白页。

The view is called as an AJAX request from the client. If I call the view as a GET request, all is fine and dandy. If I call it as a POST request with no data, all is fine and dandy. But, if I call it as POST and actually include data (no matter what that data is) Django returns a blank page.

作为鼓捣的那种,我发现,我可以从我的观点打印,并获得在调试控制台输出,让我去上班,发现了这个奇怪的怪胎:

Being the kind to tinker, I discovered that I can print from my view and get output in the debug console, so I went to work, and discovered this strange oddity:

C = Character.objects.get(ID = INT(字符))

c = Character.objects.get(id=int(character))

这是code在该视图中,与一个模型处理的第一道防线。打印表明,不仅是人品一个理智的价值(这是由该请求传递的ID),但C实际上结束了正确的数据库记录。无论如何,如果该行被调用,Django的输出请求是齐尔希。虚无缥缈。什么也没有。

This is the first line of code in that view that deals with a model. Printing shows that not only is character a sane value (it's the ID passed in by the request) but c is actually ending up with the correct database record. Regardless, if that line is called, Django's output for the request is zilch. Nada. Nothing.

如果我做任何事情与我的模型之前提前返回,Django的正确呈现请求的其余部分。做一些与模型引起响应200被发送,但内容似乎被删去。

If I return early before doing anything with my Models, Django correctly renders the rest of the request. Doing something with the Models causes a response 200 to be sent, but the content seems to be omitted.

这似乎只发生时,POST数据发送,而不管该职位数据是否被读取或以任何方式使用。我被这种完全彻底难倒。我要发表我的code在这里,希望有人在这里有一些线索,以什么妖术可能会在幕后。

This only seems to happen when POST data is sent, regardless of whether that post data is read or used in any way. I'm completely and utterly baffled by this. I'm going to post my code here and hope that someone here has some clue as to what black magic might be going on behind the scenes.

这是视图本身,这是直接从urls.py名为:

This is the view itself, which is called straight from urls.py:

@csrf_exempt
def ajax_update(request, character):
    #determine the timestamp to send the client *before* polling
    #the database

    update_time = int(time.time())

    #grab each update... thingy as json, and put it all together
    output = {
        "events": current_events(request, character, "default"),
        "character": character,
        "update_time": update_time,
    }

    return HttpResponse(json.dumps(output), mimetype = "application/json")

这是current_events code,其中包含所提到的行:

This is the current_events code, which contains the mentioned line:

def current_events(request, character, type = "default", timestamp = 0):
    '''
    purpose: grab the last 3 days worth of events, or the last 10 events,
    whichever is a larger list.
    '''

    #TODO: actually do something with timestamp
    c = Character.objects.get(id=int(character))

    #DEBUG: What on earth is going on here?
    print c

    #TODO: See if this can be optimized into a single query
    event_links = EventLink.objects.filter(viewer=c)
    events = Event.objects.filter(id__in=event_links.values('event')).order_by('timestamp')

    t = loader.get_template('ajax/world-events.html')

    output = []

    for event in events:
        revisions = Event.objects.filter(original_event=event.id).order_by('timestamp')

        display_message = event.message

        history = []
        if len(revisions):
            history.append(event) #first item in the history is the original event
            history.extend(revisions)   
            display_message = history[-1].message

        output.append({
            "id": event.id,
            "writer": event.writer.id,
            "type": event.type,
            "timestamp": int(time.mktime(event.timestamp.timetuple())),
            "message": t.render(RequestContext(request, {"event_text": display_message, "event": event, "history": history}))
        })

    return output

最后,这是它调用code,使用jQuery,从客户端CoffeeScript的:

And finally, this is the CoffeeScript which calls the code, using jQuery, from the client:

auto_refresh_abort = 0
last_server_timestamp = 0
window.update_display = ->  
    #ajax call to grab events from the server
    $.ajax 
        type: 'POST'
        url: "/ajax/update/"+window.active_character
        data: 
            "last_update": last_server_timestamp
        dataType: "json"
        success: (output) ->
            auto_refresh_abort = 0
            update_character_log output.events
        error: (XMLHttpRequest, textStatus, errorThrown) ->
            auto_refresh_abort += 1
            console.log XMLHttpRequest

    return null

如果你认为这将有助于(这是很多的话),我可以张贴所需的任何其他code样品,如c本身模型$ C $。我可以证实,这是正在看在我的测试用例的性格确实存在,而且它只是一个标准的Django的ID,我没有做什么奇怪吧。

If you think it would help (this is a LOT already) I can post any other code samples needed, like the model code itself. I can confirm that the Character which is being looked up in my test cases does exist, and it's just a standard Django ID, I'm not doing anything weird with it.

在此先感谢。

更新:根据要求,这里是一个什么样的输出应该包含一个样本:

Update: As requested, here is a sample of what output should contain:

{'update_time': 1305984080, 'character': u'1', 'events': /*snip*/}

我一路走过证实,code执行被制作它,并传递到Htt的presponse输出结果中包含此数据。对于笑声,我插一回鲜美我的查询字符的行之后,使输出的数据应该是:

I've verified that code execution is making it all the way through, and that the output passed to HttpResponse does contain this data. For giggles, I inserted a return "TASTY" after the line where I poll Character, so that output's data should be:

{'update_time': 1305984080, 'character': u'1', 'events': 'TASTY'}

和确认这是工作。我可以打印输出到调试控制台右侧前的Htt prequest,这是显示出来。

and confirmed that this is working. I can "print output" to the debug console right before HttpRequest and this is displayed.

如果我注释掉处理字符所在的行,我的浏览器接收该数据作为请求的内容。另外,如果我离开code原样,只是不发送POST数据,浏览器收到正确的数据。其中的出现故障的具体情况是,当我从client--在POST数据已经发送在这种情况下,浏览器接收完全相同的HTTP标头后操纵模型,但没有任何内容。它接收标头是:

If I comment out the line dealing with Character, my browser receives this data as the content of the request. Also, if I leave the code as-is and simply do not send POST data, the browser receives the correct data. The specific case where it fails is when I manipulate models after having sent in POST data from the client-- in this case the browser receives exactly the same HTTP headers, but no content. The headers that it receives are:

Content-Type:application/json
Date:Sat, 21 May 2011 13:29:38 GMT
Server:WSGIServer/0.1 Python/2.6

但没有实际内容的交付。

but no actual content is delivered.

推荐答案

我想我可能已经找到我的问题。当我迷上我的应用程序多达Apache和使用Django的开发服务器停了下来,的bizzare错误消失。响应现在总是得到发送。

I think I may have found my issue. As soon as I hooked my app up to Apache and stopped using Django's development server, the bizzare bug disappeared. Responses are now always getting sent.

我还是不知道的原因是我自己的code的错误,或者这仅仅是一个奇怪的怪癖开发服务器,所以我张贴这个答案的希望,如果别人运行到开发过程中类似的错误,这可能照到这个问题的一些情况。

I still don't know if the cause is a bug in my own code, or if this is just a weird quirk of the development server, so I'm posting this answer in the hopes that if anyone else runs into a similar error during development, this might shine some light on the subject.

看来,开发服务器是挑剔什么HTTP头它,不喜欢接受POST数据。我将编辑这个更多的细节,如果我都不能缩小到底是什么导致了问题,在什么情况下。我的一个临时的解决办法是使用Firefox,它的头Django的似乎想发展。 Apache下生产环境不受此漏洞。

It seems that the development server is picky about what HTTP headers it does and doesn't like to accept for POST data. I'll edit this with more details if I can ever narrow down what exactly causes the issue, and in what circumstances. A temporary fix for me was to develop using Firefox, whose headers Django seems to like. The production environment under Apache isn't affected by this bug.

这篇关于Django的产生时,POST数据发送和模型进行访问空白页的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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