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

2024-09-29 21:23:37 发布

您现在位置:Python中文网/ 问答频道 /正文

我正试图把一个CBV表格上的POST数据发送到其他CBV。你知道吗

我使用有效的\u form方法通过form.u数据然后我在这个方法中应用了一些自定义方法。 但我无法将结果发送到其他视图。你知道吗

我也试着把一个动作在html发送到另一个模板,然后抓取数据,但我不能。你知道吗

你知道吗视图.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

你知道吗主页.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 %}

你知道吗网址.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中的name、email、couple、age和kids值,并应用Quotes方法,以便在报价.html你知道吗


Tags: django方法namefromimportselfformdata
1条回答
网友
1楼 · 发布于 2024-09-29 21:23:37

我解决了。你知道吗

视图.py将表单视图中的form\u Valid方法替换为ListView中的get\u 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

主页.html必须将POST方法更改为GET方法,因为列表视图不允许POST。 将action添加到ListView模板中,我以表单的形式发送数据,并将其与self.request.GET获取在get\u queryset()方法中。你知道吗

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

最后,我使用对象列表和模型的属性将信息放入smgquotestable\u列表中。你知道吗

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

相关问题 更多 >

    热门问题