Win10 Django:NoReverseMatch at / Reverse为'index',参数'()'和关键字参数'{}'找不到。尝试过0种模式:[]

时间:2017-10-17 04:43:01

标签: python django python-3.x

我是python和Django的新手并使用win10系统。

Django版本:1.9.4。和Python版本:3.6.3。

在模板D:\ Music \ music \ templates \ music \ index.html中,第29行出错

19                        <div class="caption">
20                            <h2>{{ album.album_title }}</h2>
21                            <h4>{{ album.artist }}</h4>
22
23                            <!-- View Details -->
24                            <a href="{% url 'detail' album.id %}" class="btn btn-primary btn-sm" role="button">View Details</a>
25
26                            <!-- Delete Album -->
27                            <form action="{% url 'delete_album' album.id %}" method="post" style="display: inline;">    Traceback:

File "D:\Python\lib\site-packages\django\template\defaulttags.py" in render
  507.                               current_app=current_app)

File "D:\Python\lib\site-packages\django\core\urlresolvers.py" in reverse
  600.     return force_text(iri_to_uri(resolver._reverse_with_prefix(view, prefix, *args, **kwargs)))

File "D:\Python\lib\site-packages\django\core\urlresolvers.py" in _reverse_with_prefix
  508.                              (lookup_view_s, args, kwargs, len(patterns), patterns))

During handling of the above exception (Reverse for 'website.index' with arguments '()' and keyword arguments '{}' not found. 0 pattern(s) tried: []), another exception occurred:

File "D:\Python\lib\site-packages\django\core\handlers\base.py" in get_response
  149.                     response = self.process_exception_by_middleware(e, request)

File "D:\Python\lib\site-packages\django\core\handlers\base.py" in get_response
  147.                     response = wrapped_callback(request, *callback_args, **callback_kwargs)

File "D:\Music\music\views.py" in index
  145.             return render(request, 'music/index.html', {'albums': albums})

File "D:\Python\lib\site-packages\django\shortcuts.py" in render
  67.             template_name, context, request=request, using=using)

File "D:\Python\lib\site-packages\django\template\loader.py" in render_to_string
  97.         return template.render(context, request)

File "D:\Python\lib\site-packages\django\template\backends\django.py" in render
  95.             return self.template.render(context)

File "D:\Python\lib\site-packages\django\template\base.py" in render
  206.                     return self._render(context)

File "D:\Python\lib\site-packages\django\template\base.py" in _render
  197.         return self.nodelist.render(context)

File "D:\Python\lib\site-packages\django\template\base.py" in render
  992.                 bit = node.render_annotated(context)

File "D:\Python\lib\site-packages\django\template\base.py" in render_annotated
  959.             return self.render(context)

File "D:\Python\lib\site-packages\django\template\loader_tags.py" in render
  173.         return compiled_parent._render(context)

File "D:\Python\lib\site-packages\django\template\base.py" in _render
  197.         return self.nodelist.render(context)

File "D:\Python\lib\site-packages\django\template\base.py" in render
  992.                 bit = node.render_annotated(context)

File "D:\Python\lib\site-packages\django\template\base.py" in render_annotated
  959.             return self.render(context)

File "D:\Python\lib\site-packages\django\template\defaulttags.py" in render
  513.                         six.reraise(*exc_info)

File "D:\Python\lib\site-packages\django\utils\six.py" in reraise
  686.         raise value

File "D:\Python\lib\site-packages\django\template\defaulttags.py" in render
  499.             url = reverse(view_name, args=args, kwargs=kwargs, current_app=current_app)

File "D:\Python\lib\site-packages\django\core\urlresolvers.py" in reverse
  600.     return force_text(iri_to_uri(resolver._reverse_with_prefix(view, prefix, *args, **kwargs)))

File "D:\Python\lib\site-packages\django\core\urlresolvers.py" in _reverse_with_prefix
  508.                              (lookup_view_s, args, kwargs, len(patterns), patterns))

Exception Type: NoReverseMatch at /
Exception Value: Reverse for 'index' with arguments '()' and keyword arguments '{}' not found. 0 pattern(s) tried: []
28                                {% csrf_token %}
29                                <input type="hidden" name="album_id" value="{{ album.id }}" />
30                                <button type="submit" class="btn btn-default btn-sm">
31                                    <span class="glyphicon glyphicon-trash"></span>
32                                </button>
33                            </form>
34
35                            <!-- Favorite Album -->
36                            <a href="{% url 'favorite_album' album.id %}" class="btn btn-default btn-sm btn-favorite" role="button">
37                                <span class="glyphicon glyphicon-star {% if album.is_favorite %}active{% endif %}"></span>
38                            </a>

这是错误追溯:

  

渲染中的文件“D:\ Python \ lib \ site-packages \ django \ template \ defaulttags.py”     507. current_app = current_app)

     

反向文件“D:\ Python \ lib \ site-packages \ django \ core \ urlresolvers.py”     600. return force_text(iri_to_uri(resolver._reverse_with_prefix(view,prefix,* args,** kwargs)))

      _reverse_with_prefix中的文件“D:\ Python \ lib \ site-packages \ django \ core \ urlresolvers.py”     508.(lookup_view_s,args,kwargs,len(模式),模式))

     

