request.POST.get 没有得到输入值
request.POST.get didn't get the input value
所以,我试图做的是用户输入一个 base64 编码的图像,然后通过 pytesseract OCR 将其转换为文本。问题是每次我尝试输入时都只是重新加载页面而没有任何反应。
这是我的 html 片段:
<form method="POST" action="{% url 'ocr-view'%}"> {% csrf_token %}
<label for="base64"> Input Base64 :</label>
<input type="text" class="form-control" id="text" name="base64" />
<button type="submit" class="btn btn-primary mt-3" name="submit">
<i class="fas fa-search"></i>
Submit
</button>
</form>
<div class="card-body">
<label> Result : </label> <br>
<span class="h3">{{text}}</span>
</div>
这是我的 views.py :
class IndexView(LoginRequiredMixin, CreateView):
model = Ocr
template_name = "ocr/ocr.html"
fields = ['input']
def get_context_data (self):
text = ""
if self.request.method == 'POST':
imgstring = self.request.POST.get('base64')
imgstring = imgstring.split('base64,')[-1].strip()
image_string = BytesIO(base64.b64decode(imgstring))
image = Image.open(image_string)
text = pytesseract.image_to_string(image)
text = text.encode("ascii", "ignore")
text = text.decode()
context = {
'text': text,
}
return context
Urls.py :
from django.urls import path
from .views import IndexView
urlpatterns = [
path("", IndexView.as_view(), name="ocr-view"),
path('<int:pk>/', IndexView.as_view(), name='ocr-input'),
]
但是,如果我将 base64 代码直接放入 views.py,OCR 功能将完美运行。
imgstring = 'data:image/jpeg;base64,/9j/somerandomcode'
imgstring = imgstring.split('base64,')[-1].strip()
image_string = BytesIO(base64.b64decode(imgstring))
image = Image.open(image_string)
text = pytesseract.image_to_string(image)
text = text.encode("ascii", "ignore")
text = text.decode()
context = {
'text': text,
}
return context
我假设我的post方法有问题,请检查我的代码是否有问题
我认为 get_context_data()
不是使用 POST 数据的正确方法,因为我认为此方法不会在 POST 请求中调用(仅使用该方法在 GET 请求中)。
您是否尝试过 CreateView
的其他方法?
我建议你试试 form_valid()
method; here is another part of the docs with some examples https://docs.djangoproject.com/en/3.2/topics/class-based-views/generic-editing/#model-forms
所以,我试图做的是用户输入一个 base64 编码的图像,然后通过 pytesseract OCR 将其转换为文本。问题是每次我尝试输入时都只是重新加载页面而没有任何反应。
这是我的 html 片段:
<form method="POST" action="{% url 'ocr-view'%}"> {% csrf_token %}
<label for="base64"> Input Base64 :</label>
<input type="text" class="form-control" id="text" name="base64" />
<button type="submit" class="btn btn-primary mt-3" name="submit">
<i class="fas fa-search"></i>
Submit
</button>
</form>
<div class="card-body">
<label> Result : </label> <br>
<span class="h3">{{text}}</span>
</div>
这是我的 views.py :
class IndexView(LoginRequiredMixin, CreateView):
model = Ocr
template_name = "ocr/ocr.html"
fields = ['input']
def get_context_data (self):
text = ""
if self.request.method == 'POST':
imgstring = self.request.POST.get('base64')
imgstring = imgstring.split('base64,')[-1].strip()
image_string = BytesIO(base64.b64decode(imgstring))
image = Image.open(image_string)
text = pytesseract.image_to_string(image)
text = text.encode("ascii", "ignore")
text = text.decode()
context = {
'text': text,
}
return context
Urls.py :
from django.urls import path
from .views import IndexView
urlpatterns = [
path("", IndexView.as_view(), name="ocr-view"),
path('<int:pk>/', IndexView.as_view(), name='ocr-input'),
]
但是,如果我将 base64 代码直接放入 views.py,OCR 功能将完美运行。
imgstring = 'data:image/jpeg;base64,/9j/somerandomcode'
imgstring = imgstring.split('base64,')[-1].strip()
image_string = BytesIO(base64.b64decode(imgstring))
image = Image.open(image_string)
text = pytesseract.image_to_string(image)
text = text.encode("ascii", "ignore")
text = text.decode()
context = {
'text': text,
}
return context
我假设我的post方法有问题,请检查我的代码是否有问题
我认为 get_context_data()
不是使用 POST 数据的正确方法,因为我认为此方法不会在 POST 请求中调用(仅使用该方法在 GET 请求中)。
您是否尝试过 CreateView
的其他方法?
我建议你试试 form_valid()
method; here is another part of the docs with some examples https://docs.djangoproject.com/en/3.2/topics/class-based-views/generic-editing/#model-forms