1

我正在添加一个新库:Django_countries在我的 Django 项目上,我的表单有点问题。

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

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

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

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

这是我的forms.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()}

我的表格看起来像:

在此处输入图像描述

编辑 :

我的 HTML 模板对应 BirthCertificate Form :

{% 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 %}

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

在此处输入图像描述

4

0 回答 0