下拉列表html代码

时间:2018-08-02 12:19:11

标签: python html django python-3.x django-models

我想显示某些字段的下拉选项。我有一个模型工作单,并创建了一个模型表单。我创建了一个文件choices.py,并在模型中以及在forms.py中定义了创建的选择,但是我不知道显示选择的html代码。我是一个初学者,因此不胜感激。这是我的代码:我要添加下拉列表的字段是WO_Type_ID。

models.py

from __future__ import unicode_literals
from django.db import models
from django.contrib.gis.db import models
from multiselectfield import MultiSelectField
from MMS.choices import *


class Workorder(models.Model): 
    WO_ID = models.BigIntegerField(primary_key=True)
    WO_DateDefWO = models.DateField()
    WO_DateSched = models.DateField(blank=True, null=True)
    WO_DateFinished = models.DateField(blank=True, null=True)
    WO_ST_ID_Sign = models.BigIntegerField(blank=True, null=True)
    WO_Status_ID = models.BigIntegerField(blank=True, null=True)
    WO_Type_ID = models.BigIntegerField(blank=True, null=True, default=1, choices=TYPE_CHOICES)
    WO_Comments = models.CharField(max_length=254, blank=True, null=True)
    WO_Nav_ID = models.BigIntegerField(blank=True, null=True)
    WO_Nav_Kons_ID = models.CharField(max_length=12, blank=True, null=True)
    WO_Nav_Name = models.CharField(max_length=254, blank=True, null=True)
    WO_Nav_CustAdr = models.CharField(max_length=254, blank=True, null=True)
    WO_Nav_Debt = models.FloatField(blank=True, null=True)
    WO_Nav_PropCode = models.CharField(max_length=254, blank=True, null=True)
    WO_Nav_DepCode = models.CharField(max_length=254, blank=True, null=True)
    WO_Nav_PhoneNo = models.CharField(max_length=254, blank=True, null=True)
    WO_Nav_ReasonCompl = models.CharField(max_length=254, blank=True, null=True)
    WO_NightShift = models.BooleanField(default=False)
    WO_Priority = models.BigIntegerField(blank=True, null=True)
    WO_RE_ID = models.BigIntegerField(blank=True, null=True)
    WO_MapUrl = models.CharField(max_length=254, blank=True, null=True)

    def __unicode__(self):
        return self.WO_ID

views.py

from django.shortcuts import render, get_object_or_404, redirect
from django import forms
from django.views.generic import TemplateView
from django.core.serializers import serialize
from django.http import HttpResponse
from MMS.models import Workorder
from MMS import forms
from MMS.forms import CreateWorkorder
from django.contrib.auth.forms import UserCreationForm, AuthenticationForm
from . import forms



def workorder_create(request):
    if request.method == "POST":
        form = CreateWorkorder(request.POST)
        if form.is_valid():
            CreateWorkorder_item = form.save(commit=False)
            CreateWorkorder_item.save()
            return redirect('/workorder/' + str(CreateWorkorder_item.WO_ID) + '/')
    else:
        form = CreateWorkorder()
    return render(request, 'workorders/workorder_create.html', {'form': form})


def workorder_edit(request, id=None):
    item = get_object_or_404(Workorder, WO_ID=id)
    form = CreateWorkorder(request.POST or None, instance=item)
    if form.is_valid():
        form.save()
        return redirect('/workorder/' + str(item.WO_ID) + '/')
    return render(request, 'workorders/workorder_create.html', {'form': form})


def workorder(request, id):
    workorder = Workorder.objects.get(WO_ID=id)
    return render(request, 'workorders/workorder.html', {'workorder': workorder})

forms.py

from django.forms import ModelForm
from django import forms
from MMS import models
from MMS.choices import *

class CreateWorkorder(forms.ModelForm):
    class Meta:
        model = models.Workorder
        fields = ['WO_ID', 'WO_DateDefWO', 'WO_Type_ID', 'WO_DateSched', 'WO_Status_ID', 'WO_Nav_ReasonCompl', 'WO_Nav_CustAdr', 'WO_Nav_Name', 'WO_Nav_PhoneNo', 'WO_MapUrl']
        widgets = {
        'WO_ID': forms.NumberInput(attrs={'class': 'myfieldclass'}),
        'WO_DateDefWO': forms.DateInput(attrs={'class': 'myfieldclass'}),
        'WO_Type_ID': forms.ChoiceField(choices = TYPE_CHOICES, label="", initial="", widget=forms.Select(), required=True),
        'WO_DateSched': forms.DateInput(attrs={'class': 'myfieldclass'}),
        'WO_Status_ID': forms.NumberInput(attrs={'class': 'myfieldclass'}),
        'WO_Nav_ReasonCompl': forms.TextInput(attrs={'class': 'myfieldclass'}),
        'WO_Nav_CustAdr': forms.TextInput(attrs={'class': 'myfieldclass'}),
        'WO_Nav_Name': forms.TextInput(attrs={'class': 'myfieldclass'}),
        'WO_Nav_PhoneNo': forms.TextInput(attrs={'class': 'myfieldclass'}),
        'WO_MapUrl': forms.TextInput(attrs={'class': 'myfieldclass'}),
        }

