AndyFry
AndyFry

Reputation: 65

Django passing data from one form to another

I might be going about this all wrong but I'm trying to get a filterset passed between forms in Django and not getting anywhere fast. The objective is to filter objects in my model and then be able to send an email to each of the objects owners. The filter is working and presenting a subset of records as expected. How do I pass that subset to another view.

This is an extract of what I have so far...

urls.py

from django.urls import path
from . import views
from blah.views import BlurbListView, SearchResultsView
from blah.filters import BlurbFilter
from django_filters.views import FilterView
from django.urls import path
urlpatterns = [
    path('', views.index, name='index'),
    path('filter/', views.filter, name='filter'),
    path('contact_form/', views.contact_form, name='contact_form'),
]

views.py

def filter(request):
    blurb_list = Blah.objects.all().order_by('Name')
    blurb_filter = BlurbFilter(request.GET, queryset=blurb_list)
    context = {
        'filter': blurb_filter,
        'blurb_list': blurb_list,
    }

    return render(request,'blah/filter_list.html', context)


def contact_form(request):
    blurb_list = request.POST.get('blurb_list')
    blurb_filter = BlurbFilter(request.GET, queryset=blurb_list)
    for blurb in blurb_list:
        print(blurb)
    return render(request, 'blah/contact_form.html', { 'blurb_list': blurb_list })
import django_filters
from django_filters import DateRangeFilter, DateFilter, DateTimeFromToRangeFilter
from django_filters.widgets import RangeWidget
from .models import Blah
from django import forms

class BlurbFilter(django_filters.FilterSet):
    Name = django_filters.CharFilter(lookup_expr='icontains')
    IPADDR = django_filters.CharFilter(lookup_expr='istartswith')
    ContactEmail = django_filters.CharFilter(lookup_expr='icontains')
    class Meta:
        model = Blah
        fields = ['Name',
            'IPADDR',
            'ContactEmail',
        ]

filter_list.html

{% load render_table from django_tables2 %}
{% load widget_tweaks %}
{% load static %}
<div class="container-fluid" role="main">
        <div class="page-header">
                <h1>Filter Blurbs</h1>
        </div>
    {% block content %}
    <form method="post" novalidate action="{% url 'contact_form' %}">
        {% csrf_token %}
        <input type="hidden" name="blurb_list" value="{{ filter }}">
        <button type="submit" value="Submit" class="btn btn-primary">Contact Owners</button>
    </form>
        <form method="get" autocomplete="off" >
        {% if request.GET.output_type != "ansible" %}
          <input type="radio" id="list" name="output_type" value="list" checked>
        {% else %}
          <input type="radio" id="list" name="output_type" value="list">
        {% endif %}
        <label for="list">List</label>
        {% if request.GET.output_type == "ansible" %}
        <input type="radio" id="ansible" name="output_type" value="ansible" checked>
        {% else %}
        <input type="radio" id="ansible" name="output_type" value="ansible">
        {% endif %}
        <label for="ansible">Ansible Inventory</label>
        <button type="submit" value="Submit" class="btn btn-primary">Filter</button>
        <a href="{% url 'filter' %}" class="btn btn-primary">Reset</a>
        <div class="container-fluid">
            <div class="row ">
                <div class="col-sm">
                        <label for="id_Name">Blurb Name contains:</label>
                        {% render_field filter.form.Name class="form-control" %}
                </div>
                <div class="col-sm">
                        {{ filter.form.IPADDR.label_tag }}
                        {% render_field filter.form.IPADDR class="form-control" %}
                </div>
                <div class="col-sm">
                    {{ filter.form.ContactEmail.label_tag }}
                    {% render_field filter.form.ContactEmail class="form-control" %}
                </div>
            </div>
        </div>
        </form>
  {% if request.GET.output_type != "ansible" %}
  <div class="table">
    <table class="table w-auto small table-hover">
        <thead>
            <tr>
                <th>Blurb Name</th>
                <th>IP Address</th>
                <th>Owner</th>
                <th>Gather Time</th>
                <th>Last Updated</th>
            </tr>
        </thead>
        <tbody>
            {% for blurb in filter.qs %}
            <tr>
                <td><a href="/sysinfo/blurbdetail/{{ blurb.id }}/">{{ blurb.Name }}</a></td>
                <td>{{ blurb.IPADDR }}</td>
                <td>{{ blurb.ContactEmail }}</td>
                <td>{{ blurb.GatherTime|date:"d/m/y H:i:s (e)"}}</td>
                <td>{{ blurb.UpdatedTime|date:"d/m/y H:i:s (e)"}}</td>
            </tr>
            {% empty %}
            <tr>
                <td class="table-danger" colspan="18" align="center">No Blurbs matching filter</td>
            </tr>
            {% endfor %}
        </tbody>
        </table>
    </div>
  {% else %}
  [filtered-hosts]<br>
  {% for blurb in filter.qs %}
  {{ blurb.Name }} ansible_host={{blurb.Name }} ansible_port=22<br>
  {% endfor %}
  {% endif %}
        {% endblock %}
</div>
</body>
</html>

contact_form.html

{% include "./base.html" %}
{% load render_table from django_tables2 %}
{% load static %}
{% load widget_tweaks %}
<div class="container-fluid" role="main">
    <div class="page-header">
        <h1>Contact Form</h1>
    </div>
    {% block content %}
    <ul>
        {{ blurb_list }}
    {% for blurb in  blurb_list %}
    <li>{{ blurb }} - {{ blurb.Blah }} - {{ blurb.ContactEmail }}
    {% endfor %}
</ul>
    {% endblock %}
</div>
</body>
</html>

Upvotes: 1

Views: 1907

Answers (2)

R. Steigmeier
R. Steigmeier

Reputation: 402

There are several approaches but I guess the simplest would be to handle the email sending directly in the view (filter). As a side note, use CBV.

Another would be to store the data in the DB as @Chymdy already suggested.

Or if you really have to pass data around then have a look at Sessions https://docs.djangoproject.com/en/3.2/topics/http/sessions/.

The last one that comes up to my mind would be to use Celery. Write an email-sendig-function there an pass the filtered data to that function. Here you need to know that you can't pass model instances to Celery directly. Means that you will need to provide a list with for example the primary keys to the celery function and then load the objects there.

Upvotes: 1

Chymdy
Chymdy

Reputation: 660

You can simply mark em. It's not the best way but yes, it'll work.

Upvotes: 0

Related Questions