简体   繁体   中英

Django WSGI: A server error occurred. How to use wsgi in webapp

I am new to Django, and trying to create a web app use Django and wsgi to generate a server. But I got some error and misunderstanding about Django.
Here is the code of my 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()

And my 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>

And the error message I get when I get to 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'

The apache logs shows that:

[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

I don't know if it's right or not to write the wsgi server like this.

Simply put, I think it is very rare that Django developers docter/modify their wsgi.py files. Django should do the work for you of setting that up properly and you can let it sit.

I'd recommend digging through the Django example app that most of the community here has read through. It will introduce you to what a Django App is and how everything is wired together.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM