私はdjangoを使用した開発が初めてで、Openstack Horizon Dashboardアプリケーション(djangoアプリケーションに基づく)を変更しようとしています。
私は 1 つの関数を実装し、今、フォームを作成しようとしていますが、リクエストに問題があります。
私のコードでは、メソッド 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
このエラーについて検索しましたが、何も見つかりませんでした。
誰かが私を助けることができれば、私は感謝しています