【发布时间】:2019-04-02 01:43:29
【问题描述】:
我正在尝试将 POST 数据从 CBV 表单发送到其他 CBV。
我使用 valid_form 方法通过 form.cleaned_data 获取信息,然后在此方法中应用一些自定义方法。 但我无法将结果发送到另一个视图。
我也尝试在发送到另一个模板的 html 中放置一个动作,然后抓取数据,但我不能。
views.py
from django.shortcuts import render
from django.views.generic.edit import FormView
from django.views.generic.base import TemplateView
from .forms import QuoteForm
from .models import SmgQuotesTable
from .quotes import Quotes
class QuoteFormView(FormView):
template_name = 'core/home.html'
form_class = QuoteForm
success_url = 'quotes'
def form_valid(self, form):
name = form.cleaned_data['name']
email = form.cleaned_data['email']
couple = form.cleaned_data['couple']
age = form.cleaned_data['age']
kids = form.cleaned_data['kids']
#query_filter = Quotes().planSelector(couple, kids, age)
#obj = SmgQuotesTable.objects.filter(composite='Ind. Junior (H25)')
return super(QuoteFormView, self).form_valid(form)
class QuoteListView(ListView):
model = SmgQuotesTable
def get_queryset(self):
queryset = super(QuoteListView, self).get_queryset()
queryset = queryset #
print(queryset)
return queryset
home.html
{% block content %}
<style>label{display:none}</style>
<form method="post" action="{% url 'quotes-list' %}">{% csrf_token %}
{{ form.as_p }}
<input type="submit" class="btn btn-primary btn-block py-2" value="Cotizar">
</form>
{% endblock %}
urls.py
from django.urls import path
from .views import QuoteFormView, QuoteListView
urlpatterns = [
path('', QuoteFormView.as_view(), name='home'),
path('quotes/', QuoteListView.as_view(), name='quotes-list'),
]
我希望在 QuoteView 中获取姓名、电子邮件、夫妻、年龄和孩子的值并应用 Quotes 方法,以便我可以在 quote.html 中打印结果
【问题讨论】:
-
AttributeError: type object 'TemplateView' has no attribute 'get_queryset'
-
我将视图更改为 ListView 但现在出现以下错误:不允许的方法(POST):/quotes/ 不允许的方法:/quotes/
标签: python django django-forms django-templates django-views