【问题标题】:How to send data from CBV form to a template CBV?如何将数据从 CBV 表单发送到模板 CBV?
【发布时间】: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


【解决方案1】:

我解决了。

views.py 中,将表单视图中的 form_Valid 方法替换为 ListView 中的 get_query 方法。 使用 self.request.GET() 我获取信息并将其粘贴到自定义 quot 方法。 然后我返回过滤后的信息和结果。

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

home.html 中必须将 POST 方法更改为 GET 方法,因为列表视图不允许 POST。 将 action 添加到 ListView 模板中,我将表单中的数据发送到 get_queryset() 方法中的 self.request.GET 中。

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

最后,我使用 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 %}

【讨论】:

    猜你喜欢
    • 2017-08-31
    • 2015-01-16
    • 2014-01-04
    • 1970-01-01
    • 2019-06-21
    • 2016-04-07
    • 1970-01-01
    • 1970-01-01
    • 2013-09-12
    相关资源
    最近更新 更多