Django WSGI:发生服务器错误。如何在webapp中使用wsgi

时间:2018-02-27 18:58:43

标签: python django wsgi

我是Django的新手,并尝试使用Django和wsgi创建一个Web应用程序来生成服务器。 但是我对Django有一些错误和误解  这是我的wsgi.py代码:

from django.shortcuts import render
import urllib
import urllib2
from urllib2 import urlopen
import json
from wsgiref.simple_server import make_server

from django.http import HttpResponse

def map(request):
    return render(request,'WebPage1.html')

def searchMap(request):       
    address =request.POST['city']       
    return render(request,'WebPage1.html',{'Address':address})

def routers():

    urlpatterns = (
        ('/map',map),
        ('/searchMap',searchMap)

    )
    return urlpatterns
def application(environ, start_response):
    print(environ['PATH_INFO'])
    path=environ['PATH_INFO']
    status = '200 OK'
    response_headers = [('Content-Type', 'text/plain')]
    urlpatterns = routers()
    func = None
    for item in urlpatterns:
        if item[0] == path:
            func = item[1]
            break
    if func:
        return func(environ)
    else:
        return ["Hello World".encode("utf-8")]

httpd = make_server('', 8080, application)

print('Serving HTTP on port 8080...')
httpd.serve_forever()

我的HTML:

<!DOCTYPE html>
<html>
  <head>
    <title>Simple Map</title>
    <meta name="viewport" content="initial-scale=1.0">
    <meta charset="utf-8">
    <style>

  #map {
    height: 100%;
  }

  html, body {
    height: 100%;
    margin: 0;
    padding: 0;
  }
   #floating-panel {
    position: absolute;
    top: 10px;
    left: 25%;
    z-index: 5;
    background-color: #fff;
    padding: 5px;
    border: 1px solid #999;
    text-align: center;
    font-family: 'Roboto','sans-serif';
    line-height: 30px;
    padding-left: 10px;
  }
    </style>
  </head>
  <body>
      <div id="map"></div>
      <div id="floating-panel">
          <form method="POST" action="/searchMap/"> 
                 {% csrf_token %} 
                 <input name="city" type="text">
                 <input type="submit" value="Geocode" onclick="myMap()">
              </form>
      </div>      
      <div>
        {{Address}}
     </div>
  </body>
</html>

我到达http://127.0.0.1:8080/map后收到的错误消息:

Exception happened during processing of request from ('127.0.0.1', 54038)
Traceback (most recent call last):
  File "C:\Python27\Lib\SocketServer.py", line 290, in _handle_request_noblock
self.process_request(request, client_address)
  File "C:\Python27\Lib\SocketServer.py", line 318, in process_request
self.finish_request(request, client_address)
  File "C:\Python27\Lib\SocketServer.py", line 331, in finish_request
self.RequestHandlerClass(request, client_address, self)
  File "C:\Python27\Lib\SocketServer.py", line 652, in __init__
self.handle()
  File "C:\Python27\Lib\wsgiref\simple_server.py", line 131, in handle
handler.run(self.server.get_app())
  File "C:\Python27\Lib\wsgiref\handlers.py", line 92, in run
self.close()
  File "C:\Python27\Lib\wsgiref\simple_server.py", line 33, in close
self.status.split(' ',1)[0], self.bytes_sent
AttributeError: 'NoneType' object has no attribute 'split'

apache日志显示:

