Logout function

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”)

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%}

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

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

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.

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

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

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