Django-未找到“ list1”的反义词。 “ list1”不是有效的视图函数或模式名称

时间:2018-08-22 21:05:39

标签: django django-2.1

我正在尝试上传文件,我从某个网站获得了代码。这是用较旧的Django版本编写的,而我使用的是最新版本。在运行时遇到一些错误,请通过stackoverflow进行修复。但是现在我对提到的错误一无所知,这是我的第一个Django项目。提前致谢。 以下是我的文件

views.py

from django.shortcuts import render
from django.http import HttpResponseRedirect
from .models import Document
from .forms import DocumentForm
from django.urls import reverse
#

def list1(request):
    # Handle file upload
    if request.method == 'POST':
        form = DocumentForm(request.POST, request.FILES)
        if form.is_valid():
            newdoc = Document(docfile = request.FILES['docfile'])
            newdoc.save()

            # Redirect to the document list after POST
            return HttpResponseRedirect(reverse('myproject.myapp.views.list'))
    else:
        form = DocumentForm() # A empty, unbound form

    # Load documents for the list page
    documents = Document.objects.all()

    # Render list page with the documents and the form
    return render(request,
        'csv_manipulation/list.html',
    )

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

urls.py

from django.urls import path, include
from .views import list1

urlpatterns = [
    path(r'', list1),
    path(r'list/', list1),
]

forms.py

from django import forms

class DocumentForm(forms.Form):
    docfile = forms.FileField(
        label='Select a file',
    )

list.html

<!DOCTYPE html>
<html>
        <head>
                <meta charset="utf-8">
                <title>Minimal Django File Upload Example</title>
        </head>

        <body>
                <!-- List of uploaded documents -->
                {% if documents %}
                        <ul>
                        {% for document in documents %}
                                <li><a href="{{ document.docfile.url }}">{{ document.docfile.name }}
                                <img src="{{ document.docfile.url }}" alt="{{ document.docfile.name }}">
                                </a></li>
                        {% endfor %}
                        </ul>
                {% else %}
                        <p>No documents.</p>
                {% endif %}

                <!-- Upload form. Note enctype attribute! -->
                <form action="{% url 'list1' %}" method="post" enctype="multipart/form-data">
                        {% csrf_token %}
                        <p>{{ form.non_field_errors }}</p>
                        <p>{{ form.docfile.label_tag }} </p>
                        <p>
                                {{ form.docfile.errors }}
                                {{ form.docfile }}
                        </p>
                        <p><input type="submit" value="Upload" /></p>
                </form>

        </body>

</html>

错误

Template error:
In template C:\Users\nmasthex\projects\adept_proj\adept_stage1\csv_manipulation\templates\csv_manipulation\list.html, error at line 23
       Reverse for 'list1' not found. 'list1' is not a valid view function or pattern name.
       13 :                                 <li><a href="{{ document.docfile.url }}">{{ document.docfile.name }}
       14 :                                 <img src="{{ document.docfile.url }}" alt="{{ document.docfile.name }}">
       15 :                                 </a></li>
       16 :                         {% endfor %}
       17 :                         </ul>
       18 :                 {% else %}
       19 :                         <p>No documents.</p>
       20 :                 {% endif %}
       21 : 
       22 :                 <!-- Upload form. Note enctype attribute! -->
       23 :                 <form action=" {% url 'list1' %} " method="post" enctype="multipart/form-data">
       24 :                         {% csrf_token %}
       25 :                         <p>{{ form.non_field_errors }}</p>
       26 :                         <p>{{ form.docfile.label_tag }} </p>
       27 :                         <p>
       28 :                                 {{ form.docfile.errors }}
       29 :                                 {{ form.docfile }}
       30 :                         </p>
       31 :                         <p><input type="submit" value="Upload" /></p>
       32 :                 </form>
       33 : 


    Traceback:

    File "C:\Users\nmasthex\Envs\myproject\lib\site-packages\django\core\handlers\exception.py" in inner
      34.             response = get_response(request)

    File "C:\Users\nmasthex\Envs\myproject\lib\site-packages\django\core\handlers\base.py" in _get_response
      126.                 response = self.process_exception_by_middleware(e, request)

    File "C:\Users\nmasthex\Envs\myproject\lib\site-packages\django\core\handlers\base.py" in _get_response
      124.                 response = wrapped_callback(request, *callback_args, **callback_kwargs)

    File "C:\Users\nmasthex\projects\adept_proj\adept_stage1\csv_manipulation\views.py" in list1
      29.         'csv_manipulation/list.html',

    File "C:\Users\nmasthex\Envs\myproject\lib\site-packages\django\shortcuts.py" in render
      36.     content = loader.render_to_string(template_name, context, request, using=using)

    File "C:\Users\nmasthex\Envs\myproject\lib\site-packages\django\template\loader.py" in render_to_string
      62.     return template.render(context, request)

    File "C:\Users\nmasthex\Envs\myproject\lib\site-packages\django\template\backends\django.py" in render
      61.             return self.template.render(context)

    File "C:\Users\nmasthex\Envs\myproject\lib\site-packages\django\template\base.py" in render
      171.                     return self._render(context)

    File "C:\Users\nmasthex\Envs\myproject\lib\site-packages\django\template\base.py" in _render
      163.         return self.nodelist.render(context)

    File "C:\Users\nmasthex\Envs\myproject\lib\site-packages\django\template\base.py" in render
      937.                 bit = node.render_annotated(context)

    File "C:\Users\nmasthex\Envs\myproject\lib\site-packages\django\template\base.py" in render_annotated
      904.             return self.render(context)

    File "C:\Users\nmasthex\Envs\myproject\lib\site-packages\django\template\defaulttags.py" in render
      442.             url = reverse(view_name, args=args, kwargs=kwargs, current_app=current_app)

    File "C:\Users\nmasthex\Envs\myproject\lib\site-packages\django\urls\base.py" in reverse
      90.     return iri_to_uri(resolver._reverse_with_prefix(view, prefix, *args, **kwargs))

    File "C:\Users\nmasthex\Envs\myproject\lib\site-packages\django\urls\resolvers.py" in _reverse_with_prefix
      622.         raise NoReverseMatch(msg)

    Exception Type: NoReverseMatch at /csv_manipulation/
    Exception Value: Reverse for 'list1' not found. 'list1' is not a valid view function or pattern name.

2 个答案:

答案 0 :(得分:1)

您需要为此特定视图在URL中添加一个命名空间。

path(r'list/', list1, name=‘namespace’)

然后在模板中使用:

{% url ‘app_name:namespace’ %}

path()中的名称空间变量可以是您想要的任何内容:

path(r'list/', list1, name=‘foo’)

然后:

{% url ‘app_name:foo’ %}

答案 1 :(得分:0)

也许您应该尝试在网址路径中添加第三个参数。如果要使用 {% url 'list1' %},您需要添加名称 path(r'list/', list1, name='list1'),