Page Not Found after much searching

Need another set of eyes on this. I’m getting a 404 and I can’t find the reason.
HTML

urlPatterns

View

Error

Can anyone see an error?

Yes, the url is expecting a parameter. You’re trying to access the name of the url as if it were the url. The actual url is of the form 'tryoutRosters/<tryoutRoster_id>', which means your request would need to be of the form http://localhost:8000/tryoutRosters/15.

Still can’t find it. I have a similar part of my site that works like what I’m trying to do with updateRoster. I listed the code back to back for comparison. Does anything jump out at you from these code snippets?

{% url 'updateTryoutRoster' tryoutRoster.id %}
{% url 'show_player' player.id %}

path('show_player/<player_id>', views.show_player, name='show_player'),
path('updateTryoutRoster/<tryoutRoster_id>', views.updateTryoutRoster, name='updateTryoutRoster'),

<a href="{% url 'show_player' player.id %}" type="button" class="btn btn-dark btn-sm">View/Update</a>
<a href="{% url 'updateTryoutRoster' tryoutRoster.id %}" type="button" class="btn btn-dark btn-sm">View/Update</a>

I get a NoReversMatch error when I add "{% url 'updateTryoutRoster' tryoutRoster.id %}" to the HTML. For what its worth, there is no error when I leave the link blank:

<a href="" type="button" class="btn btn-dark btn-sm">View/Update</a>
NoReverseMatch at /tryoutRosters/
Reverse for 'updateTryoutRoster' with arguments '('',)' not found. 1 pattern(s) tried: ['updateTryoutRoster/(?P<tryoutRoster_id>[^/]+)$']
Request Method:	GET
Request URL:	http://localhost:8000/tryoutRosters/
Django Version:	3.1.7
Exception Type:	NoReverseMatch
Exception Value:	
Reverse for 'updateTryoutRoster' with arguments '('',)' not found. 1 pattern(s) tried: ['updateTryoutRoster/(?P<tryoutRoster_id>[^/]+)$']
Exception Location:	G:\Projects\Django\MealAppContainer\venv\lib\site-packages\django\urls\resolvers.py, line 685, in _reverse_with_prefix
Python Executable:	G:\Projects\Django\MealAppContainer\venv\Scripts\python.exe
Python Version:	3.9.1
Python Path:	
['G:\\Projects\\Django\\MealAppContainer\\mealapp',
 'C:\\Python39\\python39.zip',
 'C:\\Python39\\DLLs',
 'C:\\Python39\\lib',
 'C:\\Python39',
 'G:\\Projects\\Django\\MealAppContainer\\venv',
 'G:\\Projects\\Django\\MealAppContainer\\venv\\lib\\site-packages']
Server time:	Sun, 28 Aug 2022 09:06:35 -0500

Notice the error message:

You have:

What this means is at the point you’re trying to render this url, your context either doesn’t have an entry named tryoutRoster, or the object named tryoutRoster doesn’t have an attribute named id.

Here’s screen shot of the database.


There is one record in it and its id=5, its the primary key, I believe. I believe that unless you specify otherwise, Django creates a PK and assigns the attribute ‘id’ to it, correct?

That is correct.

This doesn’t have anything to do with your model.

What are you passing to your template in your context? Are you passing something called tryoutRoster?

Items commented out for troubleshooting.

def updateTryoutRoster(request, tryoutRoster_id):
  tryoutRoster = TryoutRoster.objects.get(pk=tryoutRoster_id)
  print(tryoutRoster)
  # form = TryoutRosterForm(request.POST or None, instance=TryoutRoster)
  # if form.is_valid():
  #   form.save()
    # return redirect('camp_roster')
  return render(
    request, 'tryouts/updateTryout_Roster.html',
    {'tryoutRoster': tryoutRoster,})
    # 'form': form})

And what are you getting as a result of this test?

  • What is being printed as a result of your print statement?
  • What does your updateTryout_Roster.html look like?
    (Note: If you’re still getting the same error (NoReverseMatch) with this, we’ll need to see the entire template.)

Note: Are you sure we’re looking at the right view here? It seems to me that the problem is actually in the view that is supposed to take you to this page. It appears from your earlier post that you’re trying to generate a url for this view.

  • What is being printed as a result of your print statement?

Nothing prints, the page won’t load, the error is what I see.

  • What does your updateTryout_Roster.html look like?

Commented out the tryoutRoster for testing. Commenting in or out, no impact.

