需要POST类型时,请求类型为GET

时间:2015-07-15 15:20:48

标签: python django post django-forms django-views

我正在尝试构建一个使用表单上传文件的简单Django应用程序但是每当我在localhost上运行我的应用程序时,我在尝试加载根页时遇到以下错误 - http://127.0.0.1:8000/

  

/

处的UnboundLocalError      分配前引用的

局部变量'form'

     

/Users/danieloram/PycharmProjects/FileUploaderProject/FileUploader/views.py in index

     
      
  1.                                    {'images': images, 'form': form},
    
         

    ...

  2.         

    ▶本地大战

以下是相对view.py,models.py和index.html(问题中的视图)文件的代码:

views.py ##

from django.shortcuts import render
from django.http import HttpResponseRedirect
from django.shortcuts import render_to_response
from django.template import RequestContext

#import models and forms
from FileUploader.models import Image
from FileUploader.forms import ImageUploadForm

# Create your views here.

#this view will respond to file uploads
def index(request):
    print("View loaded")
    if request.method =='POST':
        #create a form from the model I made
        form = ImageUploadForm(request.POST, request.FILES)
        #check form contains no errors
        if form.is_valid():
            #create a new image and save it! file param must be name of attribute in ImageUploadForm
            newImage = Image(imageFile = request.FILES['imageFile'])
            newImage.save()
            #redirect to success page!
            return HttpResponseRedirect('/success/')
        else:
            #create an empty form if it failed
            form = ImageUploadForm()
    else:
        #view will not load as error thrown from nothing being assigned to the form.
        print("Form method was not POST! nothing was assigned to the form and it will fail.")
    images = Image.objects.all()
    #render the page and pass the dictionary to the view
    return render_to_response('index.html',
                              {'images': images, 'form': form},
                              context_instance=RequestContext(request))
def success(request):
    debug = str(request)
    return render(request, 'success.html', {'debug': debug})

models.py ##

from django.db import models

# Create your models here.

#simple model for creating image objects
class Image(models.Model):
    imageFile = models.FileField(upload_to='images/%Y/%m/%d')

index.html ##

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    {% load staticfiles %}
    <link rel="stylesheet" type="text/css" href="{% static "style.css" %}">
    <title>Daniel Oram's FileUploading Django Project</title>
</head>
<body>
<div class="container">
    <div id="buffer"></div>
    <div id="panel">
        <p>Choose a file to upload</p>
        <form id="form" action="{% url 'index' %}" method="post" enctype="multipart/form-data">
            <input id="input" type="file" value="Enter Image here for upload" width="100">
            <input type="submit" value="Submit" width="50">
            <!-- I need csrf_token because I used RequestContext in the view -->
            {% csrf_token %}
            <p>{{ form.non_field_errors }}</p>
            <p>{{ form.imageFile.label }} {{ form.imageFile.help_text }}</p>
            <p>
                {{ form.imageFile.error_messages }}
                {{ form.imageFile }}
            </p>
        </form>
    </div>
    <!--List the files already uploaded to the webapp. -->
    <div id="list_of_Images">
        {% if images %}
        <ul>
            <!--Iterate Images stored in media directory and display a link for each -->
            {% for image in images %}
            <li><a href='{{ image.imageFile.url }}'>{{ image.imageFile.name }}</a></li>
            {% endfor %}
        </ul>
        {% else %}
        <p>There are no Images that have been previously uploaded :(</p>
        {% endif %}
    </div>
</div>
</body>
</html>

我的调试工作

到目前为止,我通过调试发现错误是由于form变量未在views.py的索引方法中初始化而导致的,因为if request.method =='POST':总是在评估{{ 1}}因为False是GET方法..

任何人都可以帮我解释如何在Django中发出POST请求而不是对此上下文的GET请求,从而解决我的错误并加载页面。我甚至不关心表单是否有效,我只是想要一个解决方案的帮助。谢谢! PS - urls.py文件设置正确。

解决方案

在此背景下:

  • 首次加载页面时会执行获取请求。

  • 提交表单数据后发布请求。

  • 表单变量需要初始化,以便在初始页面加载时传递给视图。

  • 为了初始页面加载的目的,初始化表单的Else子句被附加到错误的if语句并且无法访问。

正确的索引方法

request

2 个答案:

答案 0 :(得分:3)

如果您在浏览器中提交表单,通常的工作流程是:

  1. 您使用GET请求浏览到该网址
  2. 浏览器使用POST请求提交数据(因为表单有action="POST"
  3. 如果表单有效,您将被重定向到成功的网址
  4. 因此,您应该使代码适用于初始GET请求,而不是询问如何将所有请求更改为POST。您可以通过在else分支中创建未绑定的表单来完成此操作。

        if request.method =='POST':
            # create a form bound to the post data
            form = ImageUploadForm(request.POST, request.FILES)
            ...
        else:
            # create an unbound form for the original GET request
            form = ImageUploadForm()
            ...
    

    您可能会发现阅读有关working with forms的Django文档很有用。示例视图与您尝试的视图非常相似。

答案 1 :(得分:1)

您的问题不在于请求方法(显然您首先需要执行GET以呈现表单,以便您可以对其进行POST),但事实上您绑定名字“形式”wjen它是一个GET:

def index(request):
    print("View loaded")
    if request.method =='POST':
        form = ImageUploadForm(request.POST, request.FILES)
        if form.is_valid():
            newImage = Image(imageFile = request.FILES['imageFile'])
            newImage.save()
            return HttpResponseRedirect('/success/')    

        # this part is wrong : if the validation failed,
        # you don't want to create a new empty form but
        # redisplay the failed form so you can display
        # the validation errors
        #else:
        #    #create an empty form if it failed
        #    form = ImageUploadForm()

    else:
        ## view will not load as error thrown 
        ## from nothing being assigned to the form.
        # => actually the real error is that you dont 
        #    define the *name* 'form' at all
        #    in this branch...
        #    IOW that's where you want to create 
        #    an "empty" form so the user can fill and
        #    submit it
        form = ImageUploadForm()