gpt4 book ai didi

python - Django - app.views.add 没有返回 HttpResponse 对象 -

转载 作者:太空宇宙 更新时间:2023-11-03 18:49:08 26 4
gpt4 key购买 nike

这是我在文件views.py中添加的 View

def add(request):
#se usaran las categorias para esto,se pasaran en locals()
categorias = Categoria.objects.all()
if request.method == "POST":
form = EnlaceForm(request.POST)
if form is valid():
form.save()
return HttpResponseRedirect("/")
else:
form = EnlaceForm()

template = "form.html"
return render_to_response(template,
context_instance = RequestContext(request,locals()))

这是我的文件 urls.py,我在其中定义了“添加”url

from django.conf.urls import patterns, include, url
from django.contrib import admin
admin.autodiscover()

urlpatterns = patterns('',
# Examples:
#url(r'^$', 'app.views.hora_actual', name='hora_actual'),
url(r'^$', 'app.views.home', name='home'),
url(r'^minus/(\d+)$', 'app.views.minus', name='minus'),
url(r'^plus/(\d+)$', 'app.views.plus', name='plus'),
url(r'^categoria/(\d+)$', 'app.views.categoria', name='categoria'),
url(r'^add/$','app.views.add', name="add"),

# url(r'^proyecto2/', include('proyecto2.foo.urls')),

# Uncomment the admin/doc line below to enable admin documentation:
url(r'^admin/doc/', include('django.contrib.admindocs.urls')),

# Uncomment the next line to enable the admin:
url(r'^admin/', include(admin.site.urls)),

)

在模板“form.html”中, token {% csrf_token %} 和 {{form.as_p}} 检查表单是否有效

<section id="contenido">
<form method="post">
<div id="form">
{% csrf_token %}
{{form.as_p}}
<input type="submit">
</form>
</div>
</section>

但是,当我在浏览器中调用 url 127:0.0.1:8000/add 时,我得到以下响应或输出:

ValueError at /add/

The view app.views.add didn't return an HttpResponse object.
Request Method: GET
Request URL: http://127.0.0.1:8000/add/
Django Version: 1.5.2
Exception Type: ValueError
Exception Value:
The view app.views.add didn't return an HttpResponse object.
Exception Location: C:\Python27\lib\site-packages\django\core\handlers\base.py in get_response, line 133
Python Executable: C:\Python27\python.exe
Python Version: 2.7.5
Python Path:
['C:\\django-projects\\tests\\proyecto2',
'C:\\Python27\\lib\\site-packages\\setuptools-0.9.6-py2.7.egg',
'C:\\Python27\\python27.zip',
'C:\\Python27\\DLLs',
'C:\\Python27\\lib',
'C:\\Python27\\lib\\plat-win',
'C:\\Python27\\lib\\lib-tk',
'C:\\Python27',
'C:\\Python27\\lib\\site-packages',
'C:\\Python27\\lib\\site-packages\\win32',
'C:\\Python27\\lib\\site-packages\\win32\\lib',
'C:\\Python27\\lib\\site-packages\\Pythonwin',
'C:\\Python27\\lib\\site-packages\\setuptools-0.6c11-py2.7.egg-info']
Server time: Thu, 12 Sep 2013 13:16:44 -0500

Traceback Switch to copy-and-paste view

C:\Python27\lib\site-packages\django\core\handlers\base.py in get_response
raise ValueError("The view %s.%s didn't return an HttpResponse object." % (callback.__module__, view_name)) ...
▶ Local vars

我知道所有基于函数的 View 都会接收一个请求对象并返回一个 HttpRespose 对象,但是,HttpResponse 和 render_to_response 对象之间有什么区别

从这个意义上说,我不知道我的问题是什么。感谢您的帮助。

最佳答案

您的渲染 render_to_response 向右缩进一级。

默认情况下, View 呈现为 GET (request.method=='GET')。您仅在 POST 部分中 return render_to_response(..) ,因此当请求处理 def add 时(第一次作为 GET 请求) ),没有返回 HttpResponse 对象。因此出现了错误。

def add(request):
#se usaran las categorias para esto,se pasaran en locals()
categorias = Categoria.objects.all()
if request.method == "POST":
form = EnlaceForm(request.POST)
if form is valid():
form.save()
return HttpResponseRedirect("/")
else:
form = EnlaceForm()

template = "form.html"
return render_to_response(template,
context_instance = RequestContext(request,locals()))

还要注意 else: block 的缩进。

关于python - Django - app.views.add 没有返回 HttpResponse 对象 -,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18771705/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com