mardi 26 août 2014

Trouble logging out user in Django


Vote count:

0




I've got a function in my Django project to log out a user when a link is clicked. The problem is that when I click the link to logout, I get a 404 with the message saying "No User matches the given query". I can't seem to figure out what's going on here, I double checked the documentation and it looks like I'm doing everything right.


So far, I've found that the only way to log out is by going into the Django Admin and clicking the logout link at the top. I'm not sure why it's acting the way it is, but anyways, below is my code.


login function:



def user_login(request):
loginform = LoginForm()
new_user = NewUser()
# Initialize variables with empty string
invalid = ''
disabled = ''
# Make sure that the form was POSTed
if request.method == 'POST':
loginform = LoginForm(request.POST)
if loginform.is_valid():
# Grab the submitted values
username = request.POST['username']
password = request.POST['password']
# Attempt to grab a User object with submitted credentials
user = authenticate(username=username, password=password)
# If a User object was returned and the User is active,
# log in the user and redirect the User to their profile page
if user is not None:
if user.is_active:
login(request, user)
return HttpResponseRedirect(reverse('users:detail', args=(user.username,)))
# If the User is not active, display an error message and send them to the
# login page
else:
disabled = 'User is not active'
context = {'loginform': loginform, 'invalid': invalid, 'disabled': disabled}
return render(request, 'users/login.html', context)
# If no User object was returned, display an error message and send them to the
# login page
else:
messages.add_message(request, messages.INFO, 'The username or password you entered is incorrect. Please try again.')
context = {'loginform': loginform, 'disabled': disabled, 'new_user': new_user}
return render(request, 'users/login.html', context)
# If the form is not valid, display an error message and send them to the
# login page
else:
messages.add_message(request, messages.INFO, "In order to login, you must complete the form.")
context = {'loginform': loginform, 'disabled': disabled, 'new_user': new_user}
return render(request, 'users/login.html', context)
# If the form was not POSTed, refresh the LoginForm and send them to the
# login page
else:
loginform = LoginForm()
context = {'loginform': loginform, 'invalid': invalid, 'disabled': disabled, 'new_user': new_user}
return render(request, 'users/login.html', context)


logout function:



def user_logout(request):
logout(request)
return HttpResponseRedirect(reverse('users:index'))


urls.py:



urlpatterns = patterns('',
url(r'^$', views.index, name='index'),
url(r'^signup/$', views.new_user, name='new_user'),
url(r'^login/$', views.user_login, name='u_login'),
url(r'^browse/$', views.browse, name='browse'),
url(r'^(?P<username>\w+)/$', views.detail, name='detail'),
url(r'^(?P<username>\w+)/account/$', views.account, name='account'),
url(r'^logout/$', views.user_logout, name='u_logout'),
)


link to be clicked in order to logout:



<li><p><a href="{% url 'users:u_logout' %}" class="t-w-t">Logout</a></p></li>


I've made sure that the required settings in order to use the Django authentication system are in place. Thanks in advance for the help and please let me know if I need to post more code.



asked 1 min ago







Trouble logging out user in Django

Aucun commentaire:

Enregistrer un commentaire