Python / Django:视图xxxxx没有返回HttpResponse对象。它返回了None

时间:2021-05-31 20:23:43

I have this view:

我有这个观点:

def do_some_work(request):
    if request.method == "POST":
        data = request.FILES['some_file']

        I do some work in the file

        dates_people= [list of things]
        maestros = {dict of things}
        talks_count= [same here]

        return render(request, 'wappApp/work.html', context={'dates': dates_people, 'maestros': solo_users,'talks': talks_count})

This view is called from a button click in the template

通过模板中的按钮单击调用此视图

<form  action="{% url "do_some_work" %}" method="POST"     enctype="multipart/form-data">
        <div class="form-group">
            <label for="exampleInputFile">File input</label>
            <input type="file" id="exampleInputFile" name="some_file">
        </div>
        <button type="submit" class="btn btn-default">Submit</button>
 </form>

what i am getting is, after selecting the file and clicking submit, is a browser page saying: 'This webpage is not available' and after refreshing the page i get the:

我得到的是,在选择文件并单击提交后,是一个浏览器页面说:'此网页不可用',刷新页面后我得到:

The view App.views.calculation didn't return an HttpResponse object. It returned None instead.

I tried to debug this view but can not reach it.

我试图调试此视图但无法访问它。

This is the url for this view:

这是此视图的网址:

url(r'^work/$', views.calculation, name='do_some_work'),

Any idea what is going on?. Thank you in advance

知道发生了什么事吗?先感谢您

EDIT

I don't know why is returning a NONE object if my form has a POST method and is pointig in the 'action' to my view

我不知道为什么返回一个NONE对象,如果我的表单有一个POST方法,并且在我的视图的'action'中指向

Thanks

** 2nd EDIT**

**第二次编辑**

I marked as correct the only answer just because i realized that i needed to handle the Not POST requests.

我将唯一的答案标记为正确,因为我意识到我需要处理Not POST请求。

I keep getting the none object because i do not know why the form is using a GET method

我一直得到none对象,因为我不知道为什么表单使用GET方法

EDIT 3

AS recommended by @BriceP i added the {% csrf_token %} tag to my template. Now the POST method is working.

根据@BriceP的建议,我将{%csrf_token%}标记添加到我的模板中。现在POST方法正在运行。

But apparently the file that i am uploading (.txt file) is not behaving as it is spected.

但显然我上传的文件(.txt文件)并没有像它所说的那样表现。

Python / Django:视图xxxxx没有返回HttpResponse对象。它返回了None

When i reach this line "lines = data.split" i get this error:

当我到达这行“lines = data.split”时,我收到此错误:

'InMemoryUploadedFile' object has no attribute 'split'

But if the community has a problem with me asking more things i will write another question.

但如果社区有问题,我会问更多的事情,我会写另一个问题。

Thanks

1 个解决方案

#1


2  

You should add this at the end of calculation() :

你应该在计算结束时添加它():

return HttpResponseNotAllowed(['POST'])

or redirect to the form page :

或重定向到表单页面:

return redirect('your-form-view')

Then, if you call the page, you'll either enter the if and have your data managed, or raise your exception or have your redirection.

然后,如果您调用该页面,您将输入if并管理您的数据,或者提出异常或进行重定向。

Without this, if your if doesn't validate, the default behaviour is returning None, which for some reason seems to be occuring to you.

如果没有这个,如果你的if没有验证,默认行为是返回None,由于某些原因,你似乎正在发生这种情况。

EDIT : Also, your form is missing {% csrf_token %}. But I don't know why you're not seeing a CSRF validation error. Maybe you're developping without debug = True? Anyway, a view should always return an http response, or redirect to another page, so even if none of this is your answer, you should manage the case where the method is GET.

编辑:此外,您的表单缺少{%csrf_token%}。但我不知道为什么你没有看到CSRF验证错误。也许你在没有debug = True的情况下开发?无论如何,视图应该总是返回一个http响应,或者重定向到另一个页面,所以即使这不是你的答案,你应该管理方法是GET的情况。

#1


2  

You should add this at the end of calculation() :

你应该在计算结束时添加它():

return HttpResponseNotAllowed(['POST'])

or redirect to the form page :

或重定向到表单页面:

return redirect('your-form-view')

Then, if you call the page, you'll either enter the if and have your data managed, or raise your exception or have your redirection.

然后,如果您调用该页面,您将输入if并管理您的数据,或者提出异常或进行重定向。

Without this, if your if doesn't validate, the default behaviour is returning None, which for some reason seems to be occuring to you.

如果没有这个,如果你的if没有验证,默认行为是返回None,由于某些原因,你似乎正在发生这种情况。

EDIT : Also, your form is missing {% csrf_token %}. But I don't know why you're not seeing a CSRF validation error. Maybe you're developping without debug = True? Anyway, a view should always return an http response, or redirect to another page, so even if none of this is your answer, you should manage the case where the method is GET.

编辑:此外,您的表单缺少{%csrf_token%}。但我不知道为什么你没有看到CSRF验证错误。也许你在没有debug = True的情况下开发?无论如何,视图应该总是返回一个http响应,或者重定向到另一个页面,所以即使这不是你的答案,你应该管理方法是GET的情况。