【问题标题】:Cannot reverse url in template Django无法在模板 Django 中反转 url
【发布时间】:2020-05-14 15:34:10
【问题描述】:

我在custom-text-form.html 文件中遇到了反向网址名称。

这是我的custom-text-form.html

{% load rest_framework %}
<head>
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css">
</head>
<html>
    <body>
        <form class="form-horizontal" action="{% url 'tool:custom-text' %}" method="POST" novalidate>
            {% csrf_token %}
            {% render_form serializer template_pack='rest_framework/horizontal' %}
            <div class="form-group">
                <div class="col-sm-offset-2 col-sm-10">
                    <button type="submit" class="btn btn-default">Submit</button>
                </div>
            </div>
        </form>
    </body>
</html>

configs/urls.py.

from django.contrib import admin
from django.urls import path, include
from django.conf import settings
from django.conf.urls import url
from django.views.static import serve
from django.contrib.staticfiles.urls import staticfiles_urlpatterns


urlpatterns = [
    path('admin/', admin.site.urls),
    url(r'^run/media/(?P<path>.*)$', serve,
        {'document_root': settings.MEDIA_ROOT, }),
    path('api/', include('apps.tool.urls'))
]

urlpatterns += staticfiles_urlpatterns()

工具/urls.py.

from django.urls import path, include
from django.conf.urls import url
from rest_framework.routers import SimpleRouter

from .views import CustomTextViewSet, UploadImageViewSet, FormCustomTextView

app_name = 'tool'

router = SimpleRouter(trailing_slash=False)
router.register('upload-image', UploadImageViewSet)
router.register('custom-text', CustomTextViewSet)

urlpatterns = [
    path('', include(router.urls), name='custom-text'),
    url('form-custom-text', FormCustomTextView.as_view())
]

我收到此错误NoReverseMatch at /api/form-custom-text 尝试请求网址时http://localhost:8000/api/form-custom-text error

我的TEMPLATES 配置

TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [
            os.path.join(PROJECT_ROOT, 'templates'),
        ],
        'APP_DIRS': True,
        'OPTIONS': {
            'context_processors': [
                'django.template.context_processors.debug',
                'django.template.context_processors.request',
                'django.contrib.auth.context_processors.auth',
                'django.contrib.messages.context_processors.messages',
            ],
        },
    },
]

我的文件夹结构: img1 img2

我试图在tool/url.py 中添加namespace='tool',但仍然无法正常工作。

任何人都可以提供帮助! 谢谢

【问题讨论】:

  • 你不能引用include,因为那是一组 url,你需要指定一个,所以给目标视图一个名字。
  • 谢谢。当我将这一行 path('api/', include(('apps.tool.urls', 'tool'), namespace='tool')) 添加到 configs/urls 时它起作用了
  • 你能解释一下吗!!
  • 当我访问此 URL localhost:8000/api/custom-text 时不起作用。但在 url localhost:8000/api/custom-text 发生错误`NoReverseMatch at /api/form-custom-text Reverse for 'custom-text' not found. 'custom-text' is not a valid view function or pattern name`.

标签: django templates url django-rest-framework reverse


【解决方案1】:

更改您的configs/urls.py

....

urlpatterns = [
    ....,
    path('api/', include(('apps.tool.urls', 'tool'), namespace='tool'))
]

urlpatterns += staticfiles_urlpatterns()

由于 Django > 2.0 使用 Path 你需要指定 app_namenamespace

建议:尽量使用django-extension。已经注册了许多有用的命令。您可以使用python manage.py show_urls 显示完整的网址架构,包括网址的namespacename

【讨论】:

  • 当我访问这个 url http://localhost:8000/api/custom-text 时它起作用了。但是在 url http://localhost:8000/api/custom-text 发生错误``` NoReverseMatch at /api/form-custom-text Reverse for 'custom-text' not found。 'custom-text' 不是有效的视图函数或模式名称。 ````
  • 我建议你在你的项目中包含django-extension。已经注册了许多有用的命令。详情请查看django-extensions.readthedocs.io/en/latest。安装后,然后运行命令 python manage.py show_urls 以显示您的完整 url 架构包含 namespacename 的 url
  • 我已经包含了django-extension 并找到了确切的路径。谢谢。
猜你喜欢
  • 2012-01-04
  • 2020-07-07
  • 2021-11-15
  • 1970-01-01
  • 1970-01-01
  • 2013-08-23
  • 2012-02-09
  • 1970-01-01
  • 2015-06-28
相关资源
最近更新 更多