如何在应用程序内使用反向URL指向其他应用程序的模板?

时间:2019-06-13 10:48:36

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

我有两个应用程序:Simulation_api和building_data。每个都有模板和URL。我在主要配置中导入的网址。

在我的Simulation_api模板中,我想设置指向我的建筑数据详细视图的链接。

这就是我正在尝试的操作(在我的Simulation_api模板中),该操作引发了我一个错误(django.urls.exceptions.NoReverseMatch):

{% for item in simulations.project.buildinggroup_set.all %}
               <a href="{% url "detail_building_group" item.id %}">{{ item.description }}</a>
 {% endfor%}

for循环有效,问题出在我要反转的网址。

我正在我的building_data模型中将其反转:


class BuildingGroup(models.Model):
    name           = models.CharField(max_length=500, null=True, blank=True)
    project               = models.ForeignKey(Project, on_delete=models.CASCADE)


    def get_absolute_url(self):
        return reverse("detail_building_group", kwargs={"id" : self.id})

我认为问题是我以某种方式反转了一个应用程序中的URL,但在另一个应用程序的模板中却没有识别出该URL。但是也许还有其他东西……

有人知道吗?非常感谢!非常感谢!

不确定它是否有用,但是这里是我的简化模型:

class Simulation(models.Model):
    project                 = models.ForeignKey(Project, on_delete=models.CASCADE)


我的building_data网址

app_name = 'buildings'
urlpatterns = [
    path('list/building-group/<int:id>/', DetailBuildingGroupView.as_view(), name="detail_building_group"),

]

我的回溯:

django.urls.exceptions.NoReverseMatch
django.urls.exceptions.NoReverseMatch: Reverse for 'detail_building_group' not found. 'detail_building_group' is not a valid view function or pattern name.

