简体   繁体   中英

Django LoginView doesn't override attributes

I'm trying to override attributes in my Django application and for some reason I cannot override some attributes of the LoginView. I've tried to do that both as my own class based view and directly on the urlconfig, passing the attributes as arguments to the as_view function, nothing worked.

To be more specific, i'm trying to override the template_name but it seems like Django ignores my argument and still trying to look to 'registration/login.html', even though i've passed another path of my own HTML template in another directory. What am I doing wrong?

Here is my code, or how I tried to use it:

url(r'^accounts/logout/$', auth_views.LogoutView.as_view(template_name='accounts/login.html')),

or

class SigninView(LoginView):
template_name = 'login.html'

def get(request):
    form = LoginForm
    return render(request, self.template_name, {'form':form})

I've already tried to change the name (or the path) of my template name, both as the as_view function or on my created class based view, but Django looks always for this specific path - '/registration/login.html' - basically to the origin path. Therefore I assumed that Django doesn't recognise for some reason my overriding and looks for the original attributes as it's set in the LoginView.

The error:

    TemplateDoesNotExist at /accounts/login/
registration/login.html
Request Method: GET
Request URL:    http://localhost:8000/accounts/login/
Django Version: 1.11.6
Exception Type: TemplateDoesNotExist
Exception Value:    
registration/login.html
    Request Method: GET
Request URL:    http://localhost:8000/accounts/login/...

Template-loader postmortem
Django tried loading these templates, in this order:
Using engine django:
django.template.loaders.filesystem.Loader: /Users/Roi/Desktop/Programming/mysite/home/templates/registration/login.html (Source does not exist)
django.template.loaders.app_directories.Loader: /Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/django/contrib/admin/templates/registration/login.html (Source does not exist)
django.template.loaders.app_directories.Loader: /Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/django/contrib/auth/templates/registration/login.html (Source does not exist)
django.template.loaders.app_directories.Loader: /Users/Roi/Desktop/Programming/mysite/home/templates/registration/login.html (Source does not exist)

I've also added another path in my settings, as you can see, but django just add 'registration/login.html', so again, I can tell it's just using the original attributes no matter what...

Any ideas?

You inherit 'LoginView' in 'views.py', But in 'urls.py' you mentioned 'LogoutView'. You may not have shown us the full 'urls.py' code. Maybe you mentioned 'LoginView' instead of 'SigninView'. So 'urls.py' uses 'LoginView' to render the login page.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM