我是使用django进行开发的新手,我正在尝试修改Openstack Horizon Dashboard应用程序(基于django应用程序)。
我实现了一个函数,现在,我正在尝试做一个表单,但是我遇到了一些问题。
在我的代码中,我使用POST方法
首先,我想在同一个视图中显示表单上的内容,我就是这样做的。
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}
我的urls.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()),
)
我的模板就像这样
{% block main %}
<form action="teste/" method="POST">{% csrf_token %}{{ form.as_p }}
<input type="submit" name="OK"/>
</form>
<p>{{ texto }}</p>
{% endblock %}
我在django的文档上搜索这个,但django的例子不清楚,django的应用程序只使用方法,Horizon Dashboard使用类(如何在我的代码中)
执行此操作时,会显示错误消息。
这条消息说:
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
我搜索了这个错误,但一无所获。
如果有人可以帮助我,我很感激
答案 0 :(得分:1)
你的签名错了:
def get_context_data(request)
应该是
def get_context_data(self, **kwargs):
request = self.request
检查get_context_data和dynamic filtering上的字
由于您的第一个参数是self
对象,在本例中为request
,因此您收到错误。
答案 1 :(得分:0)
如果您仔细阅读错误消息,则表明使用 GET 方法检索了该网址。不是 POST :
AttributeError at /visualizations/validar/ 'IndexView' object has no attribute 'POST' Request Method: GET Request URL: http://127.0.0.1:8000/visualizations/validar/的详细说明,请参阅以下链接
答案 2 :(得分: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。