在处理上述异常期间(反向'site.index',参数'()'和关键字参数'{}'未找到.0模式尝试:[]),发生另一个异常:

     

get_response中的文件“D:\ Python \ lib \ site-packages \ django \ core \ handlers \ base.py”     149. response = self.process_exception_by_middleware(e,request)

     

get_response中的文件“D:\ Python \ lib \ site-packages \ django \ core \ handlers \ base.py”     147. response = wrapped_callback(request,* callback_args,** callback_kwargs)

     

索引中的文件“D:\ Music \ music \ views.py”     145. return render(request,'music / index.html',{'albums':albums})

     

渲染中的文件“D:\ Python \ lib \ site-packages \ django \ shortcuts.py”     67. template_name,context,request = request,using = using)

     

render_to_string中的文件“D:\ Python \ lib \ site-packages \ django \ template \ loader.py”     97. return template.render(context,request)

     

渲染中的文件“D:\ Python \ lib \ site-packages \ django \ template \ backends \ django.py”     95.返回self.template.render(context)

     

渲染中的文件“D:\ Python \ lib \ site-packages \ django \ template \ base.py”     206. return self._render(context)

     _render中的

文件“D:\ Python \ lib \ site-packages \ django \ template \ base.py”     197. return self.nodelist.render(context)

     

渲染中的文件“D:\ Python \ lib \ site-packages \ django \ template \ base.py”     992. bit = node.render_annotated(context)

     

render_annotated中的文件“D:\ Python \ lib \ site-packages \ django \ template \ base.py”     959. return self.render(context)

     

渲染中的文件“D:\ Python \ lib \ site-packages \ django \ template \ loader_tags.py”     173. return compiled_pa​​rent._render(context)

     _render中的

文件“D:\ Python \ lib \ site-packages \ django \ template \ base.py”     197. return self.nodelist.render(context)

     

渲染中的文件“D:\ Python \ lib \ site-packages \ django \ template \ base.py”     992. bit = node.render_annotated(context)

     

render_annotated中的文件“D:\ Python \ lib \ site-packages \ django \ template \ base.py”     959. return self.render(context)

     

渲染中的文件“D:\ Python \ lib \ site-packages \ django \ template \ defaulttags.py”     513. six.reraise(* exc_info)

     

重新加载文件“D:\ Python \ lib \ site-packages \ django \ utils \ six.py”     686.提高价值

     

渲染中的文件“D:\ Python \ lib \ site-packages \ django \ template \ defaulttags.py”     499. url = reverse(view_name,args = args,kwargs = kwargs,current_app = current_app)

     

反向文件“D:\ Python \ lib \ site-packages \ django \ core \ urlresolvers.py”     600. return force_text(iri_to_uri(resolver._reverse_with_prefix(view,prefix,* args,** kwargs)))

      _reverse_with_prefix中的文件“D:\ Python \ lib \ site-packages \ django \ core \ urlresolvers.py”     508.(lookup_view_s,args,kwargs,len(模式),模式))

     

异常类型:NoReverseMatch at /

     

异常值:反向'index',参数'()'和关键字参数'{}'未找到。尝试了0种模式:[]

这是我的音乐\ urls.py:

from django.conf.urls import url
from . import views

app_name = 'music'

urlpatterns = [
    url(r'^$', views.index, name='index'),
    url(r'^register/$', views.register, name='register'),
    url(r'^login_user/$', views.login_user, name='login_user'),
    url(r'^logout_user/$', views.logout_user, name='logout_user'),
    url(r'^(?P<album_id>[0-9]+)/$', views.detail, name='detail'),
    url(r'^(?P<song_id>[0-9]+)/favorite/$', views.favorite, name='favorite'),
    url(r'^songs/(?P<filter_by>[a-zA_Z]+)/$', views.songs, name='songs'),
    url(r'^create_album/$', views.create_album, name='create_album'),
    url(r'^(?P<album_id>[0-9]+)/create_song/$', views.create_song, name='create_song'),
    url(r'^(?P<album_id>[0-9]+)/delete_song/(?P<song_id>[0-9]+)/$', views.delete_song, name='delete_song'),
    url(r'^(?P<album_id>[0-9]+)/favorite_album/$', views.favorite_album, name='favorite_album'),
    url(r'^(?P<album_id>[0-9]+)/delete_album/$', views.delete_album, name='delete_album'),
]

urls.py:

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

urlpatterns = [
    url(r'^admin/', admin.site.urls),
    url(r'^music/', include('music.urls'),),
    url(r'^', include('music.urls')),
]

if settings.DEBUG:
    urlpatterns += static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
    urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)

如何解决这个问题?

2 个答案:

答案 0 :(得分:1)

将主要urls.py中的音乐应用网址命名为:

url(r'^music/', include('music.urls'), namespace="music"),

如果在slash config的设置中没有处理过斜杠,请从网址模式中删除尾部斜杠

url(r'^(?P<album_id>[0-9]+)$', views.detail, name='detail'),

您可以在模板中引用这些命名的url模式,如下所示:

{% url 'music:detail' album.id %}

答案 1 :(得分:0)

你正在某处调用website.index。将其更改为music:index,其中musicapp_nameindex为网址名称。