Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/django/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 请求类型是在需要POST类型时获取的_Python_Django_Post_Django Forms_Django Views - Fatal编程技术网

Python 请求类型是在需要POST类型时获取的

Python 请求类型是在需要POST类型时获取的,python,django,post,django-forms,django-views,Python,Django,Post,Django Forms,Django Views,我正在尝试构建一个简单的Django应用程序,该应用程序使用表单上载文件,但每当我在localhost上运行我的应用程序时,在尝试加载根页面时会出现以下错误-http://127.0.0.1:8000/: 未绑定的本地错误位于/ 赋值前引用的局部变量“form” /索引中的Users/danieloram/PycharmProjects/FileUploaderProject/FileUploader/views.py ▶ 局部变量 以下是quesiton文件中视图的相对view.py、mod

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

未绑定的本地错误位于/

赋值前引用的局部变量“form”

/索引中的Users/danieloram/PycharmProjects/FileUploaderProject/FileUploader/views.py

▶ 局部变量

以下是quesiton文件中视图的相对view.py、models.py和index.html的代码:

views.py models.py index.html
如果您在浏览器中提交表单,通常的工作流是:

通过GET请求浏览到URL 浏览器通过POST请求提交数据,因为表单具有action=POST 如果表单有效,则会将您重定向到成功的URL 因此,您不应该询问如何将所有请求更改为POST,而应该让代码为初始GET请求工作。您可以通过在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()
        ...

您可能会发现阅读Django文档非常有用。示例视图与您尝试执行的操作非常相似。

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

通过GET请求浏览到URL 浏览器通过POST请求提交数据,因为表单具有action=POST 如果表单有效,则会将您重定向到成功的URL 因此,您不应该询问如何将所有请求更改为POST,而应该让代码为初始GET请求工作。您可以通过在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()
        ...

您可能会发现阅读Django文档非常有用。示例视图与您尝试执行的操作非常相似。

您的问题不在于请求方法。显然,您首先需要执行GET以呈现表单,以便发布表单,但由于您没有绑定名称表单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()

您的问题不在于request方法,显然您首先需要执行GET来呈现表单,以便发布表单,但由于您没有绑定名称表单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()

天哪,我在错误的if语句中添加了else子句。。。太简单了谢谢你帮助我认识到我的错误!我目前正在阅读表单上的文档,但我认为我决定切掉并更改给定的示例以符合我自己的目标,而不是完成基本示例,这导致了像这样愚蠢的逻辑错误..天哪,我在错误的if语句中添加了else子句。。。太简单了谢谢你帮助我认识到我的错误!我目前正在阅读表单上的文档,但我认为我决定切掉并更改给定的示例以符合我自己的目标,而不是完成基本示例,这导致了类似这样愚蠢的逻辑错误。。
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/')       
    else:
        form = ImageUploadForm()
    images = Image.objects.all()
    return render_to_response('index.html',
                              {'images': images, 'form': form},
                              context_instance=RequestContext(request))
    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()
        ...
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()