Looking For Help Please

Hi, I’m very new to Django and have been building a personal training web app (to be a PWA)

Im trying to get hit a URL and display from a model, but it seems to be resolving a different URL and causing an error as its looking for a pk in the URL.

Just wanted to know what im missing as im sure its really simple but been trying for a while

urls.py

from django.urls import path
from exercises import views

urlpatterns = [
    path("", views.exercise_index, name="exercise_index"),
    path("<str:pk>/", views.exercise_detail, name="exercise_detail"),
    path("workouts/", views.workout, name="workouts"),
]

views.py

from django.shortcuts import render
from exercises.models import Exercise, Complex, ExerciseComplex

def exercise_index(request):
    exercises = Exercise.objects.all()
    context = {
        "exercises": exercises
    }
    return render(request, "exercises/exercise_index.html", context)

def exercise_detail(request, pk):
    exercise = Exercise.objects.get(pk=pk)
    context = {
        "exercise": exercise
    }
    return render(request, "exercises/exercise_detail.html", context)

def workout(request):
    workouts = ExerciseComplex.objects.filter(Complex__Active='Day 1')
    context = {
        "workouts": workouts
    }
    return render(request, "exercises/workouts.html", context)

Files:

image

Error:

# DoesNotExist at /exercises/workouts/

Exercise matching query does not exist.

thanks
Rob

Side note: When posting code here, enclose the code between lines of three backtick - ` characters. This means you’ll have a line of ```, then your code, then another line of ```. This forces the forum software to keep your code properly formatted. (I’ve taken the liberty of fixing your original post for you.)

(Also please try to avoid posting images of text data - it’s a lot more useful to post the actual text.)

Can you explain in more detail what you mean by this? What specifically do you me by “trying to get hit a URL”?

If you’re talking about accessing a link on a page, please post the HTML / template with the url being referenced.

Hi Ken, thanks for the response and thanks for letting me know how to post properly on here (was my first post)

So in the code above I have 3 URLS, which resolve to the 3 Views.

When I try to go to exercises/workouts.html I’m receiving an error “Exercise matching query does not exist.” which I’m my traceback shows it calling the “exercise_detail” view.

My html looks like -


{% block page_content %}
{% if user.is_authenticated %}
<section>
<div class="container">
    <h1 class="h3 mb-3 fw-light mt-3">WORKOUTS</h1>
    {% for x in workouts %}
        {{x.name }}
    {% endfor %}

</div>

{% else %}
{% endif %}
{% endblock %}

Traceback error (using pythonanywhere)

Request Method:	GET
Request URL:	https://laingo93.pythonanywhere.com/exercises/workout/
Django Version:	5.0.3
Exception Type:	DoesNotExist
Exception Value:	
Exercise matching query does not exist.
Exception Location:	/home/laingo93/.virtualenvs/venv/lib/python3.10/site-packages/django/db/models/query.py, line 649, in get
Raised during:	exercises.views.exercise_detail

/home/laingo93/rp-endorfit/exercises/views.py, line 22, in exercise_detail
    exercise = Exercise.objects.get(pk=pk) …
Local vars
Variable	Value
pk	
'workout'
request	
<WSGIRequest: GET '/exercises/workout/'>

So im just trying to reach the URL and then display from the view but I’m not able to

Thanks
Rob

Hi Ken,

Ive been able to get this to work by creating a new folder in the exercises folder… and updating my files to look like

views.py

from django.shortcuts import render
from exercises.models import Exercise, Complex, ExerciseComplex
from django.http import JsonResponse

def exercise_index(request):
    exercises = Exercise.objects.all()
    context = {
        "exercises": exercises
    }
    return render(request, "exercises/exercise_index.html", context)

def workout(request):
    #workouts = ExerciseComplex.objects.filter(Complex__Active='Day 1')
    workouts = Complex.objects.all()
    context = {
        "workouts": workouts
    }
    return render(request, "workout/workout.html", context)


def exercise_detail(request, pk):
    exercise = Exercise.objects.get(pk=pk)
    context = {
        "exercise": exercise
    }
    return render(request, "exercises/exercise_detail.html", context)

urls.py

from django.urls import path
from exercises import views

urlpatterns = [
    path("", views.exercise_index, name="exercise_index"),
    path("<str:pk>/", views.exercise_detail, name="exercise_detail"),
    path('workout/workout', views.workout, name='workout'),
]

I guess using the base URL meant it was still trying to resolve the exercise_detail page and look for a primary key? so putting it in a new folder mean it was completely different?

Thanks
Rob

Ok, the error you posted makes it clear here.

The Django url dispatcher searches through URLs in order, dispatching to the first entry that matches.

You have:

You will never get to the workouts url, because the string "workouts/" is going to match "<str:pk>/" and so a request to the url "workouts/" will end up dispatching to exercise_detail instead of workout.

You didn’t need to move any files at all. Your url structure does not need to have any relationship with your project’s file and directory structure. (That’s the reason why there’s a urls.py file - to separate those concerns.)

The quick fix would have been to move the pk url after the workout url.
<opinion> The more comprehensive fix would be to rethink how you’re assigning urls to avoid this. (e.g., make the url for the details to be path("detail/<str:pk>/", ... )

Makes complete sense thank you for the response Ken and sorry for the beginner questions that have probably been answered 100s of times .