#20547 closed Bug (duplicate)

Reverse url resolve fails when ~ (tilda) is in url prefix

Reported by: hordur@… Owned by: nobody
Component: Core (URLs) Version: 1.5
Severity: Normal Keywords:
Cc: bmispelon@… Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

Hello,

I'm trying to deploy django in my public_html directory which is served via http://servername/~username/ by mod_wsgi in apache.
However, I'm running into some errors when I have parameters to my views, such as in the admin interface:


Environment:

Request Method: GET
Request URL: https://192.168.0.2/~hordur/django/admin/sites/site/

Django Version: 1.5.1
Python Version: 2.7.3
Installed Applications:
('django.contrib.auth',
 'django.contrib.contenttypes',
 'django.contrib.sessions',
 'django.contrib.sites',
 'django.contrib.messages',
 'django.contrib.staticfiles',
 'django.contrib.admin',
 'django.contrib.admindocs',
 'django.contrib.flatpages',
 'south',
 'django_extensions',
 'debug_toolbar',
 'micawber.contrib.mcdjango',
 'hordur')
Installed Middleware:
('django.middleware.common.CommonMiddleware',
 'django.contrib.sessions.middleware.SessionMiddleware',
 'django.middleware.csrf.CsrfViewMiddleware',
 'django.contrib.auth.middleware.AuthenticationMiddleware',
 'django.contrib.messages.middleware.MessageMiddleware',
 'django.middleware.clickjacking.XFrameOptionsMiddleware',
 'django.contrib.flatpages.middleware.FlatpageFallbackMiddleware',
 'debug_toolbar.middleware.DebugToolbarMiddleware')


Template error:
In template /home/hordur/.virtualenvs/hordur_website/lib/python2.7/site-packages/django/contrib/admin/templates/admin/change_list.html, error at line 91
   a float is required
   81 :         {% endif %}


   82 :       {% endblock %}


   83 : 


   84 :       <form id="changelist-form" action="" method="post"{% if cl.formset.is_multipart %} enctype="multipart/form-data"{% endif %}>{% csrf_token %}


   85 :       {% if cl.formset %}


   86 :         <div>{{ cl.formset.management_form }}</div>


   87 :       {% endif %}


   88 : 


   89 :       {% block result_list %}


   90 :           {% if action_form and actions_on_top and cl.full_result_count %}{% admin_actions %}{% endif %}


   91 :            {% result_list cl %} 


   92 :           {% if action_form and actions_on_bottom and cl.full_result_count %}{% admin_actions %}{% endif %}


   93 :       {% endblock %}


   94 :       {% block pagination %}{% pagination cl %}{% endblock %}


   95 :       </form>


   96 :     </div>


   97 :   </div>


   98 : {% endblock %}


   99 : 

Traceback:
File "/home/hordur/.virtualenvs/hordur_website/lib/python2.7/site-packages/django/core/handlers/base.py" in get_response
  140.                     response = response.render()
File "/home/hordur/.virtualenvs/hordur_website/lib/python2.7/site-packages/django/template/response.py" in render
  105.             self.content = self.rendered_content
File "/home/hordur/.virtualenvs/hordur_website/lib/python2.7/site-packages/django/template/response.py" in rendered_content
  82.         content = template.render(context)
File "/home/hordur/.virtualenvs/hordur_website/lib/python2.7/site-packages/django/template/base.py" in render
  140.             return self._render(context)
File "/home/hordur/.virtualenvs/hordur_website/lib/python2.7/site-packages/django/test/utils.py" in instrumented_test_render
  65.     return self.nodelist.render(context)
File "/home/hordur/.virtualenvs/hordur_website/lib/python2.7/site-packages/django/template/base.py" in render
  830.                 bit = self.render_node(node, context)
File "/home/hordur/.virtualenvs/hordur_website/lib/python2.7/site-packages/django/template/debug.py" in render_node
  74.             return node.render(context)
File "/home/hordur/.virtualenvs/hordur_website/lib/python2.7/site-packages/django/template/loader_tags.py" in render
  124.         return compiled_parent._render(context)
File "/home/hordur/.virtualenvs/hordur_website/lib/python2.7/site-packages/django/test/utils.py" in instrumented_test_render
  65.     return self.nodelist.render(context)
File "/home/hordur/.virtualenvs/hordur_website/lib/python2.7/site-packages/django/template/base.py" in render
  830.                 bit = self.render_node(node, context)
File "/home/hordur/.virtualenvs/hordur_website/lib/python2.7/site-packages/django/template/debug.py" in render_node
  74.             return node.render(context)
File "/home/hordur/.virtualenvs/hordur_website/lib/python2.7/site-packages/django/template/loader_tags.py" in render
  124.         return compiled_parent._render(context)
