如何解决“从应用收到的回复不完整”

时间:2019-07-25 21:08:05

标签: python django

我一定疯了...

如果我的views.py文件的行数少于或等于48行,则在向其发布数据时看到

  

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

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

  

NameError,未定义“请求”

即使在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

1 个答案:

答案 0 :(得分:0)

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