So I’m trying to create a table of hosts, and as one of the fields of that table include a clickable link to an update page. Which will pass the host_id to the update page. I was hoping someone could tell me what I’m doing wrong with regards to passing the correct parameter to upate/<host_id>. As I’m not quite sure as to how to fix the issue and make it so I can direct people via a clickable button in the table rendered to the appropriate update page. When I attempt to render the page with that added in I’m getting the following error:
NoReverseMatch at /website/home/ Reverse for 'update' with arguments '(1,)' not found. 1 pattern(s) tried: ['website/update/<host_id>'] Request Method: GET Request URL: http://127.0.0.1:8000/website/home/ Django Version: 4.0.4 Exception Type: NoReverseMatch Exception Value: Reverse for 'update' with arguments '(1,)' not found. 1 pattern(s) tried: ['website/update/<host_id>']
It is also spiecificlly pointing to the following line of code as the issue:
<td><a href="{% url 'update' beacon.host_id %}"</a>Issue Command</td>
This line is found in the following HTML segment. Relevant HTML Section
{% for beacon in hosts %} <tr> <td>{{ beacon.host_id }}</td> <td>{{ beacon.hostname }}</td> <td>{{ beacon.internalIp }}</td> <td>{{ beacon.externalIp }}</td> <td>{{ beacon.current_user }}</td> <td>{{ beacon.os }}</td> <td>{{ beacon.admin }}</td> <td><a href="{% url 'update' beacon.host_id %}"</a>Issue Command</td> </tr> {% endfor %}
My urls.py section looks like the following. urls.py
urlpatterns = [ re_path('home/', views.home), re_path('host-detailed', views.host_detailed), re_path('update/<host_id>', views.update, name='update'), ]
Relevant Models.py
class Command_Node(models.Model): host_id = models.ForeignKey(Beacon, on_delete=models.CASCADE) current_commands = models.CharField( choices=CHOICES, max_length=50, null=True) def __str__(self): return str(self.host_id)
Relevant Views.py
def home(request): hosts = Beacon.objects.all() return render (request, 'home.html', {'hosts':hosts}) def host_detailed(request): return render (request, 'hosts-detailed.html') def update(request, host_id): host_id = Command_Node.objects.get(pk=host_id) form = Command_Node(request.POST or None, instance=host_id) if form.is_valid(): form.save() return redirect('home.html') return render (request, 'update.html', {'host_id':host_id,'form':form})
Advertisement
Answer
Try something like this: Urls:
path('update/<int:pk>',views.update,name='update'),
Template
href="{% url 'update' pk = beacon.host_id %}"
As the other comment also mentioned, I don’t think re_path is the way to go