NoReverseMatch at /abampdb/get/ in django

51 Views Asked by At

i am encountering a problem

Exception Type: NoReverseMatch
Exception Value:    
Reverse for 'show-protein' not found. 'show-protein' is not a valid view function or pattern name.
<form method="GET" action="">
74          <label for="q">Search:</label>
75          <input type="text" name="q" value="{{ request.GET.q }}" placeholder="Enter search keyword">
76          <button type="submit">Search</button> {{ form.as_p }}
77  
78          <input type="text" name="q" placeholder="Search...">
79          <tbody>
80              {% for protein in protein_list %}
81  
82              <tr>
83                  <td><a href="{% url 'show-protein' %}">{{ protein }}</a></td>
84                  <td>{{ protein.weight }}</td>
85                  <td>{{ protein.hydroph }}</td>
86                  <td><input type="radio" name="name" value="{{ protein.name }}"></td>
87                  <td>{{ protein.name }}</td>
88                  {% endfor %}

urls.py

from django.contrib import admin
from django.urls import path
from .import views
from .views import *

app_name = "abampdb"
urlpatterns = [
    path('get/', views.get_search_results, name='get-list'),
    path('show_protein/<protein_id>', views.show_protein, name='show-protein'),
]

main urls.py

from django.contrib import admin
from django.urls import path
from django.urls import include, path

urlpatterns = [
    path('abampdb/', include('abampdb.urls')),
    path('admin/', admin.site.urls),
]

this is my gs109 urls.py i have included the abampdb urls i have tried to include abampdb:show-protein in url but still getting error

upd:

NoReverseMatch at /abampdb/get/
Reverse for 'show-protein' with arguments '('',)' not found. 1 pattern(s) tried: ['abampdb/show_protein/(?P<proteins_id>[^/]+)\\Z']
Request Method: GET
Request URL:    http://127.0.0.1:8000/abampdb/get/
Django Version: 4.0.5
Exception Type: NoReverseMatch
Exception Value:    
Reverse for 'show-protein' with arguments '('',)' not found. 1 pattern(s) tried: ['abampdb/show_protein/(?P<proteins_id>[^/]+)\\Z']

here is my updated template

 <form method="GET" action="">
74          <label for="q">Search:</label>
75          <input type="text" name="q" value="{{ request.GET.q }}" placeholder="Enter search keyword">
76          <button type="submit">Search</button> {{ form.as_p }}
77  
78          <input type="text" name="q" placeholder="Search...">
79          <tbody>
80              {% for protein in protein_list %}
81  
82              <tr>
83                  <td><a href="{% url 'abampdb:show-protein' protein.id %}">{{ protein }}</a></td>

my models file

class Proteins(models.Model):
    name = models.CharField(max_length=70)
    weight = models.CharField(max_length=70)
    hydroph = models.CharField(max_length=70)

my views

def show_protein(request, proteins_id):
    protein = Proteins().objects.get(pk=proteins_id)
    return render(request, 'abampdb/show_protein.html', {'protein': protein})

def get_search_results(request):

    dock_list = Docks.objects.all()
    protein_list = Proteins.objects.all()
    return render(request, 'abampdb/custom_view.html', {'dock_list': dock_list, 'protein_list': protein_list})

please see i don't know what I am doing anything wrong. i am a beginner in this.

Solved and updated the url with

{% url 'abampdb:show-protein' proteins_id=protein.pk %}

final Template

<input type="text" name="q" placeholder="Search...">
        <tbody>
            {% for protein in protein_list %} {{ protein.id }}

            <tr>
                <td><a href="{% url 'abampdb:show-protein' proteins_id=protein.pk %}">{{ protein.name }}</a></td>

                <td>{{ protein.weight }}</td>
                <td>{{ protein.hydroph }}</td>
                <td><input type="radio" name="name" value="{{ protein.name }}"></td>
                <td>{{ protein.name }}</td>```



1

There are 1 best solutions below

3
IVNSTN On

Since you set this app_name = "abampdb" urls should be referenced with namespace and argument protein_id is required as per url pattern definition:

<a href="{% url 'abampdb:show-protein' proteins_id=protein.id %}">

Example from docs