Tio
Tio

Reputation: 1036

django:Reverse for 'detail' with no arguments not found. 1 pattern(s) tried: ['detail/(?P<pk>[0-9]+)/$']

I am trying to develop a web system in python3 and Django using the local environment. when I try to use a DetailView and add pk in URL, I got stuck and I don't know why it's showing this error. I searched a lot and can't find an answer.

my condition
Mac: mojave 10.14.6
Python: 3.7.5
Django: 2.2.2

error message

Reverse for 'detail' with no arguments not found. 1 pattern(s) tried: ['detail/(?P<pk>[0-9]+)/$']

views.py

from django.contrib.auth.mixins import LoginRequiredMixin
from django.urls import reverse_lazy
from django.views import generic

class HomeView(LoginRequiredMixin, generic.ListView):
        model = DbVegetableInfo
        template_name = 'home.html'

        def get_queryset(self):
            queryset = DbVegetableInfo.objects.filter(user=self.request.user).order_by('-created_at')
            return queryset


class DetailView(LoginRequiredMixin, generic.DetailView):

        model = DbVegetableInfo
        template_name = 'detail.html'

urls.py

from django.urls import path
from . import views

app_name = 'main'
urlpatterns = [
    path('home/', views.HomeView.as_view(), name='home'),
    path('detail/<int:pk>/', views.DetailView.as_view(), name='detail'),
]

home.html(ListView)

    {% extends "layout/layout_home.html" %}

    {% block javascript %}
    {% endblock %}

    {% block title %}
    table
    {% endblock %}

    {% block content %}

    <!-- page content -->
    <div class="right_col" role="main">
        <div class="">
            <div class="page-title">
                <div class="title_left">
                    <h3>test</h3>
                </div>

                <div class="title_right">
                    <div class="col-md-5 col-sm-5 col-xs-12 form-group pull-right top_search">
                        <div class="input-group">
                            <input type="text" class="form-control" placeholder="Search for...">
                            <span class="input-group-btn">
                          <button class="btn btn-secondary" type="button">Go!</button>
                        </span>
                        </div>
                    </div>
                </div>
            </div>

            <div class="clearfix"></div>

            <div class="row">
                <div class="col-md-8 offset-md-2">

                    {% if messages %}
                    <div class="container">
                        <div class="row">
                            <div class="my-div-style w-100">
                                <ul class="messages" style="list-style: none;">
                                    {% for message in messages %}
                                    <li {% if message.tags %} class="{{ message.tags }}" {% endif %}>
                                        {{ message }}
                                    </li>
                                    {% endfor %}
                                </ul>
                            </div>
                        </div>
                    </div>
                    {% endif %}


                    <div class="x_panel">
                        <div class="x_title">
                            <h2>test</h2>
                            <ul class="nav navbar-right panel_toolbox">
                                <li><a class="collapse-link"><i class="fa fa-chevron-up"></i></a>
                                </li>
                                <li class="dropdown">
                                    <a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button"
                                       aria-expanded="false"><i class="fa fa-wrench"></i></a>
                                    <div class="dropdown-menu" aria-labelledby="dropdownMenuButton">
                                        <a class="dropdown-item" href="#">Settings 1</a>
                                        <a class="dropdown-item" href="#">Settings 2</a>
                                    </div>
                                </li>
                                <li><a class="close-link"><i class="fa fa-close"></i></a>
                                </li>
                            </ul>
                            <div class="clearfix"></div>
                        </div>

                        <div class="x_content">

                            <table class="table table-striped projects" style="text-align: center">
                                <thead>
                                <tr>
                                    <th style="width: 1%">#</th>
                                    <th>test</th>
                                    <th>test</th>
                                    <th>test</th>
                                    <th>test</th>

                                    <th>test</th>
                                    <th>test</th>
                                    <th>test</th>
                                    <th>test</th>
                                    <th>test</th>
                                </tr>
                                </thead>
                                <tbody>

                                {% for vegetable_info in object_list %}

                                <tr style="font-size: 14px">

                                    <td>
                                        <p> {{ forloop.counter }}</p>
                                    </td>
                                    <td>
                                        <p> {{ vegetable_info.weather_observation }}</p>
                                    </td>
                                    <td>
                                        <p> {{ vegetable_info.field_name }}</p>
                                    </td>
                                    <td>
                                        <p>{{ vegetable_info.plant_name }} </p>
                                    </td>

                                    <td>
                                        <p>{{ vegetable_info.variety_name }} </p>
                                    </td>
                                    <td>
                                        <p> {{ vegetable_info.start_date }}</p>

                                    </td>

                                    <td class="project_progress">
                                        <div class="progress progress_sm">
                                            <div class="progress-bar bg-green" role="progressbar"
                                                 data-transitiongoal=""></div>
                                        </div>
                                        <small>%</small>
                                    </td>
                                    <td>
                                        <button type="button" style="pointer-events: none"
                                                class="btn btn-success btn-sm ">test
                                        </button>
                                    </td>
                                    <td>
                                        <p></p>
                                    </td>

                                    <td>
                                        <a href="{% url 'main:detail' object.pk %}"
                                           class="btn btn-primary btn-sm"><i class="fa fa-folder"></i>
                                            detail
                                        </a>

                                    </td>

                                </tr>
                                {% empty %}
                                    <p>test</p>
                                {% endfor %}
                                </tbody>

                            </table>
                        </div>
                    </div>

                </div>

            </div>
        </div>
    </div>

{% endblock %}

*I've already tryied this case but it has been still not solved... Reverse for 'edit' with no arguments not found. 1 pattern(s) tried: ['articles/edit/(?P<pk>[0-9]+)/$']

Upvotes: 1

Views: 2862

Answers (2)

Tio
Tio

Reputation: 1036

I could remove the error. the reason why I had the error was simple, I had another html file which has a href tag to DetailViewclass() and I fixed it, after then the error was removed and it worked well.

Upvotes: 0

bmons
bmons

Reputation: 3392

Ok, either in your home.html you can see the instance of the view as follows

{% for object in object_list %}
<a href="{% url 'main:detail' object.pk %}"{{ object.title }}</a>
{% endfor %}

or you can create a seperate list view and its template

Upvotes: 3

Related Questions