seh
seh

Reputation: 15

Display a form and the output on the same page

This code will display the form. I can input data, submit the data and the data then displays along with the previous input data from the mySQL DB table where the dat is written, but when the data displays the input form goes away (all expect the submit button that is). I've come across this subject here, but could never quite find the answer that worked for me.

**`models.py`**

class UnitPool(models.Model):

    # rack = models.ForeignKey(Rack)
    # platform = models.ForeignKey(Group)
    location = models.CharField(max_length=10, choices=LAB, default='Choose', blank=False)
    rack = models.CharField(max_length=10, choices=RACKS, default='Choose', blank=False)
    platform = models.CharField(max_length=10, choices = PLATFORM, default='Choose',blank=False)
    unit_HW_Serial = models.CharField(max_length=20, blank=False, null=False)
    unit_SW_Serial = models.CharField(max_length=20, blank=False, null=False)
    unit_SKU = models.CharField(max_length=20, blank=False, null=False)
    comments = models.CharField(max_length=64, blank=True, null=True, default='')


    def __unicode__(self):              # __unicode__ on Python 2
        return '%s %s %s %s %s %s' % (self.rack,
                                         self.platform,
                                         self.unit_HW_Serial,
                                         self.unit_SW_Serial,
                                         self.unit_SKU,
                                         self.comments)

class UUTForm(ModelForm):
    class Meta:
        model = UnitPool
        widgets = {
            'comments': TextInput(attrs={'size': 10}),
        }
        fields = ['location','rack', 'platform','unit_HW_Serial','unit_SW_Serial','unit_SKU','comments']

**forms.html**

{% extends "base.html" %}
{% load crispy_forms_tags %}

{% block content %}

<div class="container">
<div class='row'>
<div class='col-md-2'>


{% if title %}
<h1 class='{% if title_align_center %}text-align-center{% endif %}'>{{ title }}</h1>
{% endif %}


<form method='POST' action=''>{% csrf_token %}

{{ form|crispy }}

<input class='btn btn-primary' type='submit' value='Add Unit' />
</form>

</div>
</div>
</div>

{% if queryset %}
{% if rack != '' %}
    <div class="container">
        <div class="row">
        <div class='col-md-8 col-md-offset-3'>
        <h1>Unit Data Entered</h1>
        <table class='table'>
            <td><b>Item</b></td>
            <td><b>Location</b></td>
            <td><b>Rack#</b></td>
            <td><b>Platform</b></td>
            <td><b>HW SN</b></td>
            <td><b>SW SN</b></td>
            <td><b>SKU</b></td>
            <td><b>Comment</b></td>

            {% for instance in queryset %}
                <tr>
                    <td>{{ forloop.counter }}</td>
                    <td>{{ instance.location }}</td>
                    <td>{{ instance.rack }}</td>
                    <td>{{ instance.platform }}</td>
                    <td>{{ instance.unit_HW_Serial }}</td>
                    <td>{{ instance.unit_SW_Serial }}</td>
                    <td>{{ instance.unit_SKU }}</td>
                    <td>{{ instance.comments }}</td>
                </tr>
            {% endfor %}
        </table>
        </div>
        </div>
    </div>
{% endif %}
{% endif %}
{% endblock %}

**views.py**

from django.conf import settings
from django.shortcuts import render

from .models import UnitPool, UUTForm

def labunits(request):
    title = 'Enter Info'
    form = UUTForm(request.POST or None)
    context = {
      "title": title,
      "form": form
    }

    if form.is_valid():
        instance = form.save(commit=False)
        instance.save()


        queryset = UnitPool.objects.all().order_by('rack','platform') 
        context = {
            "queryset": queryset
        }

    return render(request, "labunits/forms.html", context)

Upvotes: 1

Views: 1679

Answers (1)

Rahul Gupta
Rahul Gupta

Reputation: 47846

You need to pass the form in the context after calling .is_valid() as @Daniel also mentioned.

Since you are not passing the form in the context again after calling the .is_valid() function, the form does not get displayed again in the template.

So, when you are resetting the context, you need to pass the form also.

def labunits(request):
    title = 'Enter Info'
    form = UUTForm(request.POST or None)
    context = {
      "title": title,
      "form": form
    }

    if form.is_valid():
        instance = form.save(commit=False)
        instance.save()


        queryset = UnitPool.objects.all().order_by('rack','platform') 
        context = {
            "queryset": queryset,
            "form" : form # pass the form in the context
        }

    return render(request, "labunits/forms.html", context)

Upvotes: 1

Related Questions