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
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
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?
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>/", ... )