简体   繁体   English

如何将数据从CBV表格发送到模板CBV?

[英]How to send data from CBV form to a template CBV?

I'm trying to send the POST data from a CBV form to other CBV. 我正在尝试将POST数据从CBV表单发送到其他CBV。

I use the valid_form method to get the info by form.cleaned_data and then i apply in this method some custom methods. 我使用valid_form方法通过form.cleaned_data获取信息,然后我在这个方法中应用一些自定义方法。 But I cant send the result to the other view. 但我无法将结果发送到另一个视图。

Also I tried put an action in the html sending to the other template and then grab the data but I cant. 另外我尝试在html中发送一个动作发送到另一个模板,然后抓取数据,但我不能。

views.py 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 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 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'),
]

I expect to grab the name, email, couple, age and kids values in the QuoteView and apply the Quotes method so i can print the result in the quotes.html 我希望在QuoteView中获取名称,电子邮件,情侣,年龄和孩子的值并应用引号方法,以便我可以在quotes.html中打印结果

I solve it. 我解决了

In views.py replace the form_Valid method in the form view with the get_query method in the ListView. views.py替换与在ListView的get_query方法的形式的视图方法form_Valid。 With the self.request.GET() I get the info and pase it to the custom quot method. 使用self.request.GET(),我获取信息并将其设置为自定义quot方法。 Then i return the filtered information with the result. 然后我返回带有结果的过滤信息。

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

In home.html have to change the POST method to a GET method, because the list view dont allow POST. home.html中必须将POST方法更改为GET方法,因为列表视图不允许POST。 Adding in the 加入 the action to the ListView template I send the data in the form to takeit with the self.request.GET in the get_queryset() method. 作用到ListView模板我的形式发送该数据与get_queryset()方法中的至self.request.GET takeit。

{% 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 %}

Finaly i put the information in the smgquotestable_list using the object_list and the properties of the model. 最后,我使用object_list和模型的属性将信息放在smgquotestable_list中

{% extends 'core/base.html' %}

{% block title %}Cotización{% endblock %}

{% load static %}


{% block headers %} 
    <h1>Cotización</h1>
    <span class="subheading">Cotización</span>
{% endblock %} 

{% block content %}
{% for item in object_list %}
<div class="table-responsive text-nowrap"></div>
  <table class="table table-striped">
      <thead>
        <tr>
          <th scope="col"></th>
          <th scope="col">SMG01</th>
          <th scope="col">SMG02</th>
          <th scope="col">SMG10</th>
          <th scope="col">SMG20</th>
          <th scope="col">SMG30</th>
          <th scope="col">SMG40</th>
          <th scope="col">SMG50</th>
          <th scope="col">SMG60</th>
          <th scope="col">SMG70</th>
        </tr>
      </thead>
      <tbody>
        <tr>
          <th scope="row">{{ item.composite }}</th>
          <td>$ {{ item.smg01 }}</td>
          <td>$ {{ item.smg02 }}</td>
          <td>$ {{ item.smg10 }}</td>
          <td>$ {{ item.smg20 }}</td>
          <td>$ {{ item.smg30 }}</td>
          <td>$ {{ item.smg40 }}</td>
          <td>$ {{ item.smg50 }}</td>
          <td>$ {{ item.smg60 }}</td>
          <td>$ {{ item.smg70 }}</td>
        </tr>
      </tbody>
  </table>
</div>
{% endfor %}
{% endblock %}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM