Reputation: 372
I'm trying to upload a file, I got the code from some website. Which was written in older Django version and i'm using a latest version. Got some errors while running, fixed them by going through stackoverflow. But now i'm stuck with no clue with the error mentioned, this is my first Django project. Thanks in advance. Below are my files
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>
Error
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.
Upvotes: 1
Views: 225
Reputation: 1035
Maybe you should try to add third parameter to url path. If you want to use
{% url 'list1' %}
you'll need add name
path(r'list/', list1, name='list1'),
Upvotes: 0
Reputation:
You need to add a namespace to your url for this particular view.
path(r'list/', list1, name=‘namespace’)
Then in your template, use:
{% url ‘app_name:namespace’ %}
N.B. The namespace variable in your path()
can be anything you want:
path(r'list/', list1, name=‘foo’)
Then:
{% url ‘app_name:foo’ %}
Upvotes: 1