【问题标题】:NoReverseMatch at /app/index I already URLNoReverseMatch 在 /app/index 我已经 URL
【发布时间】:2018-02-16 01:53:54
【问题描述】:

我在 /app/index 处遇到错误,NoReverseMatch。 我在views.py中写了

def index(request):
    return render(request, 'index.html')

def test1(request):
    return render(request, 'test1.html')

def test2(request):
    return render(request, 'test2.html')

在 urls.py 中

app_name = "app"
urlpatterns = [
    path('index', views.index,name='index'),
    path('test1', views.test1,name='test1'),
    path('test2', views.test2,name='test2'),
]

在 index.html 中

    <tr>

        <td align="center ">
            <a class="test1" href="{% url 'test1' %} ">test1</a>
        </td>

        <td align="center ">
            <a class="test2" href="{% url 'test2' %} ">test2</a>
        </td>
    </tr>

当我访问索引方法时, /app/index 处的 NoReverseMatch 未找到“test1”的反向。 “test1”不是有效的视图函数或模式名称。发生错误。 我重写了

<a class="test1" href="{% url 'app:test1' %} ">test1</a>

但同样的错误发生了。 我真的不明白为什么会出现这个错误。我已经 test1&test2 url。我应该如何解决这个问题? 追溯 说 Traceback(最近一次调用最后一次):

  File "/Users/xxx/anaconda/envs/py36/lib/python3.6/site-packages/django/core/handlers/exception.py", line 35, in inner
    response = get_response(request)
  File "/Users/xxx/anaconda/envs/py36/lib/python3.6/site-packages/django/core/handlers/base.py", line 128, in _get_response
    response = self.process_exception_by_middleware(e, request)
  File "/Users/xxx/anaconda/envs/py36/lib/python3.6/site-packages/django/core/handlers/base.py", line 126, in _get_response
    response = wrmymyapped_callback(request, *callback_args, **callback_kwargs)
  File "/Users/xxx/anaconda/envs/py36/lib/python3.6/site-packages/django/contrib/auth/decorators.py", line 21, in _wrmymyapped_view
    return view_func(request, *args, **kwargs)
  File "/Users/xxx/Downloads/mymyapp/myapp/views.py", line 166, in kenshinresults
    return render(request, 'index.html')
  File "/Users/xxx/anaconda/envs/py36/lib/python3.6/site-packages/django/shortcuts.py", line 36, in render
    content = loader.render_to_string(template_name, context, request, using=using)
  File "/Users/xxx/anaconda/envs/py36/lib/python3.6/site-packages/django/template/loader.py", line 62, in render_to_string
    return template.render(context, request)
  File "/Users/xxx/anaconda/envs/py36/lib/python3.6/site-packages/django/template/backends/django.py", line 61, in render
    return self.template.render(context)
  File "/Users/xxx/anaconda/envs/py36/lib/python3.6/site-packages/django/template/base.py", line 175, in render
    return self._render(context)
  File "/Users/xxx/anaconda/envs/py36/lib/python3.6/site-packages/django/template/base.py", line 167, in _render
    return self.nodelist.render(context)
  File "/Users/xxx/anaconda/envs/py36/lib/python3.6/site-packages/django/template/base.py", line 943, in render
    bit = node.render_annotated(context)
  File "/Users/xxx/anaconda/envs/py36/lib/python3.6/site-packages/django/template/base.py", line 910, in render_annotated
    return self.render(context)
  File "/Users/xxx/anaconda/envs/py36/lib/python3.6/site-packages/django/template/defaulttags.py", line 447, in render
    url = reverse(view_name, args=args, kwargs=kwargs, current_mymyapp=current_mymyapp)
  File "/Users/xxx/anaconda/envs/py36/lib/python3.6/site-packages/django/urls/base.py", line 88, in reverse
    return iri_to_uri(resolver._reverse_with_prefix(view, prefix, *args, **kwargs))
  File "/Users/xxx/anaconda/envs/py36/lib/python3.6/site-packages/django/urls/resolvers.py", line 632, in _reverse_with_prefix
    raise NoReverseMatch(msg)

我的基本网址是

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

urlpatterns = [
    url('admin/', admin.site.urls),
    url('app/', include('app.urls')),

 ]

【问题讨论】:

  • 希望这个 SO Answer 能帮助您解决问题,stackoverflow.com/questions/38390177/…
  • 请提供控制台输出。
  • @abybaddi009 我添加了我的 Traceback
  • 你能分享你的基本网址吗?
  • @vishes_shell 我添加了我的基本网址。

标签: python django


【解决方案1】:

你可以试试这个, 在views.py

def index(request):
    return render(request, 'app/index.html')

def test1(request):
    return render(request, 'app/test1.html')

def test2(request):
    return render(request, 'app/test2.html')

【讨论】:

    【解决方案2】:

    我不确定,但值得尝试更改您在 django.urls docs 上显示的导入和 urlpatterns:

    from django.urls import path, include
    
    urlpatterns = [
        path('admin/', admin.site.urls),  # note that its `path` not `url`
        path('app/', include('app.urls')),  # note that its `path` not `url`
    ]
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-09-26
      • 2013-01-19
      • 2015-07-18
      • 2015-08-18
      • 2020-02-23
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多