如何在django中与表单在同一页面上呈现表单输入

时间:2017-02-01 19:29:39

标签: python html django forms python-3.6

我是使用Python / Django的新手,但我在将表单输入打印到页面底部时遇到了很多问题。理想情况下,我希望它输出类似的东西 this(修复输入下面的文字)。

这将创建输入表单的所有维修的持续列表。到目前为止,这是我一直在努力的代码,但是我无法找到很多最新的资源或示例来表达我想要实现的目标。

forms.py

from django import forms
from .models import RepairEntry


#Database tuples defining the dropdown list options.

DISTRICT = (
    ('HA', 'Hamilton'),
    ('HL', 'Holland'),
    ('OA', 'Ottawa'),
    ('OP', 'Oakridge'),
    ('SL', 'SpringLake'),
    ('SA', 'Saugatuck'),
)

CBMODEL = (
    ('DELL1', 'Dell 11 Gen 1'),
    ('DELL2', 'Dell 11 Gen 2'),
    ('LENO1', 'Lenovo x131'),
    ('LENO2', 'Lenovo 11e'),
    ('ACER1', 'Acer C720'),
    ('ACER2', 'Acer CB5-311'),
    ('SAMS1', 'Samsung XE303C12'),
)

CBPART = (
    ('LCD', 'LCD Panel'),
    ('KEYB', 'Keyboard'),
    ('PALM', 'Palmrest'),
    ('BOTC', 'Bottom Case'),
)

class RepairEntryForm(forms.ModelForm) :
    error_css_class = 'error'

    district    = forms.ChoiceField(choices=DISTRICT, required=True)
    cbmodel     = forms.ChoiceField(choices=CBMODEL, required=True)
    cbpart      = forms.ChoiceField(choices=CBPART, required=True)

    class Meta:
        model = RepairEntry
        widgets = {
            'kace': forms.TextInput(attrs={'placeholder': 'Enter KACE Ticket #'}),
        }
        fields = ('kace', 'district', 'cbmodel', 'cbpart',)

views.py

from django.shortcuts import render
from django.http import HttpResponse
from django.urls import reverse
from django.views.generic import FormView
from .models import RepairEntry
from .forms import RepairEntryForm
from . import views

# Create your views here.
def index(request):
    return HttpResponse("This is the index page.")

class RepairEntryPage(FormView) :
    model = RepairEntry
    template_name = 'repairs/repair_entry.html'
    success_url = '/repairs/repairentry/'
    form_class = RepairEntryForm
    latest_repairs = RepairEntry.objects.order_by('-add_date')

def form_valid(self, form):
    form = RepairEntryForm(request.POST)
    new_repair = form.save()
    print (new_repair)
    return render(request, 'repairs/repair_entry.html', {'form': form})

def view_entry(request):
    RepairEntryFormSet = modelformset_factory(RepairEntry, form=RepairEntryForm)
    if request.method == 'POST':
        formset = RepairEntryFormSet(request.POST)
        if formset.is_valid():
            for form in formset:
                return(RepairEntryFormSet)
    else:
        formset = RepairEntryFormSet()
    return render(request, repair_entry.html, {'formset': formset})

repair_entry.html

{% comment %}

This is the display for the 'Repair Entry' form.

URL: /repairs/repairentry/
Model: RepairEntry
ModelForm: RepairEntryForm
View: RepairEntryPage [FormView]
View Return: HttpResponse : message - "Successfully submitted repair."

{% endcomment %}

{% block content %}
<form action=" " method="post">{% csrf_token %}
    {{ formset.management_form }}
    {% for form in formset %}
        {{ form }}
    {% endfor %}
    <!-- DISTRICT -->
    <div class="fieledWrapper">
        {{ form.district.errors }}
        <label for="id_district">District</label>
        {{ form.district }}
    </div>
    <!-- CBMODEL -->
    <div class="fieledWrapper">
        {{ form.cbmodel.errors }}
        <label for="id_cbmodel">Chromebook Model</label>
        {{ form.cbmodel }}
    </div>
    <!-- CBPART -->
    <div class="fieledWrapper">
        {{ form.cbpart.errors }}
        <label for="id_cbpart">Chromebook Part</label>
        {{ form.cbpart }}
    </div>
    <!-- KACE -->
    {{ form.non_field_errors }}
    <div class="fieledWrapper">
        {{ form.kace.errors }}
        <label for="id_kace">KACE Ticket #:</label>
        {{ form.kace }}
    </div>
    <br>
    <input type="submit" value="Submit Repair">
</form>
<h2>Repairs Entered</h2>
<!-- Output of form, not sure how to code it. Do I put it in the template or the view? -->
{% endblock %}

0 个答案:

没有答案