Skip to content
Browse files

Form submissions now work (character encoding is assumed to be utf8 t…

…hough, which may cause problems some time) - new example_forms.py demonstrates them
  • Loading branch information...
1 parent 3349d37 commit 780b4981241d30f4c6d164716ecc9eedba03fb11 Simon Willison committed May 12, 2009
Showing with 49 additions and 3 deletions.
  1. +3 −0 djng/__init__.py
  2. +2 −2 djng/response.py
  3. +8 −1 djng/wsgi.py
  4. +36 −0 example_forms.py
View
3 djng/__init__.py
@@ -3,4 +3,7 @@
from errors import ErrorWrapper
from response import Response
from wsgi import serve
+from django import forms
+from django.utils.html import escape
+from django.utils.safestring import mark_safe
#from template import TemplateResponse
View
4 djng/response.py
@@ -2,10 +2,10 @@
from Cookie import SimpleCookie
class Response(HttpResponseOld):
- default_charset = 'utf8'
+ _charset = 'utf8'
def __init__(self, content='', status=None, content_type=None):
if not content_type:
- content_type = 'text/html; charset=%s' % self.default_charset
+ content_type = 'text/html; charset=%s' % self._charset
if not isinstance(content, basestring) and\
hasattr(content, '__iter__'):
self._container = content
View
9 djng/wsgi.py
@@ -14,9 +14,16 @@ def get_script_name(environ):
# Now on with the real code...
from django import http
-from django.core.handlers.wsgi import STATUS_CODE_TEXT, WSGIRequest
+from django.core.handlers.wsgi import STATUS_CODE_TEXT
+from django.core.handlers.wsgi import WSGIRequest as WSGIRequestOld
import sys
+class WSGIRequest(WSGIRequestOld):
+ def __init__(self, environ):
+ super(WSGIRequest, self).__init__(environ)
+ # Setting self._encoding prevents fallback to django.conf.settings
+ self._encoding = 'utf8'
+
class WSGIWrapper(object):
# Changes that are always applied to a response (in this order).
response_fixes = [
View
36 example_forms.py
@@ -0,0 +1,36 @@
+import djng
+
+def index(request):
+ return djng.Response("""
+ <h1>Forms demo</h1>
+ <form action="/search/" method="get">
+ <p>
+ <input type="search" name="q">
+ <input type="submit" value="Search">
+ </p>
+ </form>
+ <form action="/submit/" method="post">
+ <p><textarea name="text" rows="5" cols="30"></textarea></p>
+ <p><input type="submit" value="Capitalise text"></p>
+ </form>
+ """)
+
+def search(request):
+ return djng.Response(
+ "This page would search for %s" % djng.escape(
+ request.GET.get('q', 'no-search-term')
+ )
+ )
+
+def submit(request):
+ text = request.POST.get('text', 'no-text')
+ return djng.Response(djng.escape(text.upper()))
+
+app = djng.Router(
+ (r'^$', index),
+ (r'^search/$', search),
+ (r'^submit/$', submit),
+)
+
+if __name__ == '__main__':
+ djng.serve(app, '0.0.0.0', 8888)

0 comments on commit 780b498

Please sign in to comment.
Something went wrong with that request. Please try again.