2013-05-07 36 views
0

I`ve在urls.py重定向得as_view()ClassView中的方法压制“搜索”按钮 有后如果诸如:Django的渲染不`吨得到的图

def as_view(): 
    if request.method == 'POST': 
     //sth 
    elif request.GET.get('test1') or request.GET.get('test2'): 
     //sth 
    else: 
     form = myForm() 
    return render(request, 'template.html', {'form' : form, 'arg': self.arg}) 

有奇怪的情况:在我的家用电脑上它可以工作,但在任何其他情况下都不行 详细地说,我已经得到这个文件在服务器上,我连接到服务器,切换在服务器上配置virtualenv,并使运行服务器0:端口

当我在我的家用电脑上这样做一切正常,但如果然后我连接到另一台计算机的http:// sererIP:端口,按下执行方法as_view()的“Search”按钮,在代码中找到这个“else”,然后窗体被初始化,但返回渲染器不会给我任何东西只有白页。当我检查服务器输出时,我得到了

[07/May/2013 05:54:33] "POST/HTTP/1.1" 405 0 

红色。

从笔记本电脑连接到服务器并使runserver 0:端口即使在这台笔记本电脑上也有同样的问题。我试图比从家用电脑连接测试,也得到了白页。

+4

您不应该重写'as_view()'。只能在'urls.py'中使用'get()','post()','delete()'等方法并使用'as_view() – 2013-05-07 11:23:17

回答

1

一类基于视图的as_view()不应该返回一个HTTP响应,但可调用的函数... 如果你想送出去GET请求的响应,为您的视图类添加get方法:

class MyView(View): 
    def get(request): 
     # return your http response here 

如果您想浏览Django的基于类的意见了一下,这里的a nice documentation

0

你为什么不使用Django FormView? This is the documentation

from django.views.generic.edit import FormView 

class MyFormView(FormView): 
    form_class = myForm 
    template_name = 'my_template.html' 
    success_url = '/thanks/' 

    def get_context_data(self, **kwargs): 
     #This is you GET 
     return super(MyFormView. self).get_context_data(**kwargs) 

    def form_valid(self, form): 
     #This is after the post, when the form is valid 
     return super(MyFormView, self).form_valid(form) 

    def form_invalid(self, form): 
     #This is after the post, when the form is invalid 
     return super(MyFormView, self).form_invalid(form) 

您可以使用get_succes_url()方法重定向到某处。

我希望有所帮助。