2017-10-05 1 views
3

AJAX로 장고 백엔드에 요청하려고합니다. AJAX가 작동하는 부분을 얻었으나 이제 mpld3 그림을 페이지에 표시하려고합니다. 여기Django보기의 Mpld3 - "NoneType"개체에 "split"속성이 없습니다.

   <form id="pointform" method="POST"> 
        {% csrf_token %} 
        <div class="row"> 
        <div class="col-lg-6 col-md-6"> 
         <input name="x_coord" type="text" accept=""id="x_coordinate" class="form-control form-control-md" type="text" placeholder="X-Coordinate"> 
        </div> 
        <div class="col-lg-6 col-md-6"> 
         <input name="y_coord" type="text" id="y_coordinate" class="form-control form-control-md" type="text" placeholder="Y-Coordinate"> 
        </div> 
        </div> 
        <button style="width: 100%" type="submit" class="btn btn-danger">Add Point</button> 
       </form> 

내 AJAX 요청한다 : 이것은 내보기입니다

 $.ajax({ 
      type: "POST", 
      url: url, 
      data: data, 
      dataType: "json", 
      success: function(json){ 

      },    
     }); 

: 여기

import mpld3 
import matplotlib.pyplot as plt 

X = [] 
y = [] 

def get_figure(request): 

    x_coord = request.POST['X'] 
    y_coord = request.POST['Y'] 

    X.append(x_coord) 
    y.append(y_coord) 

    fig = plt.figure() 
    plt.scatter(X, y) 
    model = mpld3.fig_to_html(fig) 

    data = {'Model': model} 

    return JsonResponse(data) 

전체 역 추적이에서 오는

는 HTML 양식입니다 명령 줄 :

File "C:\Users\User\AppData\Local\Programs\Python\Python35\lib\site-packages\django\core\handlers\exception.py", line 39, in inner 
    response = get_response(request) 
    File "C:\Users\User\AppData\Local\Programs\Python\Python35\lib\site-packages\django\core\handlers\base.py", line 187, in _get_response 
    response = self.process_exception_by_middleware(e, request) 
    File "C:\Users\User\AppData\Local\Programs\Python\Python35\lib\site-packages\django\core\handlers\base.py", line 185, in _get_response 
    response = wrapped_callback(request, *callback_args, **callback_kwargs) 
    File "C:\Users\User\Desktop\mlapp06\regression\views.py", line 24, in get_regression_graph 
    fig = plt.figure() 
    File "C:\Users\User\AppData\Local\Programs\Python\Python35\lib\site-packages\matplotlib\pyplot.py", line 535, in figure 
    **kwargs) 
    File "C:\Users\User\AppData\Local\Programs\Python\Python35\lib\site-packages\matplotlib\backends\backend_tkagg.py", line 81, in new_figure_manager 
    return new_figure_manager_given_figure(num, figure) 
    File "C:\Users\User\AppData\Local\Programs\Python\Python35\lib\site-packages\matplotlib\backends\backend_tkagg.py", line 98, in new_figure_manager_given_figure 
    icon_img = Tk.PhotoImage(file=icon_fname) 
    File "C:\Users\User\AppData\Local\Programs\Python\Python35\lib\tkinter\__init__.py", line 3394, in __init__ 
    Image.__init__(self, 'photo', name, cnf, master, **kw) 
    File "C:\Users\User\AppData\Local\Programs\Python\Python35\lib\tkinter\__init__.py", line 3350, in __init__ 
    self.tk.call(('image', 'create', imgtype, name,) + options) 
RuntimeError: main thread is not in main loop 
[05/Oct/2017 15:54:51] "POST /ajax/get_regression_graph/ HTTP/1.1" 500 18714 
Traceback (most recent call last): 
    File "C:\Users\User\AppData\Local\Programs\Python\Python35\lib\wsgiref\handlers.py", line 138, in run 
    self.finish_response() 
    File "C:\Users\User\AppData\Local\Programs\Python\Python35\lib\wsgiref\handlers.py", line 180, in finish_response 

    self.write(data) 
    File "C:\Users\User\AppData\Local\Programs\Python\Python35\lib\wsgiref\handlers.py", line 274, in write 
    self.send_headers() 
    File "C:\Users\User\AppData\Local\Programs\Python\Python35\lib\wsgiref\handlers.py", line 332, in send_headers 
    self.send_preamble() 
    File "C:\Users\User\AppData\Local\Programs\Python\Python35\lib\wsgiref\handlers.py", line 255, in send_preamble 
    ('Date: %s\r\n' % format_date_time(time.time())).encode('iso-8859-1') 
    File "C:\Users\User\AppData\Local\Programs\Python\Python35\lib\wsgiref\handlers.py", line 453, in _write 
    result = self.stdout.write(data) 
    File "C:\Users\User\AppData\Local\Programs\Python\Python35\lib\socket.py", line 593, in write 
    return self._sock.send(b) 
