找不到'django_with_ocr.ocr.views.list'的错误。 'django_with_ocr.ocr.views.list'不是有效的视图函数或模式名称
此处的异常值不是有效的函数或模式名称。
还有一个错误' charmap'编解码器无法编码位置843中的字符'\ ufb01':字符映射到
views.py
from django.shortcuts import render
from django.shortcuts import render
from django.shortcuts import render_to_response
from django.template import RequestContext
from django.http import HttpResponseRedirect
from django.urls import reverse
from .models import Document
from .forms import DocumentForm
from django.http import HttpResponse
import csv
import ipdb
from Cython.Compiler.Buffer import context
try:
import Image
except ImportError:
from PIL import Image
import pytesseract
global i
i = 0
def list(request):
global i
# 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()
i += 1
# import ipdb;ipdb.set_trace()
d = Document.objects.get(id=i)
#print d.docfile
k=pytesseract.image_to_string(Image.open(d.docfile))
#print k
handle = open('data.txt', 'a+')
handle.write(k)
handle.close()
txt_file = r"data.txt"
csv_file = r'mycsv.csv'
in_txt = csv.reader(open(txt_file, "r"), delimiter = ' ')
out_csv = csv.writer(open(csv_file, 'w', encoding='utf-8'))
out_csv.writerows(in_txt)
# Redirect to the document list after POST
return HttpResponseRedirect(reverse('django_with_ocr.ocr.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,
'list.html',
{'documents': documents, 'form': form},
context
)
url.py
from django.urls import path
from . import views
urlpatterns = [
path('list/', views.list,name='list' ),
]
list.html
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>OCR Converter</title>
</head>
<body>
<!-- List of uploaded documents -->
{% if documents %}
<ul>
{% for document in documents %}
<li><a href="{{ document.docfile.url }}">{{ document.docfile.name }}</a></li>
{% endfor %}
</ul>
{% else %}
<p>No documents.</p>
{% endif %}
<!-- Upload form. Note enctype attribute! -->
<form action="{% url 'list' %}" method="post" enctype="multipart/form-data">
{% csrf_token %}
<p>{{ form.non_field_errors }}</p>
<p>{{ form.docfile.label_tag }} {{ form.docfile.help_text }}</p>
<p>
{{ form.docfile.errors }}
{{ form.docfile }}
</p>
<p><input type="submit" value="Upload"/></p>
</form>
</body>
</html>
项目的网址是
from django.contrib import admin
from django.urls import path
from django.conf.urls import include, url
from django.conf import settings
from django.conf.urls.static import static
from django.views.generic import RedirectView
urlpatterns = [
path('admin/', admin.site.urls),
path('ocr/', include('ocr.urls')),
path('', RedirectView.as_view(url='/ocr/list/', permanent=True)),
] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
答案 0 :(得分:-1)
您正在致电
return HttpResponseRedirect(reverse('django_with_ocr.ocr.views.list'))
您只需通过其应用名称和网址名称调用网址(假设ocr
是您应用的名称)
return HttpResponseRedirect(reverse('ocr:list'))
修改
添加
app_name = 'ocr'
urlpatterns = [
path('list/', views.list,name='list' ),
]