0

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>```



  • Are you including this `urls.py` in another one? – Selcuk May 08 '23 at 08:23
  • yes from gs109 i have created another app abampdb gs109.abampdb.urls.app_name – Farha Anwer May 08 '23 at 08:24
  • Edit your question and post that `urls.py` as well. You should use the namespace in your `{% url %}` tag. – Selcuk May 08 '23 at 08:25
  • how ? can you please guide? – Farha Anwer May 08 '23 at 08:41
  • i have edited the url like that {{ protein }} but still showing error of NoReverseMatch Exception Value: Reverse for 'show-protein' not found. 'show-protein' is not a valid view function or pattern name. – Farha Anwer May 08 '23 at 08:44
  • Please read my comment again. You should edit your question to include the contents of your main `urls.py`. – Selcuk May 08 '23 at 08:53
  • i have edited my question with main urls.py – Farha Anwer May 08 '23 at 09:19
  • There is something wrong with the whole setup, perhaps you have multiple files with the same name in different locations which enable _magic_ failures. The last error says it tried to match url pattern with parameter `proteins_id` however shown urls.py states `` - without "s". Try to clean up project folder contents. Also try switching here `{% url 'abampdb:show-protein' protein.id %}` from `protein.id` to `protein.pk`, also it worth a try to switch to kwards: `{% url 'abampdb:show-protein' proteins_id=protein.pk %}`. – Ivan Starostin May 08 '23 at 12:16
  • And put some additional code for debugging purpose into this template: print out what is protein, protein.id, protein.pk. – Ivan Starostin May 08 '23 at 12:17
  • Thankyou resolved my error with {% url 'abampdb:show-protein' proteins_id=protein.pk %} – Farha Anwer May 08 '23 at 13:32

1 Answers1

0

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

Ivan Starostin
  • 8,798
  • 5
  • 21
  • 39
  • i have added but still showing error Exception Type: NoReverseMatch Exception Value: Reverse for 'show-protein' with arguments '('',)' not found. 1 pattern(s) tried: ['abampdb/show_protein/(?P[^/]+)\\Z'] – Farha Anwer May 08 '23 at 11:02
  • At least url resolver falls into correct url pattern now. But `protein` is null or `protein.id` is empty. Are you sure that all url reversions are inside `{% for protein in protein_list %}`? Please update your question with modified code and current error traceback (make additional `upd: ` block in the question). – Ivan Starostin May 08 '23 at 11:07
  • updated the answer with final version – Ivan Starostin May 08 '23 at 14:31