Reputation: 358
I've written a small web application and when I want to load the page I get
Reverse for 'form_view' with arguments '('',)' not found. 1 pattern(s) tried: ['form/view/(?P[0-9]+)$']
The application was working fine. I can't figure out what went wrong. I checked for typos and naming mistakes. I didn't find anything. I can't figure out if there's something wrong with the url pattern or not. The error started after I updated the database with a new entry.
Models.py
class Form(models.Model):
name = models.CharField(max_length = 200)
publish_date = models.DateField()
book_length = models.IntegerField()
first_publish = models.BooleanField()
def __str__(self):
return self.name
def get_absolute_url(self):
return reverse('form_edit', kwargs={'pk': self.pk})
urls.py
urlpatterns = [
path('', views.FormList.as_view(), name='form_list'),
path('view/<int:pk>', views.FormView.as_view(), name='form_view'),
path('new', views.FormCreate.as_view(), name='form_new'),
path('edit/<int:pk>', views.FormUpdate.as_view(), name='form_update'),
path('delete/<int:pk>', views.FormDelete.as_view(), name='from_delete'),
]
views.py
class FormList(ListView):
model = Form
class FormView(DetailView):
model = Form
class FormCreate(CreateView):
model = Form
fields = ['name', 'publish_date', 'book_length', 'first_publish']
success_url = reverse_lazy('book_list')
class FormUpdate(UpdateView):
model = Form
fields = ['name', 'publish_date', 'book_length', 'first_publish']
success_url = reverse_lazy('book_list')
class FormDelete(DeleteView):
model = Form
success_url = reverse_lazy('book_list')
form_list.html - one of the templates where the traceback tells me I have the error at <td><a href="{% url "form_view" form.id %}">view</a></td>
<h1>Books</h1>
<table border="1">
<thead>
<tr>
<th>Name</th>
<th>Publish Date</th>
<th>Total Pages</th>
<th>First Publish Date</th>
<th>View</th>
<th>Edit</th>
<th>Delete</th>
</tr>
</thead>
<tbody>
{% for book in object_list %}
<tr>
<td>{{ form.Name }}</td>
<td>{{ form.publish_date }}</td>
<td>{{ form.book_length }}</td>
<td>{{ form.first_publish }}</td>
<td><a href="{% url "form_view" form.id %}">view</a></td>
<td><a href="{% url "form_edit" form.id %}">edit</a></td>
<td><a href="{% url "form_delete" form.id %}">delete</a></td>
</tr>
{% endfor %}
</tbody>
</table>
<a href="{% url "form_new" %}">New</a>
Traceback:
In template C:\Users\smnha\OneDrive\Desktop\CRUD\form\templates\form\form_list.html, error at line 22
Reverse for 'form_view' with arguments '('',)' not found. 1 pattern(s) tried: ['form/view/(?P<pk>[0-9]+)$']
12 <th>Delete</th>
13 </tr>
14 </thead>
15 <tbody>
16 {% for book in object_list %}
17 <tr>
18 <td>{{ form.Name }}</td>
19 <td>{{ form.publish_date }}</td>
20 <td>{{ form.book_length }}</td>
21 <td>{{ form.first_publish }}</td>
22 <td><a href="{% url "form_view" form.id %}">view</a></td>
23 <td><a href="{% url "form_edit" form.id %}">edit</a></td>
24 <td><a href="{% url "form_delete" form.id %}">delete</a></td>
25 </tr>
26 {% endfor %}
27 </tbody>
28 </table>
29
30 <a href="{% url "form_new" %}">New</a>
Upvotes: 0
Views: 55
Reputation: 7330
The one problem is you should be using book
instead of form
like this:
<tbody>
{% for book in object_list %}
<tr>
<td>{{ book.Name }}</td>
<td>{{ book.publish_date }}</td>
<td>{{ book.book_length }}</td>
<td>{{ book.first_publish }}</td>
<td><a href="{% url "form_view" book.id %}">view</a></td>
<td><a href="{% url "form_edit" book.id %}">edit</a></td>
<td><a href="{% url "form_delete" book.id %}">delete</a></td>
</tr>
{% endfor %}
</tbody>
Upvotes: 2