i'm new in development using django, and i'm trying modify an Openstack Horizon Dashboard aplication (based on django aplication).
我是使用django进行开发的新手,我正在尝试修改一个Openstack Horizon仪表板拼接(基于django拼接)。
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
在我的代码中,我使用方法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
我的url。py文件是这样的
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)
我在django的文档中搜索了这个,但是django的例子并不清楚,django的aplication只是使用方法,Horizon Dashboard使用类(在我的代码中是如何使用的)
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
如果有人能帮助我,我很感激。
3 个解决方案
#1
1
Your signature is wrong:
你的签名是错误的:
def get_context_data(request)
should be
应该是
def get_context_data(self, **kwargs):
request = self.request
Check the for get_context_data and the word on dynamic filtering
检查get_context_data和关于动态过滤的单词
Since your first argument is the self
object, which in this case is request
, you are getting the error.
由于您的第一个参数是self对象,在本例中是request,因此您将得到错误。
#2
0
If you read more carefully the error message, it appears that the URL was retrieved using a GET method. Not POST:
如果您更仔细地阅读错误消息,就会发现URL是使用GET方法检索的。不是文章:
AttributeError at /visualizations/validar/ 'IndexView' object has no attribute 'POST' Request Method: GET Request URL: http://127.0.0.1:8000/visualizations/validar/
See the following link for an in deep explanation of GET vs POST
有关GET vs POST的深入解释,请参见以下链接
#3
0
TemplateView by default will return a method not allowed 405 when you try to post to it. You can write your own post method for it:
TemplateView默认将返回一个不允许405的方法。你可以为它写你自己的邮寄方法:
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)
If you're going to post from a form, use FormView instead, and you can still define context as you wish by overwriting get_context_data:
如果要从表单中发布内容,请使用FormView,您仍然可以通过覆盖get_context_data来定义上下文:
from django.views.generic import TemplateView, FormView
从django.views。一般进口TemplateView FormView
from forms import ContactUsEmailForm
从形式进口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()
And urls.py:
和urls . py:
from django.conf.urls import patterns, url
urlpatterns = patterns('contact_us.views',
url(r'^$', 'contact_us', name='contact_us'),
)
Hope this helps :) More on FormsView.
希望这有助于:)更多关于FormsView。
#1
1
Your signature is wrong:
你的签名是错误的:
def get_context_data(request)
should be
应该是
def get_context_data(self, **kwargs):
request = self.request
Check the for get_context_data and the word on dynamic filtering
检查get_context_data和关于动态过滤的单词
Since your first argument is the self
object, which in this case is request
, you are getting the error.
由于您的第一个参数是self对象,在本例中是request,因此您将得到错误。
#2
0
If you read more carefully the error message, it appears that the URL was retrieved using a GET method. Not POST:
如果您更仔细地阅读错误消息,就会发现URL是使用GET方法检索的。不是文章:
AttributeError at /visualizations/validar/ 'IndexView' object has no attribute 'POST' Request Method: GET Request URL: http://127.0.0.1:8000/visualizations/validar/
See the following link for an in deep explanation of GET vs POST
有关GET vs POST的深入解释,请参见以下链接
#3
0
TemplateView by default will return a method not allowed 405 when you try to post to it. You can write your own post method for it:
TemplateView默认将返回一个不允许405的方法。你可以为它写你自己的邮寄方法:
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)
If you're going to post from a form, use FormView instead, and you can still define context as you wish by overwriting get_context_data:
如果要从表单中发布内容,请使用FormView,您仍然可以通过覆盖get_context_data来定义上下文:
from django.views.generic import TemplateView, FormView
从django.views。一般进口TemplateView FormView
from forms import ContactUsEmailForm
从形式进口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()
And urls.py:
和urls . py:
from django.conf.urls import patterns, url
urlpatterns = patterns('contact_us.views',
url(r'^$', 'contact_us', name='contact_us'),
)
Hope this helps :) More on FormsView.
希望这有助于:)更多关于FormsView。