2

i'm new in development using django, and i'm trying modify an Openstack Horizon Dashboard aplication (based on django aplication).

I implements one function and now, i'm trying to do a form, but i'm having some problems with the request.

In my code i'm using the method POST

Firstly, i'm want to show in the same view what is on the form, and i'm doing like this.

from django import http
from django.utils.translation import ugettext_lazy as _
from django.views.generic import TemplateView
from django import forms


class TesteForm(forms.Form):
    name = forms.CharField()

class IndexView(TemplateView):
    template_name = 'visualizations/validar/index.html'

    def get_context_data(request):
        if request.POST:
            form = TesteForm(request.POST)
            if form.is_valid():
                instance = form.save()
        else :
            form = TesteForm()
        return {'form':form}

class IndexView2(TemplateView):
    template_name = 'visualizations/validar/index.html'

    def get_context_data(request):
        text = None
        if request.POST:
        form = TesteForm(request.POST)
            if form.is_valid():
                text = form.cleaned_data['name']
    else:
        form = TesteForm()
        return {'text':text,'form':form}   

My urls.py file is like this

from django.conf.urls.defaults import patterns, url
from .views import IndexView
from .views import IndexView2

urlpatterns = patterns('',
    url(r'^$',IndexView.as_view(), name='index'),
    url(r'teste/',IndexView2.as_view()),
)

and my template is like this

{% block main %}
<form action="teste/" method="POST">{% csrf_token %}{{ form.as_p }}
<input type="submit" name="OK"/>
</form>
<p>{{ texto }}</p>
{% endblock %}

I search about this on django's docs, but the django's examples aren't clear and the django's aplication just use methods, the Horizon Dashboard use class (how is in my code above)

When i execute this, an error message appears.

this message says:

AttributeError at /visualizations/validar/
'IndexView' object has no attribute 'POST'
Request Method: GET
Request URL:    http://127.0.0.1:8000/visualizations/validar/
Django Version: 1.4.5
Exception Type: AttributeError
Exception Value:'IndexView' object has no attribute 'POST'
Exception Location:
 /home/labsc/Documentos/horizon/openstack_dashboard/dashboards/visualizations/validar/views.py in get_context_data, line 14
Python Executable:  /home/labsc/Documentos/horizon/.venv/bin/python  
Python Version: 2.7.3

i search about this error, but not found nothing.

if someone can help me, i'm thankful

4

3 回答 3

1

你的签名是错误的:

def get_context_data(request)

应该

def get_context_data(self, **kwargs):
    request = self.request

检查get_context_data和关于动态过滤的词

由于您的第一个参数是self对象,在这种情况下是request,因此您会收到错误消息。

于 2013-06-24T14:57:06.503 回答
0

默认情况下,当您尝试向其发布时,TemplateView 将返回一个不允许的方法 405。您可以为它编写自己的 post 方法:

class IndexView(TemplateView):
    template_name = 'visualizations/validar/index.html'

    def get_context_data(request):
        #define your context and return
        context = super(ContactView, self).get_context_data(**kwargs)
        #context["testing_out"] = "this is a new context var"
        return context


    def post(self, request, *args, **kwargs):
        context = self.get_context_data()
        if context["form"].is_valid:
            print 'yes done'
            #save your model
            #redirect

    return super(TemplateView, self).render_to_response(context)

如果您要从表单发布,请改用 FormView,您仍然可以通过覆盖 get_context_data 来定义您希望的上下文:

从 django.views.generic 导入 TemplateView、FormView

从表单导入 ContactUsEmailForm

class ContactView(FormView):
    template_name = 'contact_us/contact_us.html'
    form_class = ContactUsEmailForm
    success_url = '.'

    def get_context_data(self, **kwargs):
        context = super(ContactView, self).get_context_data(**kwargs)
        #context["testing_out"] = "this is a new context var"
        return context

    def form_valid(self, form):
        # This method is called when valid form data has been POSTed.
        # It should return an HttpResponse.
        #form.send_email()
        #print "form is valid"
        return super(ContactView, self).form_valid(form)

contact_us = ContactView.as_view()

和 urls.py:

from django.conf.urls import patterns, url


urlpatterns = patterns('contact_us.views',
    url(r'^$', 'contact_us', name='contact_us'),
)

希望这会有所帮助:) 有关FormsView的更多信息。

于 2014-08-12T00:16:42.233 回答
0

如果您更仔细地阅读错误消息,则似乎该 URL 是使用GET方法检索的。不是POST

/visualizations/validar/ 处的 AttributeError
“IndexView”对象没有属性“POST”
请求方法:GET
请求网址:http://127.0.0.1:8000/visualizations/validar/

有关GET 与 POST的深入解释,请参阅以下链接

于 2013-06-24T14:56:05.723 回答