django文件上传给NoReverseMatch错误

时间:2016-05-02 14:30:05

标签: python django

编辑2:我知道我的网址是

url(r'^ngasite/', include('ngasite.urls')),
url(r'^admin/', admin.site.urls),
url(r'^', include('ngasite.urls')),

显示一个包含两次,我认为我基本上将根切换为ngasite.urls等,在任何一种情况下建议这可能是一个问题,我确实删除了这个似乎仍然失败,没有反向匹配,而我正在考虑更多的参数问题?只想用这个更新问题。

编辑:为app发布urls.py,然后为完整的django应用发布:

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

app_name = 'ngasite'
urlpatterns = [
    # ex: /ngasite/
    url(r'^$', views.IndexView.as_view(), name='index'),
    # ex: /ngasite/5/
    url(r'^(?P<pk>[0-9]+)/$', views.DetailView.as_view(), name='detail'),
    # ex: /ngasite/5/results/
    url(r'^(?P<pk>[0-9]+)/results/$', views.ResultsView.as_view(), name='results'),
    # ex: /ngasite/5/vote/
    url(r'^(?P<question_id>[0-9]+)/vote/$', views.vote, name='vote'),

    url(r'^list/$', views.list, name='list'),
]

完整的应用urls.py:

from django.conf.urls import url
from django.contrib import admin
from django.conf.urls import include
from django.views.generic import TemplateView

from django.conf import settings
from django.conf.urls.static import static

urlpatterns = [
    url(r'^ngasite/', include('ngasite.urls')),
    url(r'^admin/', admin.site.urls),
    url(r'^', include('ngasite.urls')),
] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)

我试图使用此处制作的好帖子将文件上传到基于django的网站:

Need a minimal Django file upload example

执行了第1步到第7步,所有内容似乎都在运行,直到我尝试点击我的列表网址并且我的list.html模板视图中出现错误:

<form action="{% url 'list' %}" method="post" enctype="multipart/form-data">

它抱怨网址列表&#39;我相信,但我不太确定错误告诉我的是什么。我承认我是一个django新手,在得到一个基本的网站并运行之后潜入这个例子,以为我有Django的要点猜不到。

模板文件与之前的示例完全相同,并且更改了正确的appname。

完整输出 控制台输出:

/usr/local/lib/python2.7/site-packages/django/shortcuts.py:45: RemovedInDjango110Warning: The context_instance argument of render_to_string is deprecated.
  using=using)

Internal Server Error: /ngasite/list/
Traceback (most recent call last):
  File "/usr/local/lib/python2.7/site-packages/django/core/handlers/base.py", line 149, in get_response
    response = self.process_exception_by_middleware(e, request)
  File "/usr/local/lib/python2.7/site-packages/django/core/handlers/base.py", line 147, in get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
  File "/Users/shane.thomas/programming/fadeenv/ngasite/ngasite/views.py", line 77, in list
    context_instance=RequestContext(request)
  File "/usr/local/lib/python2.7/site-packages/django/shortcuts.py", line 45, in render_to_response
    using=using)
  File "/usr/local/lib/python2.7/site-packages/django/template/loader.py", line 114, in render_to_string
    template_name, context, context_instance, dirs, dictionary)
  File "/usr/local/lib/python2.7/site-packages/django/template/engine.py", line 244, in render_to_string
    return t.render(context_instance)
  File "/usr/local/lib/python2.7/site-packages/django/template/base.py", line 206, in render
    return self._render(context)
  File "/usr/local/lib/python2.7/site-packages/django/template/base.py", line 197, in _render
    return self.nodelist.render(context)
  File "/usr/local/lib/python2.7/site-packages/django/template/base.py", line 992, in render
    bit = node.render_annotated(context)
  File "/usr/local/lib/python2.7/site-packages/django/template/base.py", line 959, in render_annotated
    return self.render(context)
  File "/usr/local/lib/python2.7/site-packages/django/template/defaulttags.py", line 513, in render
    six.reraise(*exc_info)
  File "/usr/local/lib/python2.7/site-packages/django/template/defaulttags.py", line 499, in render
    url = reverse(view_name, args=args, kwargs=kwargs, current_app=current_app)
  File "/usr/local/lib/python2.7/site-packages/django/core/urlresolvers.py", line 600, in reverse
    return force_text(iri_to_uri(resolver._reverse_with_prefix(view, prefix, *args, **kwargs)))
  File "/usr/local/lib/python2.7/site-packages/django/core/urlresolvers.py", line 508, in _reverse_with_prefix
    (lookup_view_s, args, kwargs, len(patterns), patterns))