Traceback (most recent call last)
File "/usr/local/lib/python3.6/site-packages/django/contrib/staticfiles/handlers.py", line 66, in __call__
return self.application(environ, start_response)
File "/usr/local/lib/python3.6/site-packages/django/core/handlers/wsgi.py", line 146, in __call__
response = self.get_response(request)
File "/usr/local/lib/python3.6/site-packages/django/core/handlers/base.py", line 81, in get_response
response = self._middleware_chain(request)
File "/usr/local/lib/python3.6/site-packages/django/core/handlers/exception.py", line 37, in inner
response = response_for_exception(request, exc)
File "/usr/local/lib/python3.6/site-packages/django/core/handlers/exception.py", line 87, in response_for_exception
response = handle_uncaught_exception(request, get_resolver(get_urlconf()), sys.exc_info())
File "/usr/local/lib/python3.6/site-packages/django/core/handlers/exception.py", line 122, in handle_uncaught_exception
return debug.technical_500_response(request, *exc_info)
File "/usr/local/lib/python3.6/site-packages/django_extensions/management/technical_response.py", line 6, in null_technical_500_response
six.reraise(exc_type, exc_value, tb)
File "/usr/local/lib/python3.6/site-packages/six.py", line 693, in reraise
raise value
File "/usr/local/lib/python3.6/site-packages/django/core/handlers/exception.py", line 35, in inner
response = get_response(request)
File "/usr/local/lib/python3.6/site-packages/django/utils/deprecation.py", line 95, in __call__
response = self.get_response(request)
File "/usr/local/lib/python3.6/site-packages/django/core/handlers/exception.py", line 37, in inner
response = response_for_exception(request, exc)
File "/usr/local/lib/python3.6/site-packages/django/core/handlers/exception.py", line 87, in response_for_exception
response = handle_uncaught_exception(request, get_resolver(get_urlconf()), sys.exc_info())
File "/usr/local/lib/python3.6/site-packages/django/core/handlers/exception.py", line 122, in handle_uncaught_exception
return debug.technical_500_response(request, *exc_info)
File "/usr/local/lib/python3.6/site-packages/django_extensions/management/technical_response.py", line 6, in null_technical_500_response
six.reraise(exc_type, exc_value, tb)
File "/usr/local/lib/python3.6/site-packages/six.py", line 693, in reraise
raise value
File "/usr/local/lib/python3.6/site-packages/django/core/handlers/exception.py", line 35, in inner
response = get_response(request)
File "/usr/local/lib/python3.6/site-packages/django/utils/deprecation.py", line 95, in __call__
response = self.get_response(request)
File "/usr/local/lib/python3.6/site-packages/django/core/handlers/exception.py", line 37, in inner
response = response_for_exception(request, exc)
File "/usr/local/lib/python3.6/site-packages/django/core/handlers/exception.py", line 87, in response_for_exception
response = handle_uncaught_exception(request, get_resolver(get_urlconf()), sys.exc_info())
File "/usr/local/lib/python3.6/site-packages/django/core/handlers/exception.py", line 122, in handle_uncaught_exception
return debug.technical_500_response(request, *exc_info)
File "/usr/local/lib/python3.6/site-packages/django_extensions/management/technical_response.py", line 6, in null_technical_500_response
six.reraise(exc_type, exc_value, tb)
File "/usr/local/lib/python3.6/site-packages/six.py", line 693, in reraise
raise value
File "/usr/local/lib/python3.6/site-packages/django/core/handlers/exception.py", line 35, in inner
response = get_response(request)
File "/usr/local/lib/python3.6/site-packages/django/utils/deprecation.py", line 95, in __call__
response = self.get_response(request)
File "/usr/local/lib/python3.6/site-packages/django/core/handlers/exception.py", line 37, in inner
response = response_for_exception(request, exc)
File "/usr/local/lib/python3.6/site-packages/django/core/handlers/exception.py", line 87, in response_for_exception
response = handle_uncaught_exception(request, get_resolver(get_urlconf()), sys.exc_info())
File "/usr/local/lib/python3.6/site-packages/django/core/handlers/exception.py", line 122, in handle_uncaught_exception
return debug.technical_500_response(request, *exc_info)
File "/usr/local/lib/python3.6/site-packages/django_extensions/management/technical_response.py", line 6, in null_technical_500_response
six.reraise(exc_type, exc_value, tb)
File "/usr/local/lib/python3.6/site-packages/six.py", line 693, in reraise
raise value
File "/usr/local/lib/python3.6/site-packages/django/core/handlers/exception.py", line 35, in inner
response = get_response(request)
File "/usr/local/lib/python3.6/site-packages/django/utils/deprecation.py", line 95, in __call__
response = self.get_response(request)
File "/usr/local/lib/python3.6/site-packages/django/core/handlers/exception.py", line 37, in inner
response = response_for_exception(request, exc)
File "/usr/local/lib/python3.6/site-packages/django/core/handlers/exception.py", line 87, in response_for_exception
response = handle_uncaught_exception(request, get_resolver(get_urlconf()), sys.exc_info())
File "/usr/local/lib/python3.6/site-packages/django/core/handlers/exception.py", line 122, in handle_uncaught_exception
return debug.technical_500_response(request, *exc_info)
File "/usr/local/lib/python3.6/site-packages/django_extensions/management/technical_response.py", line 6, in null_technical_500_response
six.reraise(exc_type, exc_value, tb)
File "/usr/local/lib/python3.6/site-packages/six.py", line 693, in reraise
raise value
File "/usr/local/lib/python3.6/site-packages/django/core/handlers/exception.py", line 35, in inner
response = get_response(request)
File "/usr/local/lib/python3.6/site-packages/django/utils/deprecation.py", line 95, in __call__
response = self.get_response(request)
File "/usr/local/lib/python3.6/site-packages/django/core/handlers/exception.py", line 37, in inner
response = response_for_exception(request, exc)
File "/usr/local/lib/python3.6/site-packages/django/core/handlers/exception.py", line 87, in response_for_exception
response = handle_uncaught_exception(request, get_resolver(get_urlconf()), sys.exc_info())
File "/usr/local/lib/python3.6/site-packages/django/core/handlers/exception.py", line 122, in handle_uncaught_exception
return debug.technical_500_response(request, *exc_info)
File "/usr/local/lib/python3.6/site-packages/django_extensions/management/technical_response.py", line 6, in null_technical_500_response
six.reraise(exc_type, exc_value, tb)
File "/usr/local/lib/python3.6/site-packages/six.py", line 693, in reraise
raise value
File "/usr/local/lib/python3.6/site-packages/django/core/handlers/exception.py", line 35, in inner
response = get_response(request)
File "/usr/local/lib/python3.6/site-packages/django/utils/deprecation.py", line 95, in __call__
response = self.get_response(request)
File "/usr/local/lib/python3.6/site-packages/django/core/handlers/exception.py", line 37, in inner
response = response_for_exception(request, exc)
File "/usr/local/lib/python3.6/site-packages/django/core/handlers/exception.py", line 87, in response_for_exception
response = handle_uncaught_exception(request, get_resolver(get_urlconf()), sys.exc_info())
File "/usr/local/lib/python3.6/site-packages/django/core/handlers/exception.py", line 122, in handle_uncaught_exception
return debug.technical_500_response(request, *exc_info)
File "/usr/local/lib/python3.6/site-packages/django_extensions/management/technical_response.py", line 6, in null_technical_500_response
six.reraise(exc_type, exc_value, tb)
File "/usr/local/lib/python3.6/site-packages/six.py", line 693, in reraise
raise value
File "/usr/local/lib/python3.6/site-packages/django/core/handlers/exception.py", line 35, in inner
response = get_response(request)
File "/usr/local/lib/python3.6/site-packages/django/utils/deprecation.py", line 95, in __call__
response = self.get_response(request)
File "/usr/local/lib/python3.6/site-packages/django/core/handlers/exception.py", line 37, in inner
response = response_for_exception(request, exc)
File "/usr/local/lib/python3.6/site-packages/django/core/handlers/exception.py", line 87, in response_for_exception
response = handle_uncaught_exception(request, get_resolver(get_urlconf()), sys.exc_info())
File "/usr/local/lib/python3.6/site-packages/django/core/handlers/exception.py", line 122, in handle_uncaught_exception
return debug.technical_500_response(request, *exc_info)
File "/usr/local/lib/python3.6/site-packages/django_extensions/management/technical_response.py", line 6, in null_technical_500_response
six.reraise(exc_type, exc_value, tb)
File "/usr/local/lib/python3.6/site-packages/six.py", line 693, in reraise
raise value
File "/usr/local/lib/python3.6/site-packages/django/core/handlers/exception.py", line 35, in inner
response = get_response(request)
File "/usr/local/lib/python3.6/site-packages/django/utils/deprecation.py", line 95, in __call__
response = self.get_response(request)
File "/usr/local/lib/python3.6/site-packages/django/core/handlers/exception.py", line 37, in inner
response = response_for_exception(request, exc)
File "/usr/local/lib/python3.6/site-packages/django/core/handlers/exception.py", line 87, in response_for_exception
response = handle_uncaught_exception(request, get_resolver(get_urlconf()), sys.exc_info())
File "/usr/local/lib/python3.6/site-packages/django/core/handlers/exception.py", line 122, in handle_uncaught_exception
return debug.technical_500_response(request, *exc_info)
File "/usr/local/lib/python3.6/site-packages/django_extensions/management/technical_response.py", line 6, in null_technical_500_response
six.reraise(exc_type, exc_value, tb)
File "/usr/local/lib/python3.6/site-packages/six.py", line 693, in reraise
raise value
File "/usr/local/lib/python3.6/site-packages/django/core/handlers/exception.py", line 35, in inner
response = get_response(request)
File "/app/analytics/middleware.py", line 22, in __call__
response = self.get_response(request)
File "/usr/local/lib/python3.6/site-packages/django/core/handlers/exception.py", line 37, in inner
response = response_for_exception(request, exc)
File "/usr/local/lib/python3.6/site-packages/django/core/handlers/exception.py", line 87, in response_for_exception
response = handle_uncaught_exception(request, get_resolver(get_urlconf()), sys.exc_info())
File "/usr/local/lib/python3.6/site-packages/django/core/handlers/exception.py", line 122, in handle_uncaught_exception
return debug.technical_500_response(request, *exc_info)
File "/usr/local/lib/python3.6/site-packages/django_extensions/management/technical_response.py", line 6, in null_technical_500_response
six.reraise(exc_type, exc_value, tb)
File "/usr/local/lib/python3.6/site-packages/six.py", line 693, in reraise
raise value
File "/usr/local/lib/python3.6/site-packages/django/core/handlers/exception.py", line 35, in inner
response = get_response(request)
File "/usr/local/lib/python3.6/site-packages/django/utils/deprecation.py", line 95, in __call__
response = self.get_response(request)
File "/usr/local/lib/python3.6/site-packages/django/core/handlers/exception.py", line 37, in inner
response = response_for_exception(request, exc)
File "/usr/local/lib/python3.6/site-packages/django/core/handlers/exception.py", line 87, in response_for_exception
response = handle_uncaught_exception(request, get_resolver(get_urlconf()), sys.exc_info())
File "/usr/local/lib/python3.6/site-packages/django/core/handlers/exception.py", line 122, in handle_uncaught_exception
return debug.technical_500_response(request, *exc_info)
File "/usr/local/lib/python3.6/site-packages/django_extensions/management/technical_response.py", line 6, in null_technical_500_response
six.reraise(exc_type, exc_value, tb)
File "/usr/local/lib/python3.6/site-packages/six.py", line 693, in reraise
raise value
File "/usr/local/lib/python3.6/site-packages/django/core/handlers/exception.py", line 35, in inner
response = get_response(request)
File "/usr/local/lib/python3.6/site-packages/django/core/handlers/base.py", line 158, in _get_response
response = self.process_exception_by_middleware(e, request)
File "/usr/local/lib/python3.6/site-packages/django/core/handlers/base.py", line 156, in _get_response
response = response.render()
File "/usr/local/lib/python3.6/site-packages/django/template/response.py", line 106, in render
self.content = self.rendered_content
File "/usr/local/lib/python3.6/site-packages/django/template/response.py", line 83, in rendered_content
content = template.render(context, self._request)
File "/usr/local/lib/python3.6/site-packages/django/template/backends/django.py", line 61, in render
return self.template.render(context)
File "/usr/local/lib/python3.6/site-packages/django/template/base.py", line 175, in render
return self._render(context)
File "/usr/local/lib/python3.6/site-packages/django/test/utils.py", line 98, in instrumented_test_render
return self.nodelist.render(context)
File "/usr/local/lib/python3.6/site-packages/django/template/base.py", line 943, in render
bit = node.render_annotated(context)
File "/usr/local/lib/python3.6/site-packages/django/template/base.py", line 910, in render_annotated
return self.render(context)
File "/usr/local/lib/python3.6/site-packages/django/template/loader_tags.py", line 155, in render
return compiled_parent._render(context)
File "/usr/local/lib/python3.6/site-packages/django/test/utils.py", line 98, in instrumented_test_render
return self.nodelist.render(context)
File "/usr/local/lib/python3.6/site-packages/django/template/base.py", line 943, in render
bit = node.render_annotated(context)
File "/usr/local/lib/python3.6/site-packages/django/template/base.py", line 910, in render_annotated
return self.render(context)
File "/usr/local/lib/python3.6/site-packages/django/template/loader_tags.py", line 67, in render
result = block.nodelist.render(context)
File "/usr/local/lib/python3.6/site-packages/django/template/base.py", line 943, in render
bit = node.render_annotated(context)
File "/usr/local/lib/python3.6/site-packages/django/template/base.py", line 910, in render_annotated
return self.render(context)
File "/usr/local/lib/python3.6/site-packages/django/template/defaulttags.py", line 211, in render
nodelist.append(node.render_annotated(context))
File "/usr/local/lib/python3.6/site-packages/django/template/base.py", line 910, in render_annotated
return self.render(context)
File "/usr/local/lib/python3.6/site-packages/django/template/defaulttags.py", line 447, in render
url = reverse(view_name, args=args, kwargs=kwargs, current_app=current_app)
File "/usr/local/lib/python3.6/site-packages/django/urls/base.py", line 90, in reverse
return iri_to_uri(resolver._reverse_with_prefix(view, prefix, *args, **kwargs))
File "/usr/local/lib/python3.6/site-packages/django/urls/resolvers.py", line 636, in _reverse_with_prefix
raise NoReverseMatch(msg)
django.urls.exceptions.NoReverseMatch: Reverse for 'detail_building_group' not found. 'detail_building_group' is not a valid view function or pattern name.

1 个答案:

答案 0 :(得分:0)

我解决了这个问题:

我在主配置中为URL添加了名称空间。

然后我像这样反转它:

    def get_absolute_url(self):
        return reverse("buildings:detail_building_group", kwargs={"id": self.id})

其中buildings是我的命名空间,detail_building_group是我的URL名称。

我还必须在URL中将我的detail-URL也添加到其他应用程序中,以便它可以识别该URL。

感谢JPG将我引向正确的方向!

这也有帮助:http://cheng.logdown.com/posts/2016/01/06/django-how-to-reverse-urls-belonged-to-other-apps