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

2024-04-24 08:10:36 发布

您现在位置:Python中文网/ 问答频道 /正文

我是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服务器对不对。你知道吗


Tags: djangoinpyselfreturnrequestlibline
1条回答
网友
1楼 · 发布于 2024-04-24 08:10:36

简单地说,我认为Django开发人员篡改/修改他们的wsgi.py公司文件夹。Django应该为你做正确设置的工作,你可以让它坐着。你知道吗

我建议你仔细阅读这里大多数社区都读过的Django example app。它将向您介绍什么是Django应用程序,以及所有东西是如何连接在一起的。你知道吗

相关问题 更多 >