NoReverseMatch: Reverse for 'list' with arguments '()' and keyword arguments '{}' not found. 0 pattern(s) tried: []
[02/May/2016 07:20:26] "GET /ngasite/list/ HTTP/1.1" 500 118169

网页输出:

NoReverseMatch at /ngasite/list/
Reverse for 'list' with arguments '()' and keyword arguments '{}' not found. 0 pattern(s) tried: []
Request Method:
GET
Request URL:
http://localhost:8000/ngasite/list/
Django Version:
1.9.5
Exception Type:
NoReverseMatch
Exception Value:
Reverse for 'list' with arguments '()' and keyword arguments '{}' not found. 0 pattern(s) tried: []
Exception Location:
/usr/local/lib/python2.7/site-packages/django/core/urlresolvers.py in _reverse_with_prefix, line 508
Python Executable:
/usr/local/opt/python/bin/python2.7
Python Version:
2.7.11
Python Path:
['/Users/shane.thomas/programming/fadeenv/mysite',
 '/Library/Frameworks/SQLite3.framework/Versions/C/Python/2.7',
 '/Library/Frameworks/GEOS.framework/Versions/3/Python/2.7/site-packages',
 '/Library/Frameworks/GDAL.framework/Versions/1.11/Python/2.7/site-packages',
 '/usr/local/lib/python2.7/site-packages/pyproj-1.9.5.1-py2.7-macosx-10.11-x86_64.egg',
 '/usr/local/Cellar/python/2.7.11/Frameworks/Python.framework/Versions/2.7/lib/python27.zip',
 '/usr/local/Cellar/python/2.7.11/Frameworks/Python.framework/Versions/2.7/lib/python2.7',
 '/usr/local/Cellar/python/2.7.11/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-darwin',
 '/usr/local/Cellar/python/2.7.11/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-mac',
 '/usr/local/Cellar/python/2.7.11/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-mac/lib-scriptpackages',
 '/usr/local/Cellar/python/2.7.11/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-tk',
 '/usr/local/Cellar/python/2.7.11/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-old',
 '/usr/local/Cellar/python/2.7.11/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-dynload',
 '/usr/local/lib/python2.7/site-packages',
 '/usr/local/lib/python2.7/site-packages/geos',
 '/Library/Python/2.7/site-packages/numpy-override',
 '/Library/Python/2.7/site-packages']
Server time:
Mon, 2 May 2016 07:20:26 -0700
Error during template rendering
In template /Users/shane.thomas/programming/fadeenv/mysite/ngasite/templates/ngasite/list.html, error at line 20
Reverse for 'list' with arguments '()' and keyword arguments '{}' not found. 0 pattern(s) tried: []
10
        <ul>

11
        {% for document in documents %}

12
            <li><a href="{{ document.docfile.url }}">{{ document.docfile.name }}</a></li>

13
        {% endfor %}

14
        </ul>

15
    {% else %}

16
        <p>No documents.</p>

17
    {% endif %}

18


19
        <!-- Upload form. Note enctype attribute! -->

20
        <form action="{% url 'list' %}" method="post" enctype="multipart/form-data">

21
            {% csrf_token %}

22
            <p>{{ form.non_field_errors }}</p>

F

2 个答案:

答案 0 :(得分:0)

我在你的项目url.py文件中看到了一些问题

urlpatterns = [
    url(r'^ngasite/', include('ngasite.urls')),
    url(r'^admin/', admin.site.urls),
    url(r'^', include('ngasite.urls')),
] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)

你已经提到了两次

url(r'^ngasite/', include('ngasite.urls')),

url(r'^', include('ngasite.urls')),

这意味着view.list

有两个网址路径
1) /list/
2) /ngasite/list/

我想这肯定是问题。希望这有帮助

答案 1 :(得分:0)

我正在回答我自己的问题(我讨厌这样做)但也许它会帮助另一个菜鸟django。问题似乎是一个命名空间问题,在我需要的情况下我需要:

<form action="{% url 'ngasite:list' %}" method="post" enctype="multipart/form-data">

而不是

<form action="{% url 'list' %}" method="post" enctype="multipart/form-data">

我有