【问题标题】:Django - NoReverseMatch - Reverse for 'contextual' with keyword arguments '{'api': '9933124422'}' not foundDjango - NoReverseMatch - 没有找到关键字参数'{'api':'9933124422'}'的'contextual'的反向
【发布时间】:2018-12-28 04:18:54
【问题描述】:

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 %} 注入了字典,但它仍然给我一个错误。请帮忙!

【问题讨论】:

  • 让我们看看你的ContextualMainView
  • @SancaKembang 抱歉,感谢您的指出。我刚刚添加了它

标签: django django-models django-templates django-views django-urls


【解决方案1】:

您应该将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,如果您有另一个包含 url,则不应使用。

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

到;

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

【讨论】:

  • 我不知道出了什么问题,但我仍然遇到完全相同的错误。我改为详细说明你并复制了你的 get_object 方法,但它仍然不起作用
  • 调试页面说这行有问题:&lt;td&gt;&lt;a href="{% url 'eric_base:contextual' api=well_instance.api %}"&gt;{{ value }}&lt;/a&gt;&lt;/td&gt;
  • 我认为你的问题在这种情况下 well_list\\/contextual/$(?P&lt;api&gt;[\\w-]+)/$ ,让我看看你的 urls.py well_list/contextual
  • app urls 实际上是项目 url 的扩展,即 well_list/contextual。我更新了代码
  • omg 非常感谢,非常喜欢。有用!!我真的不能为此感谢你。非常感谢您的帮助 omg
猜你喜欢
  • 2018-12-28
  • 2023-03-15
  • 2018-12-28
  • 2018-09-08
  • 2013-07-25
  • 2013-12-22
  • 2011-10-01
  • 2016-11-11
  • 2017-02-26
相关资源
最近更新 更多