# Logout function

**URL:** <https://forum.djangoproject.com/t/logout-function/2710>\
**Category:** Using Django\
**Created:** [May 30, 2020, 6:29pm UTC](https://forum.djangoproject.com/t/logout-function/2710 "2020-05-30T18:29:00Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![bzo82938](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/bzo82938/32/1297_2.png) [@bzo82938](https://forum.djangoproject.com/u/bzo82938)\
**Post date:** [May 30, 2020, 6:29pm UTC](https://forum.djangoproject.com/t/logout-function/2710/1 "2020-05-30T18:29:00Z")

</div>

Hi all,

I just started learning en thus using django and want to make a small app, with the function to store notes of a Item under investigation in a case.

Up to know: I have a few pages, and a login function. Im at the point where i want to create the logout button. But i cant get it to work. The page refreshes, but i remain logged in. It doesnt re-direct me to the page i setup to redirect to. Can anyone have a look?

**my urls.py:**  
from django.conf.urls import url  
from django.contrib import admin  
from django.urls import path, re\_path  
from django.conf import settings  
from django.views.static import serve  
from django.contrib import admin  
from django.conf.urls import include, url  
from django.urls import path  
from django.contrib import admin  
from DRS import views  
from django.contrib.staticfiles.urls import staticfiles\_urlpatterns  
from django.conf.urls.static import static

urlpatterns = [  
path(‘admin/’, admin.site.urls),

```
#In home een welkom bericht, inlogmogelijkheid en een register button
path('', views.homepage, name='home'),

#De pagina waar je als gebruiker een registratie kan doorlopen
path('registratie/', views.registeren, name='registreren'),

#De pagina waar je als user op terecht komt, om een zaak aan te maken of op te vragen
path('zaakoverzicht/', views.zaak, name='overzicht'),

#De pagina waar je op detail niveau een goed kan bekijken, wijzigen, notitie kan toevoegen een een bijlage kan uploaden
path('goeddetail/', views.goederen, name='detail'),

path('uitloggen/', views.uitloggen, name='uitloggen'),

# De pagina waar je als user op terecht komt, om een zaak aan te maken of op te vragen
path('testen/', views.testen, name='testen'),

re_path('^(?P<slug>[\w-]+)/$',views.zaakdetail, name = "zaak_detail"),

```

]

urlpatterns += staticfiles\_urlpatterns()

#if settings.DEBUG:

# urlpatterns += static(settings.STATIC\_URL, document\_root=settings.STATIC\_ROOT)

urlpatterns += static(settings.MEDIA\_URL, document\_root=settings.MEDIA\_ROOT)

**My views.py:**

from django.shortcuts import render, redirect  
from django.http import HttpResponse  
from django.contrib.auth.forms import UserCreationForm, AuthenticationForm  
from .models import Zaak,GegevensdragerSoort,Gegevensdrager,Notitie,Bijlage,StatusOnderzoek  
from django.contrib.auth import login, logout, authenticate

# Create your views here.

#Homepage met welkom, login en registreer link  
def homepage(request):  
if request.method == ‘POST’:  
form = AuthenticationForm(data=request.POST)  
if form.is\_valid():  
user = form.get\_user()  
login(request,user)  
#redirect nog veranderen !!!@@@@@@@@@@@@@@@@@@@@@@@@  
return redirect(‘overzicht’)

else:  
form = AuthenticationForm()  
return render(request, ‘homepage.html’, {‘form’: form})

def registeren(request):  
if request.method == ‘POST’:  
form = UserCreationForm(request.POST)  
if form.is\_valid():  
user = form.save()  
login(request,user)  
return redirect(‘home’)

else:  
form = UserCreationForm()  
return render(request, “registeren.html”, {‘form’: form})

def uitloggen(request):  
logout(request)

def zaak(request):  
zaakoverzicht = Zaak.objects.all().order\_by(‘Created’)  
return render(request,‘zaakoverzicht.html’, {‘zaakoverzicht’:zaakoverzicht})

def zaakdetail(request,slug):  
return HttpResponse(slug)

def goederen(request):  
goeddetail = Gegevensdrager.objects.all().order\_by(‘Created’)  
return render(request,‘goeddetail.html’, { ‘goeddetail’:goeddetail} )

def detail(request):  
return HttpResponse(‘Hier kan je gedetailleerd de ingevoerde goederen bekijken en aanvullen/aanpassen’)

def testen(request):  
return render(request,“testen.html”)

---

<div class="post-metadata">

**Author:** ![bzo82938](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/bzo82938/32/1297_2.png) [@bzo82938](https://forum.djangoproject.com/u/bzo82938)\
**Post date:** [May 30, 2020, 6:30pm UTC](https://forum.djangoproject.com/t/logout-function/2710/2 "2020-05-30T18:30:04Z")

</div>

**Base.html:**  
{% load static %}

 Digitaal Registratie Systeem 

```
    <div class="wrapper">

    <nav>
        <ul>
            <li>
                <form class="logout-link" method="post">
                    {% csrf_token %}
                    <button type="submit">Uitloggen</button>
                </form>
            </li>
        </ul>

    </nav>

        {% block content %}
        {% endblock %}

    </div>

</body>

```

**This is my registratie.html**

{% extends ‘base.html’ %}  
{% load static %}

{%block content%}

# Registratie

```
<form method="post">
    {% csrf_token %}
    {{ form.as_p }}
    <input type="submit" value="Opslaan">
</form>

```

{%endblock%}

---

<div class="post-metadata">

**Author:** ![bzo82938](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/bzo82938/32/1297_2.png) [@bzo82938](https://forum.djangoproject.com/u/bzo82938)\
**Post date:** [May 30, 2020, 6:31pm UTC](https://forum.djangoproject.com/t/logout-function/2710/3 "2020-05-30T18:31:31Z")

</div>

Hopefully, it makes any sense. i could not all my code here, because i am a new member.

---

<div class="post-metadata">

**Author:** ![KenWhitesell](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/kenwhitesell/32/280_2.png) [@KenWhitesell](https://forum.djangoproject.com/u/KenWhitesell)\
**Post date:** [May 30, 2020, 6:51pm UTC](https://forum.djangoproject.com/t/logout-function/2710/4 "2020-05-30T18:51:00Z")

</div>

You haven’t associated your logout button with your view. You’ve defined a form for it, but no action attribute to specify what URL to invoke when the button is pressed.

Ken

---

<div class="post-metadata">

**Author:** ![bzo82938](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/bzo82938/32/1297_2.png) [@bzo82938](https://forum.djangoproject.com/u/bzo82938)\
**Post date:** [May 30, 2020, 7:01pm UTC](https://forum.djangoproject.com/t/logout-function/2710/5 "2020-05-30T19:01:29Z")

</div>

Hi Ken, thanks for your quick reply!

So in url.py: the path(‘uitloggen/’, views.uitloggen, name=‘uitloggen’), is correct.  
This is a logout ‘page’.

In the views.py this following functions is complete?  
def uitloggen(request):  
if request == ‘POST’:  
logout(request)  
return redirect(‘home’) (home is equal to homepage.html)

What do you mean with action? Do I have to assign the function in the ‘base html’? I have seen something like:

```
<form class="logout-link"action=??????? method="post">

```

But what to put in at ???

I apologize for the noob question, but trying to figure it out.

---

<div class="post-metadata">

**Author:** ![KenWhitesell](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/kenwhitesell/32/280_2.png) [@KenWhitesell](https://forum.djangoproject.com/u/KenWhitesell)\
**Post date:** [May 30, 2020, 7:04pm UTC](https://forum.djangoproject.com/t/logout-function/2710/6 "2020-05-30T19:04:26Z")

</div>

action is the url you want that button to use - in your case, it looks like it would be ‘/uitloggen/’.

---

<div class="post-metadata">

**Author:** ![bzo82938](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/bzo82938/32/1297_2.png) [@bzo82938](https://forum.djangoproject.com/u/bzo82938)\
**Post date:** [May 30, 2020, 7:10pm UTC](https://forum.djangoproject.com/t/logout-function/2710/7 "2020-05-30T19:10:42Z")

</div>

Oke Ken, you are awesome. I have been working on this single button for hours now… It worked 🙂

I changed it to; \<form class=“logout-link"action=”{% url ‘uitloggen’ %}" method=“post”\>