{% extends 'mealapp/base.html' %}
{% block content %}
<br>
<br>
<br>
<br>

<h1>Update Roster</h1>
  
  <!-- {{ tryoutRoster }} -->

<br/>
{% endblock %}

(Note: If you’re still getting the same error (NoReverseMatch) with this, we’ll need to see the entire template.)

What do you mean entire template?

This was a late edit to my earlier post - copying it here in case you missed it.

So the problem is not in this view - the problem is in the view that is supposed to send you to this view.

We need to look at the view that is causing the error.

Modeled from an other section I’ve created that works…
I am trying to create a button in a list of rosters that takes the user to the roster to edit it. Below is the view of the roster list.

def tryoutRoster(request):
  roster_list = TryoutRoster.objects.all()
  # order_by('lastName')
  
  return render(request, 'tryouts/tryoutRosters.html', 
  {
  'roster_list': roster_list,
  # 'total_forwards': total_forwards,
  # 'total_goaltenders': total_goaltenders,
  })

This view is working and displays the page, but the trouble comes when I start adding the link in the HTML adding the button

{% extends 'mealapp/base.html' %}
{% block content %}
<br>
<br>
<br>
<br>
<br/>

<div class="container mt-4">
  <div class="shadow p-4 mb-5 bg-body rounded">
    <h1>Rosters ({{ total_players }})</h1>
    <div class="container">
    </div>
    <table class="table table-hover table-sm">
      <thead>
        <tr class="table-dark">
          <th scope="col">Roster Name</th>
          <!-- <th scope="col">First Name</th> -->
          <th scope="col">Event Code</th>
          <!-- <th scope="col">Released</th> -->
          <!-- <th scope="col">Date Released</th> -->
          <th scope="col">Roster Info</th>
          <th scope="col"></th>
        </tr>
      </thead>
      <tbody">
        <tr>
          {% for roster in roster_list %}
          <td>{{ roster.tryoutRoster }}</td>
          <td>{{ roster.tryoutDate }}</td>
          <td>
          </td>
          <td>
            <a href="{% url 'updateTryoutRoster' tryoutRoster.id %}" type="button" class="btn btn-dark btn-sm">View/Update</a>
          </td>
        </tr>
      </tbody>
      {% endfor %}
    </table>
  </div>
  </div>

{% endblock %}

Ok, so where in that context are you supplying something called tryoutRoster?

I don’t follow. updateTryoutRoster is the view function that searches for the tryoutRoster_id passed to it from the page. The tryoutRoster_id is returned and the update_Tryouts page opens.
I guess I don’t understand the function of returning context dictionary variable tryoutRoster. It contains the id, what do I do with it in order to open the page that will allow editing of that id?

This has nothing to do with the No Reverse Match error being discussed here. There is nothing in, or associated with, updateTryoutRoster that is relevant here. Focus only on tryoutRoster.

The issue is only concerned with your tryoutRoster view, and the URL it is trying to create to access updateTryoutRoster.

You have a template being rendered in tryoutRoster.

This template has (in part):

This means that the template rendering function is going to look for something named tryoutRoster in the current context. (This is the context being used to render this template.) Once it retrieves the object referenced as tryoutRoster, the rendering engine will then try to access an attribute named id using the rules described at Templates | Django documentation | Django.

The issue is that in this view, for this template, you do not have anything in the context named tryoutRoster.

Edit: Follow-up question - which id do you want used at that point in the template?

the id I want is the id of the selected roster.
At the outside chance you might be available for a chat… I’m Discord member #3757. Totally understand if your not.

You’re jumping ahead - nothing has been selected yet.

You’re trying to render a page with links.

It looks like you’re trying to render a number of links in that template. (You’re iterating over a list - actually a queryset.)

For each entry in that list, you’re rendering some data.

For any particular entry then, which id field are you looking to use?

For any particular entry then, which id field are you looking to use?

the id of the tryoutRoster.
Here’s the example of what I’ve done previously that works.


I want to do the same thing here. Note: this page only displays because I’ve removed the link in the HTML.

<a href="" type="button" class="btn btn-dark btn-sm">View/Update</a>

But which tryout roster? Aren’t there many of them?

The button will be at the end of each roster in the list of rosters. The point is to be able to view any roster and make changes. There is only one roster in the list currently, just testing right now. Eventually there will be a dozen or so rosters. With the goal being many rosters in the regulars season. But those will be line ups after I get this working.

For clarity, you had one image in your previous reply, now you’ve got two.

Which one of these two are we talking about here?