mirror of
https://github.com/django/django.git
synced 2025-07-04 09:49:12 +00:00
unicode: Added a unicode-aware version of urlencode. Since we now have a
collection of URL encoding functions, moved them into django.utils.http (out of django.utils.html). git-svn-id: http://code.djangoproject.com/svn/django/branches/unicode@5338 bcc190cf-cafb-0310-a4f2-bffc1f526a37
This commit is contained in:
parent
78b75b9065
commit
7b51097847
@ -10,10 +10,11 @@ from django.core.handlers.base import BaseHandler
|
||||
from django.core.handlers.wsgi import WSGIRequest
|
||||
from django.core.signals import got_request_exception
|
||||
from django.dispatch import dispatcher
|
||||
from django.http import urlencode, SimpleCookie, HttpRequest
|
||||
from django.http import SimpleCookie, HttpRequest
|
||||
from django.test import signals
|
||||
from django.utils.functional import curry
|
||||
from django.utils.encoding import smart_str
|
||||
from django.utils.http import urlencode
|
||||
|
||||
BOUNDARY = 'BoUnDaRyStRiNg'
|
||||
MULTIPART_CONTENT = 'multipart/form-data; boundary=%s' % BOUNDARY
|
||||
|
@ -125,22 +125,3 @@ def clean_html(text):
|
||||
return text
|
||||
clean_html = allow_lazy(clean_html, unicode)
|
||||
|
||||
def urlquote(url, safe='/'):
|
||||
"""
|
||||
A version of Python's urllib.quote() function that can operate on unicode
|
||||
strings. The url is first UTF-8 encoded before quoting. The returned string
|
||||
can safely be used as part of an argument to a subsequent iri_to_uri() call
|
||||
without double-quoting occurring.
|
||||
"""
|
||||
return force_unicode(urllib.quote(smart_str(url)))
|
||||
urlquote = allow_lazy(urlquote, unicode)
|
||||
|
||||
def urlquote_plus(url, safe=''):
|
||||
"""
|
||||
A version of Python's urllib.quote_plus() function that can operate on
|
||||
unicode strings. The url is first UTF-8 encoded before quoting. The
|
||||
returned string can safely be used as part of an argument to a subsequent
|
||||
iri_to_uri() call without double-quoting occurring.
|
||||
"""
|
||||
return force_unicode(urllib.quote_plus(smart_str(url), safe))
|
||||
urlquote_plus = allow_lazy(urlquote_plus, unicode)
|
||||
|
35
django/utils/http.py
Normal file
35
django/utils/http.py
Normal file
@ -0,0 +1,35 @@
|
||||
import urllib
|
||||
from django.utils.encoding import smart_str, force_unicode
|
||||
from django.utils.functional import allow_lazy
|
||||
|
||||
def urlquote(url, safe='/'):
|
||||
"""
|
||||
A version of Python's urllib.quote() function that can operate on unicode
|
||||
strings. The url is first UTF-8 encoded before quoting. The returned string
|
||||
can safely be used as part of an argument to a subsequent iri_to_uri() call
|
||||
without double-quoting occurring.
|
||||
"""
|
||||
return force_unicode(urllib.quote(smart_str(url)))
|
||||
urlquote = allow_lazy(urlquote, unicode)
|
||||
|
||||
def urlquote_plus(url, safe=''):
|
||||
"""
|
||||
A version of Python's urllib.quote_plus() function that can operate on
|
||||
unicode strings. The url is first UTF-8 encoded before quoting. The
|
||||
returned string can safely be used as part of an argument to a subsequent
|
||||
iri_to_uri() call without double-quoting occurring.
|
||||
"""
|
||||
return force_unicode(urllib.quote_plus(smart_str(url), safe))
|
||||
urlquote_plus = allow_lazy(urlquote_plus, unicode)
|
||||
|
||||
def urlencode(query, doseq=0):
|
||||
"""
|
||||
A version of Python's urllib.urlencode() function that can operate on
|
||||
unicode strings. The parameters are first case to UTF-8 encoded strings and
|
||||
then encoded as per normal.
|
||||
"""
|
||||
if hasattr(query, 'items'):
|
||||
query = query.items()
|
||||
return urllib.urlencode([(smart_str(k), smart_str(v)) for k,
|
||||
v in query], doseq)
|
||||
|
@ -149,8 +149,8 @@ a little tricky, so Django provides some assistance.
|
||||
* The function ``django.utils.encoding.iri_to_uri()`` implements the
|
||||
conversion from IRI to URI as required by `the specification`_.
|
||||
|
||||
* The functions ``django.utils.html.urlquote()`` and
|
||||
``django.utils.html.urlquote_plus()`` are versions of Python's standard
|
||||
* The functions ``django.utils.http.urlquote()`` and
|
||||
``django.utils.http.urlquote_plus()`` are versions of Python's standard
|
||||
``urllib.quote()`` and ``urllib.quote_plus()`` that work with non-ASCII
|
||||
characters (the data is converted to UTF-8 prior to encoding).
|
||||
|
||||
@ -235,7 +235,7 @@ encoding yourself. Normally, this would involve a combination of the
|
||||
example::
|
||||
|
||||
from django.utils.encoding import iri_to_uri
|
||||
from django.utils.html import urlquote
|
||||
from django.utils.http import urlquote
|
||||
|
||||
def get_absolute_url(self):
|
||||
url = u'/person/%s/?x=0&y=0' % urlquote(self.location)
|
||||
|
@ -1,3 +1,4 @@
|
||||
# coding: utf-8
|
||||
"""
|
||||
38. Testing using the Test Client
|
||||
|
||||
@ -27,11 +28,14 @@ class ClientTest(TestCase):
|
||||
|
||||
def test_get_view(self):
|
||||
"GET a view"
|
||||
response = self.client.get('/test_client/get_view/')
|
||||
# The data is ignored, but let's check it doesn't crash the system
|
||||
# anyway.
|
||||
data = {'var': u'\xf2'}
|
||||
response = self.client.get('/test_client/get_view/', data)
|
||||
|
||||
# Check some response details
|
||||
self.assertContains(response, 'This is a test')
|
||||
self.assertEqual(response.context['var'], 42)
|
||||
self.assertEqual(response.context['var'], u'\xf2')
|
||||
self.assertEqual(response.template.name, 'GET Template')
|
||||
|
||||
def test_get_post_view(self):
|
||||
@ -281,4 +285,4 @@ class ClientTest(TestCase):
|
||||
self.assertEqual(mail.outbox[1].from_email, 'from@example.com')
|
||||
self.assertEqual(mail.outbox[1].to[0], 'second@example.com')
|
||||
self.assertEqual(mail.outbox[1].to[1], 'third@example.com')
|
||||
|
||||
|
||||
|
@ -10,7 +10,7 @@ from django.shortcuts import render_to_response
|
||||
def get_view(request):
|
||||
"A simple view that expects a GET request, and returns a rendered template"
|
||||
t = Template('This is a test. {{ var }} is the value.', name='GET Template')
|
||||
c = Context({'var': 42})
|
||||
c = Context({'var': request.GET.get('var', 42)})
|
||||
|
||||
return HttpResponse(t.render(c))
|
||||
|
||||
|
@ -17,7 +17,7 @@ r"""
|
||||
friends'
|
||||
|
||||
### urlquote #############################################################
|
||||
>>> from django.utils.html import urlquote, urlquote_plus
|
||||
>>> from django.utils.http import urlquote, urlquote_plus
|
||||
>>> urlquote(u'Paris & Orl\xe9ans')
|
||||
u'Paris%20%26%20Orl%C3%A9ans'
|
||||
>>> urlquote_plus(u'Paris & Orl\xe9ans')
|
||||
|
Loading…
x
Reference in New Issue
Block a user