【问题标题】:How to fix "Incomplete response received from application"如何修复“从应用程序收到的不完整响应”
【发布时间】:2019-12-04 05:08:52
【问题描述】:

我一定要疯了……

如果我有 48 行或更少行的 views.py 文件,当我向它发布数据时,我会看到

从应用程序收到不完整的响应

但是,如果我有 49 行或更多行,我会得到一个

NameError, 'request' 未定义

在第 31 和 49 行抛出,即使第 31/49 行为空。

有人能解释一下发生了什么吗?

顺便说一句,Django Admin - "Incomplete response received from application" 没有回答我的问题。

veiws.py:

from django.shortcuts import render
from django.shortcuts import render
from django.shortcuts import HttpResponse
from django.core.exceptions import *
from datetime import datetime
def remove_xss(chat, request):
    return chat
def find_urls(chat, request):
    new_chat = ' '.join([('<a href="{}">{}</a>'.format(word, word) if ('http://' in word or 'https://' in word) else word) for word in chat.split(' ')])
    return new_chat
def format_chat(chat, username, request):
    chat = remove_xss(chat, request)
    chat = find_urls(chat, request)
    request = request
    return "hi"
def chat_index(request):
    return render(request, 'chatroom.html')
def chat(request):
    if request.method == 'POST':
        chat = request.POST.get('textfield', None)
        if request.user.is_authenticated():
            u = request.user.username
            f_chat = format_chat(chat, u, request)
        else:
            return HttpResponse('You must be signed in to continue')
        with open('chats.txt', 'a') as chats: 
            chats.write(f_chat)    
    return render(request, 'chatroom.html')

urls.py:(工作(我认为))

from chat import views

from django.urls import path

urlpatterns = [
    path('/', views.chat_index),
    path('chat/', views.chat)
]

完整回溯:(当有 >=49 行时)

Traceback:

File "/home/raveivcs/virtualenv/backend/3.5/lib/python3.5/site-packages/django/core/handlers/exception.py" in inner
  34.             response = get_response(request)

File "/home/raveivcs/virtualenv/backend/3.5/lib/python3.5/site-packages/django/core/handlers/base.py" in _get_response
  115.                 response = self.process_exception_by_middleware(e, request)

File "/home/raveivcs/virtualenv/backend/3.5/lib/python3.5/site-packages/django/core/handlers/base.py" in _get_response
  113.                 response = wrapped_callback(request, *callback_args, **callback_kwargs)

File "/home/raveivcs/backend/chat/views.py" in chat
  49.     

File "/home/raveivcs/backend/chat/views.py" in format_chat
  31.     

Exception Type: NameError at /chat/chat/
Exception Value: name 'request' is not defined

【问题讨论】:

  • 尝试将请求作为所有视图函数的第一个参数。
  • 没有骰子,但谢谢

标签: python django


【解决方案1】:

好的,对于任何来到这里的人,您所需要做的就是查看不同的文件。错误不必在您认为它所在的文件中。

【讨论】:

  • 如果您解决了自己的问题,发布解决方案会有所帮助。还是我错过了什么?
猜你喜欢
  • 2021-04-09
  • 2018-12-03
  • 1970-01-01
  • 1970-01-01
  • 2021-12-05
  • 2015-08-22
  • 2015-05-28
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多