[Wed Feb 28 03:01:42 2018] [error] /map
[Wed Feb 28 03:01:42 2018] [error] Traceback (most recent call last):
[Wed Feb 28 03:01:42 2018] [error]   File "C:\\Python27\\Lib\\wsgiref\\handlers.py", line 85, in run
[Wed Feb 28 03:01:42 2018] [error]     self.result = application(self.environ, self.start_response)
[Wed Feb 28 03:01:42 2018] [error]   File "C:/Users/alienware/Downloads/DjangoWebProject12/DjangoWebProject12/wsgi.py", line 181, in application
[Wed Feb 28 03:01:42 2018] [error]     return func(environ)
[Wed Feb 28 03:01:42 2018] [error]   File "C:/Users/alienware/Downloads/DjangoWebProject12/DjangoWebProject12/wsgi.py", line 146, in map
[Wed Feb 28 03:01:42 2018] [error]     return render(request,'WebPage1.html')
[Wed Feb 28 03:01:42 2018] [error]   File "C:\\Python27\\lib\\site-packages\\django\\shortcuts.py", line 30, in render
[Wed Feb 28 03:01:42 2018] [error]     content = loader.render_to_string(template_name, context, request, using=using)
[Wed Feb 28 03:01:42 2018] [error]   File "C:\\Python27\\lib\\site-packages\\django\\template\\loader.py", line 67, in render_to_string
[Wed Feb 28 03:01:42 2018] [error]     template = get_template(template_name, using=using)
[Wed Feb 28 03:01:42 2018] [error]   File "C:\\Python27\\lib\\site-packages\\django\\template\\loader.py", line 18, in get_template
[Wed Feb 28 03:01:42 2018] [error]     engines = _engine_list(using)
[Wed Feb 28 03:01:42 2018] [error]   File "C:\\Python27\\lib\\site-packages\\django\\template\\loader.py", line 72, in _engine_list
[Wed Feb 28 03:01:42 2018] [error]     return engines.all() if using is None else [engines[using]]
[Wed Feb 28 03:01:42 2018] [error]   File "C:\\Python27\\lib\\site-packages\\django\\template\\utils.py", line 89, in all
[Wed Feb 28 03:01:42 2018] [error]     return [self[alias] for alias in self]
[Wed Feb 28 03:01:42 2018] [error]   File "C:\\Python27\\lib\\site-packages\\django\\template\\utils.py", line 86, in __iter__
[Wed Feb 28 03:01:42 2018] [error]     return iter(self.templates)
[Wed Feb 28 03:01:42 2018] [error]   File "C:\\Python27\\lib\\site-packages\\django\\utils\\functional.py", line 35, in __get__
[Wed Feb 28 03:01:42 2018] [error]     res = instance.__dict__[self.name] = self.func(instance)
[Wed Feb 28 03:01:42 2018] [error]   File "C:\\Python27\\lib\\site-packages\\django\\template\\utils.py", line 29, in templates
[Wed Feb 28 03:01:42 2018] [error]     self._templates = settings.TEMPLATES
[Wed Feb 28 03:01:42 2018] [error]   File "C:\\Python27\\lib\\site-packages\\django\\conf\\__init__.py", line 56, in __getattr__
[Wed Feb 28 03:01:42 2018] [error]     self._setup(name)
[Wed Feb 28 03:01:42 2018] [error]   File "C:\\Python27\\lib\\site-packages\\django\\conf\\__init__.py", line 39, in _setup
[Wed Feb 28 03:01:42 2018] [error]     % (desc, ENVIRONMENT_VARIABLE))
[Wed Feb 28 03:01:42 2018] [error] ImproperlyConfigured: Requested setting TEMPLATES, but settings are not configured. You must either define the environment variable DJANGO_SETTINGS_MODULE or call settings.configure() before accessing settings.
[Wed Feb 28 03:01:42 2018] [error] 127.0.0.1 - - [28/Feb/2018 03:01:42] "GET /map HTTP/1.1" 500 59

我不知道是否正确编写像这样的wsgi服务器。

1 个答案:

答案 0 :(得分:2)

简单地说,我认为Django开发人员非常罕见地修改/修改他们的wsgi.py文件。 Django应该为你做正确设置的工作,你可以让它坐下来。

我建议深入了解这里的大部分社区已经阅读过的Django example app。它将向您介绍Django应用程序是什么以及所有内容是如何连接在一起的。