0

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.

nicorellius
  • 3,715
  • 4
  • 48
  • 79
  • What view are you using to generate the logout page? – Scott Woodall Dec 26 '13 at 17:37
  • So far, the default view... I extended the `AuthenticationView` for log in but when I pass the same view to `urls.py` it throws an error. This does feel like where I might be missing something but Im not sure how to implement it. – nicorellius Dec 26 '13 at 17:40
  • The default Django logout view does not have any forms associated with it. Am I correct in that you are wanting to log out a user and then have them redirected to the login form again? – Scott Woodall Dec 26 '13 at 17:43
  • Yes, that is correct, but to a different template, `logout.html`, so I can display message to user. I didn't realize the default view didn't have any forms associated with it. This is likely my problem, then. What is the best way to proceed? I tried passing the extended form I created for the log in view/page to the `urls.py` as an argument and it threw an error... – nicorellius Dec 26 '13 at 17:46

2 Answers2

1

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.

Scott Woodall
  • 10,456
  • 6
  • 39
  • 37
  • Thanks for the answer. Already tried this and not quite what I need (mentioned in my OP)... I want users to land on the `logout.html` page that contains the same form that is in the `login.html` file. It contains some `html` that displays a message, eg, "you've been logged out". I might be able to use the `logout_then_login` view if I can get the message to show up on the `login.html` page. – nicorellius Dec 26 '13 at 17:59
  • 1
    What message is it? Is it like "You've been logged out" ? because you could use the messages framework for that. – tsurantino Dec 26 '13 at 18:03
  • I'm still new to Django, so I'm unaware of all the cool tricks for this kind of thing. I'm sure I'm being single-minded trying to force the logout view/form idea. Perhaps using `logout_then_login` with the messages framework is a better option. Thanks. – nicorellius Dec 26 '13 at 18:32
0

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.

nicorellius
  • 3,715
  • 4
  • 48
  • 79