[Python] Using a Django generated slug URL to link to specific pages

Discussão em 'Python' iniciado por Stack, Agosto 15, 2025.

  1. Stack

    Stack Membro Participativo

    Hi generous and kind people of Stack Overflow. I'm fairly new to Python and Django, and have undertaken the project of building a portfolio for my copywriting.

    I have a main landing page that's going to have a grid of 9 different writing projects. Someone will click on one of the images for a project and then be brought over to a new page that has a more detailed description of the project.

    Each individual project page has a URL, whose path contains a slug. The code I have works in generating this path. Each writing project will have a URL path that contains projects/(slug)

    My problem is that I can't get this slug based link that will point to each individual page to work in my code. I'm getting this error: NoReverseMatch at / Reverse for 'individual_project' not found. 'individual_project' is not a valid view function or pattern name.

    Please forgive any wonkiness with this code, as I am struggling a bit in understanding it all.

    Here is my views.py:

    from django.shortcuts import render, get_object_or_404, get_list_or_404
    from .models import Project

    def individual_project_view(request, project_id):
    projectpage = get_list_or_404(Project, slug=project_id)
    return render(request, 'projects/project_detail.html', {'projectpage':projectpage})

    def project_home(request):
    portfolioprojects = get_list_or_404(Project)
    return render(request, 'projects/project_detail.html', {'portfolioprojects':portfolioprojects})


    Here is my urls.py:

    from django.urls import path, include
    from projects import views
    app_name = 'portfolio'

    urlpatterns = [path('<slug:project_id>', views.individual_project_view, name='individual_project'),]


    And finally my HTML:

    <a href="{% url 'individual_project' %}">
    <img src="{{ project.image.url }}" class="img-fluid mb-2"></a>
    {% endif %}
    <div>
    <h2 class= "category">{{ project.category }}</h2>
    <h3 class="client">{{ project.description }}</h3>
    <h4 class="project_type">{{ project.title }}</h4>
    </div>


    Thanks for reading - and I appreciate the help!

    Continue reading...

Compartilhe esta Página