Django小部件不起作用:国旗

时间:2017-01-11 08:46:08

标签: python django django-models django-forms django-countries

我在我的Django项目中添加了一个新库:#-*- coding: utf-8 -*- from django.db import models from Identity.models import Country, Identity from django.utils.encoding import force_text from django_countries.fields import CountryField ###################################### # Choix à l'utilisateur pour le sexe # ###################################### SEX_CHOICES = ( ('Masculin', 'Masculin'), ('Feminin', 'Feminin') ) #################################################################################### # Création d'une table permettant de renseigner toutes les informations concernant # # l'enfant et reprise des champs pour les parents # #################################################################################### class BirthCertificate(models.Model): lastname = models.CharField(max_length=30, null=False, verbose_name='Nom de famille') firstname = models.CharField(max_length=30, null=False, verbose_name='Prénom(s)') sex = models.CharField(max_length=8, choices=SEX_CHOICES, verbose_name='Sexe') birthday = models.DateField(null=False, verbose_name='Date de naissance') birthhour = models.TimeField(null=True, verbose_name='Heure de naissance') birthcity = models.CharField(max_length=30, null=False, verbose_name='Ville de naissance') birthcountry = CountryField(blank_label='(Pays de naissance)') fk_parent1 = models.ForeignKey(Identity, related_name='ID_Parent1', verbose_name='ID parent1', null=False) fk_parent2 = models.ForeignKey(Identity, related_name='ID_Parent2', verbose_name='ID parent2', null=False) ,我的表单出现了一点问题。

我在表单中正确显示了我的国家/地区列表,但我没有在每个国家/地区旁边显示带有小部件的标记。

我正在使用此文档:https://pypi.python.org/pypi/django-countries但即使所有元素都有效,我也找不到显示国家/地区标记的方法。

这是来自BirthCertificate应用程序的 models.py 文件:

#-*- coding: utf-8 -*-

from django import forms
from BirthCertificate.models import *
from django_countries.widgets import CountrySelectWidget

class CustomLabelModelChoiceField(forms.ModelChoiceField):

    def __init__(self, *args, **kwargs):
        self._label_from_instance = kwargs.pop('label_func', force_text)
        super(CustomLabelModelChoiceField, self).__init__(*args, **kwargs)

    def label_from_instance(self, obj):
        return self._label_from_instance(obj)

class BirthCertificateForm(forms.ModelForm):
    fk_parent1 = CustomLabelModelChoiceField(Identity.objects.filter(sex = "Masculin"), required=False, label = "Père", label_func=lambda obj: '%s %s %s %s' % (obj.lastname, obj.firstname, obj.birthday, obj.birthcity))
    fk_parent2 = CustomLabelModelChoiceField(Identity.objects.filter(sex = "Feminin"), required=False, label = "Mère", label_func=lambda obj: '%s %s %s %s' % (obj.lastname, obj.firstname, obj.birthday, obj.birthcity))

    class Meta :
        model = BirthCertificate
        fields = '__all__'
        widgets = {'country': CountrySelectWidget()}

    def __init__(self, *args, **kwargs):    
        super(BirthCertificateForm, self).__init__(*args, **kwargs)
        for key, value in self.fields.iteritems() :
            self.fields[key].widget.attrs.update({'class':'form-fields'})            

class IdentityForm(forms.ModelForm) :

    class Meta :
        model = Identity
        fields = '__all__'
        widgets = {'country': CountrySelectWidget()}

这是我的 forms.py 文件:

{% extends 'Base_BirthCertificate.html' %}

{% load staticfiles %}

{% block content %}

    <!-- ############### -->
    <!-- Page principale -->
    <!-- ############### -->

    <h1 align="center"> Formulaire d'acte de naissance </h1>

    {% load bootstrap %}

    <form class = "col-sm-8" method='POST' action=''> {% csrf_token %}
        <h3> Formulaire permettant la création de l'acte de naissance</h3>
        <br></br>
        {{ Bform|bootstrap}} <!-- Display child part formulary -->
        {{ value|date:"%d/%m/%Y" }}
        {{ value|time:"H:M" }}
        <br></br>

    <input class="button" type ="submit" name="_save2" value="Valider l'acte de naissance" /> 
    <input class="button" type ="submit" name="_preview2" value="Prévisualiser l'acte de naissance" />
    </form>
    <form class = "col-sm-8" method='POST' action="{% url "BChome" %}"> {% csrf_token %}
    <input class="button" type ="submit" name="retour" value="Retour" /> 
    </form>   

{% endblock content %}

我的表单如下:

enter image description here

编辑:

我的HTML模板对应于BirthCertificate Form:

detailViewController.aboutUsDetails = [aboutUs[row]]

浏览器的控制台不显示错误:

enter image description here

0 个答案:

没有答案