Opened 12 years ago

Closed 12 years ago

#19595 closed Bug (invalid)

The 'redirect' from django.shortcuts does not work in a view function when the only parameter is a URL string

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

Description

The fragment of urls.py:

urlpatterns = patterns('',
    # Examples:
	url(r'^$', 'home.views.home', name='home'),
        url(r'^viewer', 'home.views.viewer', name='viewer'),
    ...
    # Uncomment the next line to enable the admin:
    # url(r'^admin/', include(admin.site.urls)),
)

The home/views.py:

...
def viewer( request ):	

	from django.shortcuts import redirect
	return redirect( '/' )
...

I tested the redirect in another similar project, in both cases the traceback is similar:

File "C:\Python27\lib\site-packages\django\core\handlers\base.py" in get_response
  115.                         response = callback(request, *callback_args, **callback_kwargs)
File "C:\cygwin\home\sergzach\lads\lads\views.py" in response
  14. 		request, result = view( *args, **kwargs )
File "C:\cygwin\home\sergzach\lads\home\views.py" in viewer
  106. 	return redirect( '/' )
File "C:\Python27\lib\site-packages\django\shortcuts\__init__.py" in redirect
  70.     return redirect_class(resolve_url(to, *args, **kwargs))
File "C:\Python27\lib\site-packages\django\shortcuts\__init__.py" in resolve_url
  143.         return urlresolvers.reverse(to, args=args, kwargs=kwargs)
File "C:\Python27\lib\site-packages\django\core\urlresolvers.py" in reverse
  496.     return iri_to_uri(resolver._reverse_with_prefix(view, prefix, *args, **kwargs))
File "C:\Python27\lib\site-packages\django\core\urlresolvers.py" in _reverse_with_prefix
  382.         possibilities = self.reverse_dict.getlist(lookup_view)
File "C:\Python27\lib\site-packages\django\core\urlresolvers.py" in reverse_dict
  297.             self._populate()
File "C:\Python27\lib\site-packages\django\core\urlresolvers.py" in _populate
  286.                 lookups.appendlist(pattern.callback, (bits, p_pattern, pattern.default_args))
File "C:\Python27\lib\site-packages\django\core\urlresolvers.py" in callback
  230.         self._callback = get_callable(self._callback_str)
File "C:\Python27\lib\site-packages\django\utils\functional.py" in wrapper
  29.         result = func(*args)
File "C:\Python27\lib\site-packages\django\core\urlresolvers.py" in get_callable
  92.         mod_name, func_name = get_mod_func(lookup_view)
File "C:\Python27\lib\site-packages\django\core\urlresolvers.py" in get_mod_func
  142.         dot = callback.rindex('.')

Exception Type: AttributeError at /viewer/
Exception Value: 'TemplateView' object has no attribute 'rindex'

The function worked in Django-1.3 (I updated from Django-1.3 or some release of Django-1.4). In current releases Django-1.4.3 and Django-1.5c1 it does not work as expected.

[The docs](https://docs.djangoproject.com/en/1.5/topics/http/shortcuts/#django.shortcuts.redirect) says we could pass the string URL as the first parameter.

Change History (4)

comment:1 by Claude Paroz, 12 years ago

I'm quite sure your problem is not related to the redirect function, but to something wrong in your urls definition. The traceback shows that the error is occurring in RegexURLResolver._populate(). Could you post your entire urls definitions for the failing project?

comment:2 by sergzach, 12 years ago

The main lads/urls.py:

from django.conf.urls import patterns, include, url
from django.views.generic import TemplateView

# Uncomment the next two lines to enable the admin:
# from django.contrib import admin
# admin.autodiscover()

urlpatterns = patterns('',
    # Examples:
	url(r'^$', 'home.views.home', name='home'),
    url(r'^viewer', 'home.views.viewer', name='viewer'),
    url(r'^lazy-load', 'home.views.viewer_lazy_load'),
	url( r'^delete-image', 'home.views.delete_image' ),
    url( r'^photo-log', 'home.views.photo_log' ),
    url( r'^unhandled', 'home.views.unhandled_lad_profile' ),
    url( r'^proxy', 'home.views.proxy' ),
	url( r'^how-many', 'home.views.how_many' ),
    url( r'^get-photos', 'home.views.get_photos' ),
    url( r'^all-lads', 'home.views.all_lads' ),
    url( r'^add-lad', 'home.views.add_lad' ),
    url( r'^filter-lads', 'home.views.filter_lads' ),
    url( r'^save-thumbnail', 'home.views.save_thumbnail' ),
    url( r'^google001d510d0966bd9a.html', TemplateView( template_name = "home/google001d510d0966bd9a.html" ) ),
    url( r'^yandex_6af1bdaeb51f904e.html', TemplateView( template_name = "home/yandex_6af1bdaeb51f904e.html" ) ),
	url( r'^robots.txt', TemplateView( template_name = "home/empty.html" ) ),
	#url( r'^myauth', include( 'myauth.urls' ) ),
	url( r'^myauth', 'myauth.views.vk_view' ),
	url( r'^([\w\-]+)', 'home.views.lad_profile' ),
    # url(r'^lads/', include('lads.foo.urls')),

    # Uncomment the admin/doc line below to enable admin documentation:
    # url(r'^admin/doc/', include('django.contrib.admindocs.urls')),

    # Uncomment the next line to enable the admin:
    # url(r'^admin/', include(admin.site.urls)),
)

There is also an urls.py file in myauth, but it is not included in the main file:

from django.conf.urls import patterns, url
from views import vk_view

# Uncomment the next two lines to enable the admin:
# from django.contrib import admin
# admin.autodiscover()

urlpatterns = patterns('',
    # Examples:
	url(r'^vk', vk_view ),
)



"""from django.conf.urls.defaults import *
from orders.views import OrdersView, OrderDetailView

from django.contrib.auth.decorators import login_required

urlpatterns = patterns(	'',
			url( r'(\d+)', login_required( OrderDetailView.as_view() ) ),
			url( r'(\w)', login_required( OrdersView.as_view() ) ),
			)"""

    # Uncomment the next line to enable the admin:
    # url(r'^admin/', include(admin.site.urls)),
)

Thank you.

comment:3 by sergzach, 12 years ago

Yes, I see the problem. I used TemplateView( template_name = ... ) instead of TemplateView.as_view( template_name = ... ).

comment:4 by Simon Charette, 12 years ago

Resolution: invalid
Status: newclosed
Note: See TracTickets for help on using tickets.
Back to Top