【问题标题】:Django - NoReverseMatch: Reverse for 'bha' with no arguments not foundDjango - NoReverseMatch:“bha”的反向没有找不到参数
【发布时间】:2018-12-28 07:29:08
【问题描述】:

project/urls.py - 在这里,我为主键传递了正则表达式

from django.urls import path, re_path, include
from contextual import views

urlpatterns = [
    url('admin/', admin.site.urls),
    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'^(?P<pk>[-\w]+)/contextual/', include('contextual.urls')),

        ]))
]

contextual/urls.py

app_name = 'contextual'

urlpatterns = [

    re_path(r'^$', base_views.ContextualMainView.as_view(), name='main'),
    re_path(r'^bha/$', base_views.BHA_UpdateView.as_view(), name='bha'),

]

views.py

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


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_zipped'] = zip([instance for instance in context['well_info']], context['well_info_values'])

        return context

class BHA_UpdateView(UpdateView):
    template_name = 'contextual_BHA.html'
    model = models.WellInfo
    fields = '__all__'
    success_url = reverse_lazy('well_list')

well_list.html - 主键在 html 中提供

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

contextual_main.html - html 中不提供主键

<button type="button" class="btn btn-default" data-container="body" data-toggle="popover">
  <a href="{% url 'contextual:bha' %}">BHA</a>
</button>

这就是问题所在: http://127.0.0.1:8000/well_list/123412-11-33/contextual/ 不起作用并给我错误:

NoReverseMatch at /well_list/123412-11-33/contextual/
Reverse for 'bha' with no arguments not found. 1 pattern(s) tried: ['well_list\\/(?P<pk>[-\\w]+)/contextual/bha/$']

但是,如果我修改我的 contextual_main.html,并手动传入主键,它会起作用:

<button type="button" class="btn btn-default" data-container="body" data-toggle="popover">
  <a href="{% url 'contextual:bha' pk='123412-11-33' %}">BHA</a>
</button>

如果我想访问http://127.0.0.1:8000/well_list/123412-11-33/contextual/bha/,看来我必须再次传入主键

当我已经在父 url 中传递了 pk 时,为什么 Django 让我再次传递 pk?由于我已经在well_list.html中传入了pk,这是contextual_main.html的父页面,我的理解是不用再传一遍.

有什么办法可以解决这个问题,比如让 django 只从父级继承主键,或者在不重新注入主键的情况下这样做?

【问题讨论】:

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


    【解决方案1】:

    url 模板标签不考虑上下文中的当前路径。它使用 django 的 reverse 方法从给定的参数创建 url。 引用 url templatetag 文档的第一行。

    返回与给定视图和可选参数匹配的绝对路径引用(没有域名的 URL)。

    因此,在您的情况下,您必须在 contextual_main.html 中传递 pk 才能解析 html 中的 url。

    查看文档了解更多详情。 https://docs.djangoproject.com/en/2.0/ref/templates/builtins/#url

    查看reverse 的文档以获取更多详细信息。 https://docs.djangoproject.com/en/2.0/ref/urlresolvers/#reverse

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-10-05
      • 2018-12-28
      • 1970-01-01
      • 1970-01-01
      • 2021-03-01
      • 2021-03-29
      • 2023-03-15
      • 1970-01-01
      相关资源
      最近更新 更多