Question

I am using django-registration. After getting it working, I went on extending views and forms for field customization using Bootstrap.

I have this working for everything except the logout page. In the default method, I simply built the form by scratch and it worked OK. But when trying to use Django's forms, it's not showing the fields at all. Both the login and logout templates use this basic form:

<div class="auth-form">
    <form class="form-signin" method="post" action="/accounts/login/">
        {% csrf_token %}
        <h2 class="form-signin-heading">Please log in</h2>
            <div class="font-16px top-mar-1em">
                <label for="id_username"></label>
                {{ form.username }}
                <label for="id_password1"></label>
                {{ form.password }}
                <div class="form-group">
                    <div class="col-sm-10">
                        <div class="level-checkbox">
                            <label for="checkbox">
                                {{ form.remember_me }} Remember me
                            </label>
                        </div>
                    </div>
                </div>
                <button class="btn btn-primary" type="submit">Log in</button>
        </div>
    </form>
</div>

This works fine for the login page, but for the logout page it doesn't (the fields are missing). It's like the form is not available to this page. I'm sure I missed something simple but I can't figure it out.

My urls.py section look like this:

url(r'^accounts/login/$','django.contrib.auth.views.login', {
    'template_name': 'registration/login.html',
    'authentication_form': UserAuthenticationForm,
}),

I've tried adding a logout section to urls.py similar to the login one, but that hasn't worked. I've tried using logout_then_login, using login.html as import template on logout.html and other miscellaneous things to no avail.

Apparently, there is no form associated with the default logout view, so this is probably the issue but I'm not sure the best way to implement a standard form in the logout view.

Was it helpful?

Solution 2

Thanks to @tsurantino I was able to get this working using the messages framework. I always like when I have to implement a new feature using a part of Django I haven't used before. Adding to the toolkit, so to speak. Here is what I did:

Since I had a signal_connectors.py file already for sending email based on user registration/activation I just added to it:

@receiver(user_logged_out)
def user_logged_out_message(sender, request, **kwargs):
    messages.add_message(
        request,
        messages.INFO,
        'You\'ve been successfully logged out. You can log in again below.'
)

Then in my common app's __init__.py I imported it:

from common.signal_connectors import user_logged_out_message

Then in my login.html template:

{% if messages %}
    <div class="alert-success align-ctr font-16px top-mar-2em">
        {% for message in messages %}
            {{ message }}
        {% endfor %}
    </div>
{% endif %}

Finally, to redirect to login page after logout (urls.py):

url(r'^accounts/logout/$','django.contrib.auth.views.logout_then_login'),

Thanks for everyone’s help and comments.

OTHER TIPS

You should be able to add the following to your urls.py and have everything work.

url(r'^accounts/logout/$','django.contrib.auth.views.logout_then_login')

Make sure you have LOGIN_URL defined your settings.py. Then when a user clicks a logout link they will hit the logout view which takes care of redirecting to the login view which contains your form.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top