私はDjangoに悩まされています。私はこのフレームワークについてもっと学びたいと思います。私はいくつかのチュートリアル(英語とフランス語のチュートリアル)を読み、私が始めているように、私は自分が望むものを見つけません。ModelChoiceField with Django Forms
私は私のアプリのディレクトリにforms.py file
あります
#-*- coding: utf-8 -*-
from django import forms
class BirthCertificateForm (forms.Form) :
# rowid = forms.IntegerField() # rowid primary key
# numero_acte = forms.IntegerField() # Form number
date_acte = forms.DateField('%d/%m/%Y') # Form date
heure_acte = forms.TimeField('%H:%M:%S') # Form hour
nom = forms.CharField(max_length=30, label=u"Nom") # Lastname
prenom = forms.CharField(max_length=30, label=u"Prénom") # Firstname
sexe = forms. # <== I would like to have the choice between 'M' or 'F'
をそして、これは私のviews.app
ファイルです:
from django.shortcuts import render_to_response
from django.http import HttpResponse
from django.template import loader
from BirthCertificate.forms import BirthCertificateForm
# Create your views here.
def BirthCertificateAccueil(request) :
# Fonction permettant de créer la page d'accueil de la rubrique Acte de Naissance
#Cherche le fichier html accueil et le renvois
template = loader.get_template('accueil.html')
return HttpResponse(template.render(request))
def BirthCertificateCreationAccueil(request) :
# Fonction permettant de créer la page de création du formulaire de la partie : Acte de Naissance
template = loader.get_template('creation_accueil.html')
return HttpResponse(template.render(request))
def BirthCertificateForm(request) :
# Fonction permettant de créer le formulaire Acte de Naissance et le remplissage
if request.method == 'POST' :
form = BirthCertificateForm(request.POST or None)
if form.is_valid():
numero_acte = form.cleaned_data["Numero de l'acte"]
nom = form.cleaned_data['Nom']
prenom = form.cleaned_data['Prenom']
return render_to_response(request, 'birthform.html', {'form' : form})
1)私はsexeフィールドの2つのオプションの選択を持っていると思います。ユーザーはM
またはF
を選択する必要があります。私はModelChoiceField
を使用しなければならないと思うが、私はこの機能をどのように使うことができるのか分からない。
2)その他の質問:フォームを送信すると、後ろのデータベースが正しく更新されますか?ですから、rowid
をどこかに指定する必要がありますか、それとも自動的に追加されますか?
ありがとうございました!
フォームに関連するモデルがありますか?また、[Django awesome docs](https://docs.djangoproject.com/ja/1.10/)で見つけることができるほとんどすべて! –
@vishes_shellいいえ、私のmodels.pyファイルは空です。 1つ持っている必要がありますか?私が従っているチュートリアルでは、モデルファイルを指定していません。 – Deadpool