我正在尝试将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方法,以便我可以将结果打印在quotes.html中
答案 0 :(得分:0)
我解决了。
在 views.py 中,将列表视图中的 form_Valid 方法替换为ListView中的 get_query 方法。 使用 self.request.GET(),我可以获取信息并将其放入自定义quot方法中。 然后,我将过滤后的信息与结果一起返回。
clc;
theta_1 = 90;
theta_4 = 180;
theta_5 = 90;
R_1 = 13.336;
R_2 = 45.610;
R_3 = 64.221;
R_4 = 60.354;
R_5 = 21.967;
syms theta_2 theta_3
left = R_1*exp(1i*pi/180*theta_1)+R_2*exp(1i*pi/180*theta_2);
right = R_5*exp(1i*pi/180*theta_5)+R_4*exp(1i*pi/180*theta_4)+R_3*exp(1i*pi/180*theta_3);
eqn = left==right;
sol = solve(eqn,theta_2,theta_3);
theta2 = sol.theta_2
theta3 = sol.theta_3
在 home.html 中必须将POST方法更改为 GET 方法,因为列表视图不允许POST。 在 动作中添加到ListView模板中,我以get_queryset()方法中的self.request.GET将数据以表格形式发送给Takeit。
from django.shortcuts import render
from django.views.generic.edit import FormView
from django.views.generic.list import ListView
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'
class QuoteListView(ListView):
model = SmgQuotesTable
def get_queryset(self):
r_get = self.request.GET
d_get = {'name': None , 'email':None , 'couple': None, 'age': None, 'kids':None ,}
for value in d_get:
d_get[value] = r_get[value]
query_filter = Quotes().planSelector(d_get['couple'], d_get['kids'], d_get['age'])
queryset = super(QuoteListView, self).get_queryset().filter(composite=query_filter)
return queryset
最后,我使用object_list和模型的属性将信息放入 smgquotestable_list 中。
{% extends 'core/base.html' %}
{% block title %}Cotizador{% endblock %}
{% load static %}
{% block headers %}
<h1>Cotizador</h1>
<span class="subheading">Cotiza tu plan!</span>
{% endblock %}
{% block content %}
<style>label{display:none}</style>
<form method="get" action="{% url 'quotes-list' %}">{% csrf_token %}
{{ form.as_p }}
<input type="submit" class="btn btn-primary btn-block py-2" value="Cotizar">
</form>
{% endblock %}