Django-NoReverseMatch:反向“ bha”,未找到任何参数

时间:2018-07-20 03:57:07

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

project / urls.py -在这里,我传入了用于主键的正则表达式

split

contextual / urls.py

$ perl -V
Summary of my perl5 (revision 5 version 24 subversion 1) configuration:

views.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')),

        ]))
]

well_list.html -主键在html中提供

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'),

]

contextual_main.html -html中未提供主键

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')

这是问题所在: <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> 不起作用,给我错误:

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

但是,如果我修改了 contextual_main.html 并手动传递了主键,它将起作用:

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/$']

,似乎必须再次输入主键。

当我已经在父网址中传递pk时,为什么Django让我再次传递pk?由于我已经在 contextual_main.html 的父页面 well_list.html 中传递了pk,所以我的理解是我不必再次传递它。

是否有任何方法可以解决此问题,例如使django仅从父级继承主键,还是无需重新注入主键就可以做到这一点?

1 个答案:

答案 0 :(得分:0)

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

  

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

因此,在您的情况下,您必须在pk中传递contextual_main.html才能获得解析后的html中的网址。

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

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