我正在尝试使用本地环境在python3和Django中开发Web系统。 当我尝试使用DetailView并在URL中添加pk时,我被卡住了,我不知道为什么它显示此错误。我搜索了很多,却找不到答案。
我的状况
Mac:mojave 10.14.6
Python:3.7.5
Django:2.2.2
错误消息
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 %}
*我已经尝试过这种情况,但仍未解决... Reverse for 'edit' with no arguments not found. 1 pattern(s) tried: ['articles/edit/(?P<pk>[0-9]+)/$']
答案 0 :(得分:0)
好吧,您可以在home.html中看到如下视图的实例
{% for object in object_list %}
<a href="{% url 'main:detail' object.pk %}"{{ object.title }}</a>
{% endfor %}
或者您可以创建一个单独的列表视图及其模板
答案 1 :(得分:0)
我可以删除该错误。 我发生该错误的原因很简单,我有另一个html文件,该文件的DetailViewclass()具有href标记,并对其进行了修复,然后将该错误消除并运行良好。