workorder_create.html

{% extends 'workorders/base.html' %}
{% load static %}

{% block content %}

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

<h1> Create Workorder </h1>

{{ form.non_field_errors }}

<div class="field_wrapper">

{{ form.WO_ID.errors }}

<label for="{{ form.WO_ID.id_for_label }}">Id:</label>

{{ form.WO_ID }}

</div>

<div class="field_wrapper">

{{ form.WO_DateDefWO.errors }}

<label for="{{ form.WO_DateDefWO.id_for_label }}">Defined:</label>

{{ form.WO_DateDefWO }}

</div>

<div class="field_wrapper">

{{ form.WO_Type_ID.errors }}

<label for="{{ form.WO_Type_ID.id_for_label }}">Order type:</label>

{{ form.WO_Type_ID }}

</div>

<div class="field_wrapper">

{{ form.WO_DateSched.errors }}

<label for="{{ form.WO_DateSched.id_for_label }}">Date Scheduled:</label>

{{ form.WO_DateSched }}

</div>

<div class="field_wrapper">

{{ form.WO_Status_ID.errors }}

<label for="{{ form.WO_Status_ID.id_for_label }}">Status:</label>

{{ form.WO_Status_ID }}

</div>

<div class="field_wrapper">

{{ form.WO_Nav_ReasonCompl.errors }}

<label for="{{ form.WO_Nav_ReasonCompl.id_for_label }}">Request:</label>

{{ form.WO_Nav_ReasonCompl }}

</div>

<div class="field_wrapper">

{{ form.WO_Nav_CustAdr.errors }}

<label for="{{ form.WO_Nav_CustAdr.id_for_label }}">Address:</label>

{{ form.WO_Nav_CustAdr }}

</div>

<div class="field_wrapper">

{{ form.WO_Nav_Name.errors }}

<label for="{{ form.WO_Nav_Name.id_for_label }}">Customer:</label>

{{ form.WO_Nav_Name }}

</div>

<div class="field_wrapper">

{{ form.WO_Nav_PhoneNo.errors }}

<label for="{{ form.WO_Nav_PhoneNo.id_for_label }}">Tel No:</label>

{{ form.WO_Nav_PhoneNo }}

</div>

<div class="field_wrapper">

{{ form.WO_MapUrl.errors }}

<label for="{{ form.WO_MapUrl.id_for_label }}">Map:</label>

{{ form.WO_MapUrl }}

</div>

<input type="submit" value="Save">
</form>

{% endblock %}

choices.py

TYPE_CHOICES = (
    ('1', ("(-)")),
    ('2', ("Ankesë: Bllokim i Kanalzimit")),
    ('3', ("Ankesë: Ndërprerje e furnizimit të ujit/rënia e presionit")),
    ('4', ("Ankesë: Cilësia e Ujit")),
    ('5', ("Ankesë: Të tjera")),
    ('6', ("Kërkesë : Aplikacion për lidhje të re - pëlqim")),
    ('7', ("Kërkesë : Ndërrim i ujëmatësit")),
    ('8', ("Kërkesë : Rrjedhje e ujit")),
    ('9', ("Kërkesë: Të tjera")),
    ('10', ("Kërkesë : Interne - Intervenim ne ujesjelles")),
    ('11', ("Kërkesë : Interne - Intervenim ne kanalizim")),
    ('12', ("Kërkesë : Interne - Nderrim i ujematesit")),
    ('13', ("Kërkesë : Interne - Bllombim")),
    ('14', ("Kërkesë : Interne - Kyçje/Shkyçje")),
    ('15', ("Orari i caktuar-ujë")),
    ('16', ("Orari i caktuar-kanalizim")),
    ('17', ("Orari per ndërrimin e ujëmatësave"))
)

1 个答案:

答案 0 :(得分:0)

您可以在forms.py中删除整个小部件。 您的表格应该是:

class CreateWorkorder(forms.ModelForm):
class Meta:
    model = models.Workorder
    fields = ['WO_ID', 'WO_DateDefWO', 'WO_Type_ID', 'WO_DateSched', 'WO_Status_ID', 'WO_Nav_ReasonCompl', 'WO_Nav_CustAdr', 'WO_Nav_Name', 'WO_Nav_PhoneNo', 'WO_MapUrl']

由于您在models.py中指定了选项,因此WO_Type_ID将自动呈现为html中的选择标记