Skip to content

Commit

Permalink
Form submissions now work (character encoding is assumed to be utf8 t…
Browse files Browse the repository at this point in the history
…hough, which may cause problems some time) - new example_forms.py demonstrates them
  • Loading branch information
Simon Willison committed May 12, 2009
1 parent 3349d37 commit 780b498
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 3 deletions.
3 changes: 3 additions & 0 deletions djng/__init__.py
Expand Up @@ -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
4 changes: 2 additions & 2 deletions djng/response.py
Expand Up @@ -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
Expand Down
9 changes: 8 additions & 1 deletion djng/wsgi.py
Expand Up @@ -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 = [
Expand Down
36 changes: 36 additions & 0 deletions 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.