File "/home/hordur/.virtualenvs/hordur_website/lib/python2.7/site-packages/django/test/utils.py" in instrumented_test_render
  65.     return self.nodelist.render(context)
File "/home/hordur/.virtualenvs/hordur_website/lib/python2.7/site-packages/django/template/base.py" in render
  830.                 bit = self.render_node(node, context)
File "/home/hordur/.virtualenvs/hordur_website/lib/python2.7/site-packages/django/template/debug.py" in render_node
  74.             return node.render(context)
File "/home/hordur/.virtualenvs/hordur_website/lib/python2.7/site-packages/django/template/loader_tags.py" in render
  63.             result = block.nodelist.render(context)
File "/home/hordur/.virtualenvs/hordur_website/lib/python2.7/site-packages/django/template/base.py" in render
  830.                 bit = self.render_node(node, context)
File "/home/hordur/.virtualenvs/hordur_website/lib/python2.7/site-packages/django/template/debug.py" in render_node
  74.             return node.render(context)
File "/home/hordur/.virtualenvs/hordur_website/lib/python2.7/site-packages/django/template/loader_tags.py" in render
  63.             result = block.nodelist.render(context)
File "/home/hordur/.virtualenvs/hordur_website/lib/python2.7/site-packages/django/template/base.py" in render
  830.                 bit = self.render_node(node, context)
File "/home/hordur/.virtualenvs/hordur_website/lib/python2.7/site-packages/django/template/debug.py" in render_node
  74.             return node.render(context)
File "/home/hordur/.virtualenvs/hordur_website/lib/python2.7/site-packages/django/template/base.py" in render
  1185.                     _dict = func(*resolved_args, **resolved_kwargs)
File "/home/hordur/.virtualenvs/hordur_website/lib/python2.7/site-packages/django/contrib/admin/templatetags/admin_list.py" in result_list
  286.             'results': list(results(cl))}
File "/home/hordur/.virtualenvs/hordur_website/lib/python2.7/site-packages/django/contrib/admin/templatetags/admin_list.py" in results
  264.             yield ResultList(None, items_for_result(cl, res, None))
File "/home/hordur/.virtualenvs/hordur_website/lib/python2.7/site-packages/django/contrib/admin/templatetags/admin_list.py" in __init__
  256.         super(ResultList, self).__init__(*items)
File "/home/hordur/.virtualenvs/hordur_website/lib/python2.7/site-packages/django/contrib/admin/templatetags/admin_list.py" in items_for_result
  219.             url = cl.url_for_result(result)
File "/home/hordur/.virtualenvs/hordur_website/lib/python2.7/site-packages/django/contrib/admin/views/main.py" in url_for_result
  384.                        current_app=self.model_admin.admin_site.name)
File "/home/hordur/.virtualenvs/hordur_website/lib/python2.7/site-packages/django/core/urlresolvers.py" in reverse
  496.     return iri_to_uri(resolver._reverse_with_prefix(view, prefix, *args, **kwargs))
File "/home/hordur/.virtualenvs/hordur_website/lib/python2.7/site-packages/django/core/urlresolvers.py" in _reverse_with_prefix
  391.                     candidate = (prefix_norm + result) % dict(zip(prefix_args + params, unicode_args))

Exception Type: TypeError at /admin/sites/site/
Exception Value: a float is required

What seems to be the problem is that the ~ is replaced with %7E by urlquote:
https://github.com/django/django/blob/90e530978d590a5bdcf75525aa03f844766018b8/django/core/urlresolvers.py#L384

This causes the string formatting to fail because %7E expects a float.

Following is a test case reproduces the error.

from django.core.urlresolvers import reverse

class TildaTest(TestCase):
    urls = 'mysite.urls'
    def test(self):
        self.assertEqual(reverse('test', prefix='/~test/', args=(1,)), '/~test/test/1/')

and the mysite.urls file:

from django.conf.urls import patterns, include, url

urlpatterns = patterns('',
    url(r'^test/(?P<id>\d+)/$', 'test', {}, name='test'),
)

Change History (4)

comment:1 Changed 21 months ago by bmispelon

  • Needs documentation unset
  • Needs tests unset
  • Patch needs improvement unset

Hi,

Thanks for the detailed report.

Can you check if the issue is fixed with the latest version?

It looks like a duplicate of #20022.

comment:2 Changed 21 months ago by bmispelon

  • Cc bmispelon@… added

comment:3 Changed 21 months ago by hordur@…

Yes, it is indeed fixed with the latest version from repository, sorry about that.
It is not in 1.5.1, however. Will it be included in the next 1.5. bug fix release?

comment:4 Changed 21 months ago by bmispelon

  • Resolution set to duplicate
  • Status changed from new to closed

The patch was merged into master at the time (which became 1.6), and it wasn't backported to the stable/1.5.x branch.

Note: See TracTickets for help on using tickets.
Back to Top