如何修复 Django:没有返回 HttpResponse 对象。它返回 None 而不是? [复制]
Posted
技术标签:
【中文标题】如何修复 Django:没有返回 HttpResponse 对象。它返回 None 而不是? [复制]【英文标题】:How to fix Django : didn't return an HttpResponse object. It returned None instead? [duplicate] 【发布时间】:2019-02-15 12:19:56 【问题描述】:我使用过 python3.7 和 django 2.1,对于 django 应用程序模型和视图目录有单独的文件夹和文件。
项目名称:融合 应用名称:admin_lte出现以下错误
/csv/ 处的值错误 视图 django_adminlte.views.file_csv_view.file_csv_show.file_csv_show 没有返回 HttpResponse 对象。它返回 None 。
Traceback :
Traceback (most recent call last):
File "C:\Users\parth\Desktop\admin_lte\lib\site-packages\django\core\handlers\exception.py", line 34, in inner
response = get_response(request)
File "C:\Users\parth\Desktop\admin_lte\lib\site-packages\django\core\handlers\base.py", line 137, in _get_response
"returned None instead." % (callback.__module__, view_name)
ValueError: The view django_adminlte.views.file_csv_view.file_csv_show.file_csv_show didn't return an HttpResponse object. It returned None instead.
[15/Feb/2019 17:23:57] "POST /csv/ HTTP/1.1" 500 67126
我已经为单独的文件夹制作了所有 init.py 文件。
View file : file_csv_show.py
from django.shortcuts import render,redirect
from django_adminlte.forms import File_csvForm
from django_adminlte.models import file_csv
def file_csv_show(request):
if request.method == 'POST':
form = File_csvForm(request.POST,request.FILES) #request.POST = geting title/auth request.FILES = for book upload
if form.is_valid():
try:
form.save()
return redirect("file_csv_show")
except:
pass
else:
form = File_csvForm()
return render(request,'adminlte/file_csv_index.html','form' : form)
models file : file_csv.py
from django.db import models
# Create your models here.
class File_csv(models.Model):
title = models.CharField(max_length=50)
csv_files = models.FileField(upload_to='files/csv')
class Meta:
db_table = "csv"
def __str__(self):
return self.title
html file
% extends 'adminlte/base.html' %
% load crispy_forms_tags %
% block content %
% if user.is_authenticated %
<h2>Upload</h2>
<form method="post" enctype="multipart/form-data" action="% url "file_csv" %">
% csrf_token %
<input type="file" name="document">
<button type="submit">Upload File</button>
</form>
% if url %
<p>Uploaded File : <a href="url">url</a></p>
% endif %
% endif %
% endblock %
urls.py file
from django_adminlte import views
from django_adminlte.views.file_csv_view import file_csv_show
urlpatterns = [
path('csv/',views.file_csv_view.file_csv_show,name='file_csv'),
]
I want to when file has been uploaded and after page redirect to index page
【问题讨论】:
发布要重定向到的索引函数及其网址 @ans2human file_csv_show.py 是视图文件,包含索引函数 请不要做except: pass
。事实上,无论如何都没有理由尝试/除了那里。
【参考方案1】:
你的观点有问题。当表单无效时,它不会返回任何内容。
def file_csv_show(request):
if request.method == 'POST':
form = File_csvForm(request.POST,request.FILES) #request.POST = geting title/auth request.FILES = for book upload
if form.is_valid():
try:
form.save()
return redirect("file_csv_show")
except:
# If the form is invalid,
pass
# This part is missing:
# Here you need an else for when the form is invalid.
else:
return render(request,'adminlte/file_csv_index.html','form' : form)
else:
form = File_csvForm()
return render(request,'adminlte/file_csv_index.html','form' : form)
【讨论】:
以上是关于如何修复 Django:没有返回 HttpResponse 对象。它返回 None 而不是? [复制]的主要内容,如果未能解决你的问题,请参考以下文章
如何修复“AttributeError at /orders/create/‘tuple’对象在 Django 中没有属性‘get’
54.1 怎样才算学会django? 知道这28个知识点才算会django2