查看未从图像上载页面接收POST数据

时间:2017-06-12 08:12:33

标签: python html django

我已经看过类似的问题,但无法解决这个问题,我有一个程序可以在模特上传图片:

views.py:

def upload_pic(request):
    if request.method == "POST":
        image_form = ImageUploadForm(request.POST, request.FILES)
        if image_form.is_valid():
            image_form.save()
            return HttpResponse('success')
    else:
        return HttpResponse('fail')
    return render(request, 'tracker/upload_pic.html', {"image_form": image_form})

模板:

{% load staticfiles %}
<!DOCTYPE html>
<html>
<body>
<form method="POST" enctype="multipart/form-data">{% csrf_token %}
{{ image_form.img }}
<input type="submit" value="submit" />
</form>
</body>
</html>

urls.py:

urlpatterns = [
url(r'^upload_pic', views.upload_pic, name='upload_pic')
]

只是尝试保存到用户模型以便稍后检索,但我无法访问任何POST数据,我还尝试将action = "{% url upload_pic %}"添加到form在HTML模板中,但我收到NoReverseMatch错误

class ImageUploadForm(forms.ModelForm):
    #model to store the information about the pictures
    class Meta:
        model = ExtendedProfile
        exclude = ()

1 个答案:

答案 0 :(得分:2)

您需要在表单上添加action,以便检索网址名称。

<form action="{% url 'upload_pic' %}" method="POST" enctype="multipart/form-data">{% csrf_token %}
    {{ image_form.img }}
    <input type="submit" value="submit" />
</form>

您忘记了''上的upload_pic