मेरे forms.py:एक ही टेम्पलेट पर दो अलग Django फॉर्म का उपयोग कैसे करें?
class AlertForm(forms.ModelForm):
class Meta:
model=Alert
fields = ('high','medium', 'user')
widgets = {
'user': forms.HiddenInput()
}
AlertCountFormset = modelformset_factory(Alert,
form = AlertForm)
एक और Django फार्म वर्ग:
NOTIFIER_TYPE = (
(0, _('E-mail')),
(1, _('Skype')),
(2, _('IRC'))
)
मैं एक ही पर इन दो रूपों भरना चाहते हैं:
class NotifierForm(forms.ModelForm):
high = forms.ChoiceField(choices=NOTIFIER_TYPE)
medium = forms.ChoiceField(choices=NOTIFIER_TYPE)
low = forms.ChoiceField(choices=NOTIFIER_TYPE)
def save(self, commit=True):
alert = super(NotifierForm, self).save(commit=False)
alert.high = self.cleaned_data["high"]
alert.medium = self.cleaned_data["medium"]
alert.low = self.cleaned_data["low"]
alert.save()
return alert
class Meta:
model=Notifier
fields = ('high','medium', 'low', 'user')
widgets = {
'user': forms.HiddenInput()
}
NotifierFormset = modelformset_factory(Notifier,
form = NotifierForm)
नीचे चुनाव क्षेत्रों के लिए है टेम्पलेट। तो मैं दोनों अर्थात के लिए एक ही बार देखा गया लिखने के लिए चुनें:
def profile_setting(request, slug):
if request.method == 'POST':
alert_form = AlertForm(request.POST)
notifier_form = NotifierForm(request.POST)
if alert_form.is_valid() and notifier_form.is_valid():
alert = alert_form.save(commit=False)
notifier = notifier_form.save(commit=False)
alert.user = request.user.username
notifier.user = request.user.username
notifier.save()
alert.save()
return HttpResponseRedirect(reverse('profile_setting', args=[slug]))
extra_context = {
'alert_form': AlertForm(),
'notifier_form': NotifierForm()
}
return direct_to_template(request,'users/user_profile_setting.html',
extra_context)
कि मेरे template.html में के अनुसार:
{% block content %}
<h3>{% trans "Alerts limit" %}</h3>
<form action="" method="POST">{% csrf_token %}
{{ alert_form.as_p }}
<input type="submit" value="{% trans 'Update' %}" />
</form>
<h3>{% trans "Notifier setting" %}</h3>
<form action="" method="POST">{% csrf_token %}
{{ notifier_form.as_p }}
<input type="submit" value="{% trans 'Update' %}" />
</form>
सब कुछ सही है, यह भी डेटाबेस के लिए डेटा की बचत है। लेकिन समस्या यह है कि जब भी मैं aler_form भरता हूं और अद्यतन बटोन पर क्लिक करता हूं। यह एक ही मूल्य या इसके विपरीत के साथ एक और रूप भी अद्यतन करता है। उदाहरण के लिए यदि मैं
1 2 3 for high , medium and low for alert_Form
फिर यह notify_form के लिए समान मान भी सहेजता है। ये क्यों हो रहा है। क्या यह विचारों में कुछ गलत है?
उन परिवर्तनों के अलावा। क्या मुझे टेम्पलेट पक्ष में बदलाव करना चाहिए या नहीं? –
नहीं, आपको टेम्पलेट में कोई भी परिवर्तन करने की आवश्यकता नहीं है। इसे जोड़ने के बाद – Alasdair
। कुछ नही हो रहा है। मेरा मतलब है, डेटाबेस में कोई मूल्य दर्ज नहीं है। यहां तक कि –