JavaCake
JavaCake

Reputation: 4115

Custom password reset view class returning ImproperlyConfigured exception

I am attempting on implementing the password_reset in django.contrib.auth.views as described shown https://github.com/django/django/blob/master/django/contrib/auth/views.py#L133

But i have absolute no luck with this as i keep getting following exception:

Exception Type: ImproperlyConfigured
Exception Value: The included urlconf tutorial.urls doesn't have any patterns in it

My urls.py for the project (tutorial):

urlpatterns = patterns('',
    url(r'^adminp/', include('adminApp.urls')),
    url(r'^mobile/', include('mobileApp.urls')),
)

My urls.py for the app:

urlpatterns = patterns('',
    ....
    url(r'^login/$', UserLoginView.as_view(), name='admin_user_login'),
    url(r'^logout/$', UserLogoutView.as_view(), name='admin_user_logout'),
    url(r'^password/reset/$', UserPasswordResetView.as_view(), name='admin_password_reset'),
    url(r'^password/reset/done/$', UserPasswordResetDoneView.as_view(), name='admin_password_reset_done'),
    ....

And my class:

class UserPasswordResetView(FormView):
    template_name = 'adminApp/registration/password_reset_form.html'
    form_class = MyPasswordResetForm
    email_template_name = 'adminApp/registration/password_reset_email.html'
    subject_template_name = 'changeMe'
    post_reset_redirect = reverse('adminApp:admin_password_reset_done')

    def form_valid(self, form):
        password_reset(self.request)
        return super(UserPasswordResetView, self).form_valid(form)

Traceback:

File "/Library/Python/2.7/site-packages/django/core/handlers/base.py" in get_response
  101.                 resolver_match = resolver.resolve(request.path_info)
File "/Library/Python/2.7/site-packages/django/core/urlresolvers.py" in resolve
  318.             for pattern in self.url_patterns:
File "/Library/Python/2.7/site-packages/django/core/urlresolvers.py" in url_patterns
  346.         patterns = getattr(self.urlconf_module, "urlpatterns", self.urlconf_module)
File "/Library/Python/2.7/site-packages/django/core/urlresolvers.py" in urlconf_module
  341.             self._urlconf_module = import_module(self.urlconf_name)
File "/Library/Python/2.7/site-packages/django/utils/importlib.py" in import_module
  40.         __import__(name)
File "/Users/Jimbo/Dropbox/Sandbox/tutorial/tutorial/urls.py" in <module>
  12.     url(r'^adminp/', include('adminApp.urls')),
File "/Library/Python/2.7/site-packages/django/conf/urls/__init__.py" in include
  26.         urlconf_module = import_module(urlconf_module)
File "/Library/Python/2.7/site-packages/django/utils/importlib.py" in import_module
  40.         __import__(name)
File "/Users/Jimbo/Dropbox/Sandbox/tutorial/adminApp/urls.py" in <module>
  3. from adminApp import views
File "/Users/Jimbo/Dropbox/Sandbox/tutorial/adminApp/views.py" in <module>
  44. class UserPasswordResetView(FormView):
File "/Users/Jimbo/Dropbox/Sandbox/tutorial/adminApp/views.py" in UserPasswordResetView
  49.     post_reset_redirect = reverse('admin_password_reset_done')
File "/Library/Python/2.7/site-packages/django/core/urlresolvers.py" in reverse
  509.     return iri_to_uri(resolver._reverse_with_prefix(view, prefix, *args, **kwargs))
File "/Library/Python/2.7/site-packages/django/core/urlresolvers.py" in _reverse_with_prefix
  387.         possibilities = self.reverse_dict.getlist(lookup_view)
File "/Library/Python/2.7/site-packages/django/core/urlresolvers.py" in reverse_dict
  296.             self._populate()
File "/Library/Python/2.7/site-packages/django/core/urlresolvers.py" in _populate
  262.         for pattern in reversed(self.url_patterns):
File "/Library/Python/2.7/site-packages/django/core/urlresolvers.py" in url_patterns
  350.             raise ImproperlyConfigured("The included urlconf %s doesn't have any patterns in it" % self.urlconf_name)

Exception Type: ImproperlyConfigured at /adminp/password/reset/ Exception Value: The included urlconf tutorial.urls doesn't have any patterns in it Now if i comment out the post_reset_redirect it works fine, so i assume that its not really finding the admin_password_reset_done name? And why is that?

UPDATE:

I replaced reverse with reverse_lazy as according to this https://stackoverflow.com/a/7430924/531203, but it invoked another error which may or not be in the same domain:

Exception Type: NoReverseMatch
Exception Value: Reverse for 'password_reset_done' with arguments '()' and keyword arguments '{}' not found. 0 pattern(s) tried: []

When reverse_lazy is used it assumes that post_reset_redirect variable is none and sets: https://github.com/django/django/blob/master/django/contrib/auth/views.py#L144

Upvotes: 1

Views: 2649

Answers (1)

Kevin Christopher Henry
Kevin Christopher Henry

Reputation: 49132

Your original problem was with circular imports. reverse_lazy fixed that.

You don't seem to be using the password_reset function correctly. The function you linked to is expecting to be passed keyword arguments, it's not going to see the variables defined in your class. Also, you're ignoring the return value - you should be returning it instead.

I'm not familiar with this part of the auth system, but these appear to be regular function-based views, so I would just write my own and pass the appropriate parameters to password_reset.

def my_password_reset_view(request):
    return password_reset(request,
        template_name='adminApp/registration/password_reset_form.html',
        email_template_name='adminApp/registration/password_reset_email.html',
        subject_template_name='changeMe',
        post_reset_redirect=reverse('adminApp:admin_password_reset_done'),
        password_reset_form=MyPasswordResetForm)

And similarly with the other views.

Upvotes: 1

Related Questions