在同一页面上显示表单和输出

时间:2015-08-31 16:51:23

标签: django

此代码将显示表单。我可以输入数据,提交数据和数据,然后显示来自写入dat的mySQL DB表中的先前输入数据,但是当数据显示输入表单消失时(所有人都希望提交按钮)。我在这里遇到过这个问题,但却找不到适合我的答案。

**`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)

1 个答案:

答案 0 :(得分:1)

您需要在调用form 后将.is_valid()传递给form,同时提及@Daniel

由于在调用.is_valid()函数后没有再次在上下文中传递form,因此表单不会再次显示在模板中。

因此,当您重置上下文时,您还需要传递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)

var newBudget = $("#newBudgetSum").val();

var currentUser = Parse.User.current();
currentUser.set("budget", newBudget);
currentUser.save(null,
{
    success: function(user) {
        alert("new name is: " + user.get("name"));
    },
    error: function(error) {
        alert("error: " + error.code +" "+error.message);
    }
});