Django - NoReverseMatch - 反向 'contextual' 关键字参数 '{'api': '9933124422'}' 未找到

Django - NoReverseMatch - Reverse for 'contextual' with keyword arguments '{'api': '9933124422'}' not found

Html

{% for well_instance, values in well_info %}
<tr>
  {% for value in values %}
  <td><a href="{% url 'eric_base:contextual' api=well_instance.api %}">{{ value }}</a></td>
  {% endfor %}
</tr>
{% endfor %}

views.py

class WellList_ListView(ListView):
    template_name = 'well_list.html'
    context_object_name = 'well_info'
    model = models.WellInfo

    def get_context_data(self, **kwargs):
        context = super().get_context_data(**kwargs)

        # get string representation of field names in list
        context['fields'] = [field.name for field in models.WellInfo._meta.get_fields()]

        # nested list that has all objects' all attribute values
        context['well_info_values'] = [[getattr(instance, field) for field in context['fields']] for instance in context['well_info']]

        # includes well instance objects & values string list for each well
        context['well_info'] = zip([instance for instance in context['well_info']], context['well_info_values'])

        return context

class ContextualMainView(TemplateView):
    template_name = 'contextual_main.html'

eric_base/urls.py

app_name = 'eric_base'

urlpatterns = [

        re_path(r'^(?P<api>[\w-]+)/$', base_views.ContextualMainView.as_view(), name='contextual'),

]

project/urls.py

urlpatterns = [

    path('well_list/', include([

        re_path(r'^$', views.WellList_ListView.as_view(), name='well_list'),
        re_path(r'^create/$', views.AddWell_CreateView.as_view(), name='create'),
        re_path(r'^contextual/$', include('eric_base.urls')),

        ]))

models.py

# Create your models here.
class WellInfo(models.Model):
    api = models.CharField(max_length=100, primary_key=True)
    well_name = models.CharField(max_length=100)
    status = models.CharField(max_length=100)
    phase = models.CharField(max_length=100)
    region = models.CharField(max_length=100)
    start_date = models.CharField(max_length=100)
    last_updates = models.CharField(max_length=100)

    def __str__(self):
        return self.well_name

错误信息

NoReverseMatch at /well_list/

Reverse for 'contextual' with keyword arguments '{'api': '9933124422'}' not found. 1 pattern(s) tried: ['well_list\/contextual/$(?P<api>[\w-]+)/$']

我真的不明白这有什么问题。从我得到 {'api': '9933124422'} 的非空值这一事实来看,我认为我正确地从 {% url 'eric_base:contextual' api=well_instance.api %} 注入了字典,但它仍然给我一个错误。请帮忙!

您应该将 TemplateView 更改为 DetailView,因为 TemplateView 没有 get_object 方法。例如你的情况:

class ContextualMainView(TemplateView):
    template_name = 'contextual_main.html'

至;

from django.views.generic import DetailView
from django.shortcuts import get_object_or_404

from eric_base.models import WellInfo


class ContextualMainView(DetailView):
    template_name = 'contextual_main.html'
    model = WellInfo

    def get_object(self):
        return get_object_or_404(self.model, api=self.kwargs['api'])

另请参阅此文档:


经过讨论,真正的问题来了。字符 $ 应用于最后的 url,如果您有另一个包含 urls.

,则不应使用该字符
re_path(r'^contextual/$', include('eric_base.urls'))

至;

re_path(r'^contextual/', include('eric_base.urls'))