1
votes

In a Django project, my homepage view is as follows:

@login_required
def home(request):
    return render(request, 'home.html')

So that when someone tries to access the homepage, they're automatically taken to a login form if no one is logged in. Here's that form, straight from the Django docs:

{% extends "base.html" %}

{% block content %}

{% if form.errors %}
<p>Your username and password didn't match. Please try again.</p>
{% endif %}

<form method="post" action="{% url django.contrib.auth.views.login %}">
{% csrf_token %}
<table>
<tr>
    <td>{{ form.username.label_tag }}</td>
    <td>{{ form.username }}</td>
</tr>
<tr>
    <td>{{ form.password.label_tag }}</td>
    <td>{{ form.password }}</td>
</tr>
</table>

<input type="submit" value="login" />
<input type="hidden" name="next" value="{{ next }}" />
</form>

{% endblock %}

Once someone logs into the form, they get taken to the homepage which looks like this:

{% extends "base.html" %}

{% block content %}

<form method="post" action="{% url django.contrib.auth.views.logout_then_login %}">
{% csrf_token %}
<input type="submit" value="logout" />
</form>

<h1>Home Page</h1>

{% endblock %}

As you can see, I've attempted to create a logout button that will take the user right back to the login page. However, I noticed that when first arriving at the login page, the URL ends in ?next=/, whereas once I "logout", the "login" page I'm taken to has a URL lacking ?next=/. And when I try to login using that page, I'm sent to the URL /accounts/profile/ (instead of the proper homepage URL), which doesn't exist. I'm guessing I've done something wrong in urls.py, but I'm not sure what:

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

What am I missing here?

1

1 Answers

3
votes

You should tell Django where it should redirect visitors when it doesn't receive a next parameter. This is done with a LOGIN_REDIRECT_URL settings, as explained in the Django documentation.