ConnectionAbortedError: [WinError 10053] Eine bestehende Verbindung wurde softwaregesteuert 
durch den Hostcomputer abgebrochen 
[05/Oct/2017 15:54:51] "POST /ajax/get_regression_graph/ HTTP/1.1" 500 59 
---------------------------------------- 
Exception happened during processing of request from ('127.0.0.1', 53724) 
Traceback (most recent call last): 
    File "C:\Users\User\AppData\Local\Programs\Python\Python35\lib\wsgiref\handlers.py", line 138, in run 
    self.finish_response() 
    File "C:\Users\User\AppData\Local\Programs\Python\Python35\lib\wsgiref\handlers.py", line 180, in finish_response 

    self.write(data) 
    File "C:\Users\User\AppData\Local\Programs\Python\Python35\lib\wsgiref\handlers.py", line 274, in write 
    self.send_headers() 
    File "C:\Users\User\AppData\Local\Programs\Python\Python35\lib\wsgiref\handlers.py", line 332, in send_headers 
    self.send_preamble() 
    File "C:\Users\User\AppData\Local\Programs\Python\Python35\lib\wsgiref\handlers.py", line 255, in send_preamble 
    ('Date: %s\r\n' % format_date_time(time.time())).encode('iso-8859-1') 
    File "C:\Users\User\AppData\Local\Programs\Python\Python35\lib\wsgiref\handlers.py", line 453, in _write 
    result = self.stdout.write(data) 
    File "C:\Users\User\AppData\Local\Programs\Python\Python35\lib\socket.py", line 593, in write 
    return self._sock.send(b) 
ConnectionAbortedError: [WinError 10053] Eine bestehende Verbindung wurde softwaregesteuert 
durch den Hostcomputer abgebrochen 

During handling of the above exception, another exception occurred: 

Traceback (most recent call last): 
    File "C:\Users\User\AppData\Local\Programs\Python\Python35\lib\wsgiref\handlers.py", line 141, in run 
    self.handle_error() 
    File "C:\Users\User\AppData\Local\Programs\Python\Python35\lib\site-packages\django\core\servers\basehttp.py", line 95, in handle_error 
    super(ServerHandler, self).handle_error() 
    File "C:\Users\User\AppData\Local\Programs\Python\Python35\lib\wsgiref\handlers.py", line 368, in handle_error 
    self.finish_response() 
    File "C:\Users\User\AppData\Local\Programs\Python\Python35\lib\wsgiref\handlers.py", line 180, in finish_response 

    self.write(data) 
    File "C:\Users\User\AppData\Local\Programs\Python\Python35\lib\wsgiref\handlers.py", line 274, in write 
    self.send_headers() 
    File "C:\Users\User\AppData\Local\Programs\Python\Python35\lib\wsgiref\handlers.py", line 331, in send_headers 
    if not self.origin_server or self.client_is_modern(): 
    File "C:\Users\User\AppData\Local\Programs\Python\Python35\lib\wsgiref\handlers.py", line 344, in client_is_modern 
    return self.environ['SERVER_PROTOCOL'].upper() != 'HTTP/0.9' 
TypeError: 'NoneType' object is not subscriptable 

During handling of the above exception, another exception occurred: 

Traceback (most recent call last): 
    File "C:\Users\User\AppData\Local\Programs\Python\Python35\lib\socketserver.py", line 625, in process_request_thread 
    self.finish_request(request, client_address) 
    File "C:\Users\User\AppData\Local\Programs\Python\Python35\lib\socketserver.py", line 354, in finish_request 
    self.RequestHandlerClass(request, client_address, self) 
    File "C:\Users\User\AppData\Local\Programs\Python\Python35\lib\socketserver.py", line 681, in __init__ 
    self.handle() 
    File "C:\Users\User\AppData\Local\Programs\Python\Python35\lib\site-packages\django\core\servers\basehttp.py", line 174, in handle 
    handler.run(self.server.get_app()) 
    File "C:\Users\User\AppData\Local\Programs\Python\Python35\lib\wsgiref\handlers.py", line 144, in run 
    self.close() 
    File "C:\Users\User\AppData\Local\Programs\Python\Python35\lib\wsgiref\simple_server.py", line 36, in close 
    self.status.split(' ',1)[0], self.bytes_sent 
AttributeError: 'NoneType' object has no attribute 'split' 

왜 그런 일이 발생했는지에 대한 제안이 있으십니까?

미리 감사드립니다.

+0

는 전체 스택 추적을 게시 할 수 있습니다 :이 비슷한 질문이 문제를 해결하는 데 도움 여부

확인하시기 바랍니다? – pazitos10

+0

좋아요, done ----- – OhMad

+0

'self.status'가'None' 이외의 것으로 설정되어 있다고 가정하기 때문에 발생하지만'None'입니다. –

답변

4

그래픽 출력이없는 서버에서 코드가 실행되고있는 것 같습니다. 따라서 그림을 만드는 것은 실패 할 수 있습니다. GUI가 아닌 백엔드를 사용할 수 있습니다.

import matplotlib 
matplotlib.use("Agg") 
import mpld3 
import matplotlib.pyplot as plt 
# ... rest of code 
+0

아니, 불행히도. 내 localhost에서 실행 중입니다. – OhMad

+1

localhost는 서버입니다. 위의 코드 또는 다른 코드를 실행할 때 동일한 오류가 발생합니까? (나는 그것이 다르다는 것을 짐작할 것입니다, 그러므로 그것의 완전한 오류 역 추적을 아는 것이 좋을 것입니다) – ImportanceOfBeingErnest

+0

그것은 여전히 ​​동일합니다. – OhMad

2

다른 스레드에서 이것을 호출합니까? TKinter는 주 스레드에서 실행해야합니다. RuntimeError: main thread is not in main loop https://www.reddit.com/r/learnpython/comments/69z9fs/error_main_thread_not_in_main_loop/

+0

웹 출력을위한 그림을 만들 때 실제로 Tkinter를 사용하고 싶지는 않다는 것이 더 중요합니다. 따라서 내 대답에 제안 된대로 백엔드를 변경하면 아마도 갈 경로 일 것입니다. – ImportanceOfBeingErnest