Django:上传和处理图像时出现问题(CSRF 令牌丢失或不正确)
Django: Troubles uploadind and handling an image (CSRF token missing or incorrect)
我只是想用 Django 练习。我试图制作一个简单的应用程序,它允许您上传图像和 return 它的像素化版本。
我有两种观点:一种是显示表格,另一种是处理图像和 return 结果。
问题是引发了 'Forbidden (403)' 而不是结果。给出的失败原因:CSRF 令牌丢失或不正确。
urls.py
from django.conf.urls import url
from . import views
urlpatterns = [
url(r'^$', views.index, name='index'),
url(r'^result/$', views.pixelate, name='pixelate')
]
views.py
from django.http import HttpResponse
from django.shortcuts import render_to_response
from .forms import UploadImageForm
from .pixelate import pixelate_image
def index(request):
form = UploadImageForm()
return render_to_response('pixelate/index.html', {'form': form})
def pixelate(request):
form = UploadImageForm(request.POST, request.FILES)
if form.is_valid():
response = HttpResponse(content_type='image/png')
response['Content-Disposition'] = 'filename="image.png"'
img = pixelate_image(request.FILES['image'])
response.write(img)
return response
pixelate.py
from PIL import Image
from io import BytesIO
def pixelate_image(img, pixelSize=9):
buffer = BytesIO()
p = Image(buffer)
image = p.open(img)
image = image.resize((image.size[0]/pixelSize, image.size[1]/pixelSize), Image.NEAREST)
image = image.resize((image.size[0]*pixelSize, image.size[1]*pixelSize), Image.NEAREST)
image.save()
final_image = buffer.getvalue()
buffer.close()
return final_image
forms.py
from django import forms
class UploadImageForm(forms.Form):
image = forms.ImageField()
index.html
<form action="{% url 'pixelate:pixelate' %}" method="post">
{% csrf_token %}
{{ form }}
<input type="submit" value="Submit" />
</form>
index.html
<form action="{% url 'pixelate:pixelate' %}" method="post" enctype="multipart/form-data">
{% csrf_token %}
{{ form }}
<input type="submit" value="Submit" />
</form>
我只是想用 Django 练习。我试图制作一个简单的应用程序,它允许您上传图像和 return 它的像素化版本。 我有两种观点:一种是显示表格,另一种是处理图像和 return 结果。 问题是引发了 'Forbidden (403)' 而不是结果。给出的失败原因:CSRF 令牌丢失或不正确。
urls.py
from django.conf.urls import url
from . import views
urlpatterns = [
url(r'^$', views.index, name='index'),
url(r'^result/$', views.pixelate, name='pixelate')
]
views.py
from django.http import HttpResponse
from django.shortcuts import render_to_response
from .forms import UploadImageForm
from .pixelate import pixelate_image
def index(request):
form = UploadImageForm()
return render_to_response('pixelate/index.html', {'form': form})
def pixelate(request):
form = UploadImageForm(request.POST, request.FILES)
if form.is_valid():
response = HttpResponse(content_type='image/png')
response['Content-Disposition'] = 'filename="image.png"'
img = pixelate_image(request.FILES['image'])
response.write(img)
return response
pixelate.py
from PIL import Image
from io import BytesIO
def pixelate_image(img, pixelSize=9):
buffer = BytesIO()
p = Image(buffer)
image = p.open(img)
image = image.resize((image.size[0]/pixelSize, image.size[1]/pixelSize), Image.NEAREST)
image = image.resize((image.size[0]*pixelSize, image.size[1]*pixelSize), Image.NEAREST)
image.save()
final_image = buffer.getvalue()
buffer.close()
return final_image
forms.py
from django import forms
class UploadImageForm(forms.Form):
image = forms.ImageField()
index.html
<form action="{% url 'pixelate:pixelate' %}" method="post">
{% csrf_token %}
{{ form }}
<input type="submit" value="Submit" />
</form>
index.html
<form action="{% url 'pixelate:pixelate' %}" method="post" enctype="multipart/form-data">
{% csrf_token %}
{{ form }}
<input type="submit" value="Submit" />
</form>