Django-app.views.add没有返回HttpResponse对象- [英] Django - app.views.add didn't return an HttpResponse object -

查看:54
本文介绍了Django-app.views.add没有返回HttpResponse对象-的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

This is my view add in the file views.py

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

This is my file urls.py in the which I define the "add" 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"中,令牌{%csrf_token%}和{{form.as_p}}检查表单是否有效

In the template "form.html" the token {% csrf_token %} and the {{form.as_p}} check that the form was valid

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

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

But, when I call the url in the browser 127:0.0.1:8000/add , I get this response or output:

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

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

I knew that all function based view receive an request object and return an HttpRespose Object, but, what is the difference between HttpResponse and render_to_response object

从这个意义上说,我不是我的问题.感谢您的帮助.

In that sense, I don't what could be my problem. Thanks for your help.

推荐答案

您的 render render_to_response 向右缩进一层.

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

By default, the view is rendered as GET (request.method=='GET'). You had return render_to_response(..) only in the POST section, so when the request processes def add (as a GET request the first time), there is no HttpResponse object returned. Hence the error.

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:块的缩进.

这篇关于Django-app.views.add没有返回HttpResponse对象-的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