Capture typed URL using SLUG and redirecting

I added rest of my code in extra reply’s, because i cant put them all in one post (because i am a new user).

Hi all,

I am trying to build a small web application. I have a small it running; Login function, logout, register and a few pages.
In the end its offering the following functions:

  1. Create a case (registration number, investigation name)
  2. Add items to this case (Phone, brand, serial numbers etc.)
  3. Add 0 or more notes and attachments to a single Item

Im able to capture the string what user put in the urlbox at the / level using this URL:
re_path(’^(?P[\w-]+)/$’,views.zaakdetail, name = “zaak_detail”),

But what i want to capture is the string a user typed in after /zaakoverzicht/’…’ (the … part).
Why do i want to do this? In the URL section showing all cases i want to click on the case redirecting me to the case with a Ítems’ there…

Thanks for your time,
With kind regards,

I have made a ‘test’ page in URL:
path(‘testen/’, views.testen, name=‘testen’),

I have created a test function in
def testen(request):
if request.method ==‘POST’:
form = forms.MaakZaak(request.POST)
if form.is_valid():
tijdelijk =
tijdelijk.Aangemaakt_door = request.user

     return redirect('overzicht')

form = forms.MaakZaak()
return render(request,“testen.html”,{ ‘form’:form })

I have a testen.html:

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

{% block content %}

Dit is een test met h1

Dit is een test p

<form class=“site-form"acton=”{% url ‘testen’ %}"method=“post”>
{% csrf_token %}
{{ form }}
<input type="submit"value=“Maak Aan”>

{% endblock %}

And i have created a form where a user can create a new case:

from django import forms
from . import models

class MaakZaak(forms.ModelForm):
class Meta:
model = models.Zaak
fields = [‘Registratienummer’,‘Onderzoeksnaam’, ‘Slugzaak’]

When creating a case, im showing all created cases in another page:

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

The zaakoverzicht.html:
{% extends ‘base.html’ %}
{% load static %}

{% block content %}

    <h1>Dit is het Dashboard</h1>
        <div class="zaakoverzicht">
            {%for zaak in zaakoverzicht%}
                <div class="zaak">
                <h2><a href="#">{{ zaak.Onderzoeksnaam }}</a></h2>

            {% endfor %}


{% endblock %}

First, a side note: When you’re posting code fragments here, enclose them between lines containing three “backtick” characters (```). Be sure to use the backtick `, not the apostrophe '. This helps keep the formatting making it easier to read/understand.

You might find it helpful to review the URL dispatcher documentation, along with the path and the re_path methods.

By using a name in your pattern, that portion of the url gets passed to your view as a named parameter. Without a name, it’s a positional parameter. To see examples, review part 3 of the Django tutorial.


Thanks for the sidenote, ill do that again. I have read the turtorial, but will re-do so i must have missed something.

Hi @KenWhitesell,

So i grabbed the slug, clicking the registration number. I created an extra ‘app’ in my project, called Zaak. (Translation is ‘Case’)

This is my Zaken/

def overzicht(request):
   zaakoverzicht = Zaak.objects.all().order_by('Created')
   return render(request, 'zaken/overzicht.html', {'zaakoverzicht':zaakoverzicht})

def zaak_reg(request, slug):
   return HttpResponse(slug)```

This is my Zaken/

app_name = ‘Zaken’

urlpatterns = [
path(’’, views.overzicht, name=‘overzicht’),
re_path(’^(?P[\w-]+)/$’,views.zaak_reg, name = “zaakreg”),```

This is my overzicht.html:

{% extends 'base.html' %}
{% load static %}

<!DOCTYPE html>
{% block content %}

        <h1>Dit is het zaaksoverzicht</h1>
            <div class="zaakoverzicht">
                {%for zaak in zaakoverzicht%}
                    <div class="zaak">
                        <h2><a href="{% url 'Zaken:zaakreg' slug=zaak.slug %}">{{ zaak.Registratienummer }}</a></h2>
                        <h2> {{ zaak.Onderzoeksnaam }}</h2>

                {% endfor %}

{% endblock %}```

A case has zero or more items (under investigation). When clicking the registration number i would like to get on a page, where i can see all items attached to this case.

This is my items (in dutch Gegevensdragers) class:

class Gegevensdrager(models.Model):
    Merk_Type = models.CharField(max_length=255, blank=True, null=True, verbose_name='Merk en Type')
    serienummer = models.CharField(max_length=255, blank=False)
    imei = models.CharField(max_length=15, blank=True, null=True, validators =[RegexValidator(r'^[0-9]{15}(,[0-9]{15})*$')])
    Created = models.DateTimeField(auto_now_add=True)
    gegevensdrager_soortsocode = models.ForeignKey(GegevensdragerSoort, on_delete=models.CASCADE, db_column='FK_SoortCode')
    zaakid = models.ForeignKey('Zaak', on_delete=models.CASCADE, db_column='FK_Zaakid')
    Sluggegevensdrager = models.SlugField()```

Im struggling getting these 'attached items'. I was thinking about doing something like: 

def zaak_reg(request, slug):
items = Gegevensdragers.objects.GET(???)
return render(request, #still have to create a .html to see the detailview of a case container the Items)

But how do i grab the Items (Gegevensdrager) attached to a Case?

So what you’re looking for here are the ForeignKey relationships - in this case, following relationships backwards

Given the instance of the GegevensdragerSoort for which you want to find the related Gegevensdrager objects, you can retrieve them through the ForeignKey manager. (And keep in mind that that manager is going to return a queryset, which means you would be iterating over it to retrieve all the individual instances.)


Personal note: Despite having worked for a Dutch-based company (AEGON) for over 25 years, I never learned a bit of Dutch. Thank you for the translations.

1 Like

Ha thats funny Ken, small world. I havnt figured it out, so i might come back to you tommorrow. Have a great day/evening.

With kind regards,