Я следовал этому решению stackoverflow , чтобы использовать мастер форм, так как я впервые использую мастер форм. Весь приведенный ниже код принадлежит приложению «фонды». При попытке просмотреть форму в браузере, я получаю следующую ошибку:
KeyError at / Funds / Raise / Medical /
'0'
MODELS.py
from django.db import models
from django.contrib.auth.models import User
# Create your models here.
class Campaign(models.Model):
user = models.ForeignKey(User, on_delete=models.CASCADE)
# this is many to one relationship, on_deleting user, campaign will also be deleted
funds_for = models.CharField(max_length=200)
relationship = models.CharField(max_length=200, blank=True)
benefiary_name = models.CharField(max_length=200, blank=True)
illness = models.CharField(max_length=200, blank=True)
undergoing_treatment = models.CharField(max_length=200, blank=True)
hospital = models.CharField(max_length=200, blank=True)
campaign_title = models.CharField(max_length=200, blank=True)
amount_required = models.IntegerField(null=True, blank=True)
amount_desc = models.TextField( blank=True)
campaign_duration = models.IntegerField(null=True, blank=True)
mobile = models.CharField(max_length=10, blank=True)
campaign_image = models.ImageField(default="profilepic.jpg",upload_to="campaign_pictures")
campaign_desc = models.TextField( blank=True)
terms = models.BooleanField(default=False)
def __str__(self):
return f'{self.user.username} campaign'
FROMS.PY
from django import forms
from .models import Campaign
class RaiseFundsFrom1(forms.ModelForm):
class Meta:
model = Campaign
fields = ['funds_for','relationship','benefiary_name','illness','undergoing_treatment','hospital']
class RaiseFundsFrom2(forms.ModelForm):
class Meta:
model = Campaign
fields = ['campaign_title','amount_required','amount_desc','illness','campaign_duration','mobile']
class RaiseFundsFrom3(forms.ModelForm):
class Meta:
model = Campaign
fields = ['campaign_image']
class RaiseFundsFrom4(forms.ModelForm):
class Meta:
model = Campaign
fields = ['campaign_desc','terms']
VIEWS.PY
from django.core.files.storage import DefaultStorage
FORMS = [("form1", RaiseFundsFrom1),
("form2", RaiseFundsFrom2),
("form3", RaiseFundsFrom3),
("form4", RaiseFundsFrom4)]
TEMPLATES = {"form1": "funds/raise_funds_medical_1.html",
"form2": "funds/raise_funds_medical_2.html",
"form3": "funds/raise_funds_medical_3.html",
"form4": "funds/raise_funds_medical_4.html"}
class ContactWizard(SessionWizardView):
file_storage = DefaultStorage()
def get_template_names(self):
return TEMPLATES[self.steps.current]
def done(self, form_list, **kwargs):
print(form_list)
return HttpResponse(form_list)
URLS.PY
path('raise/medical/', views.ContactWizard.as_view([RaiseFundsFrom1,RaiseFundsFrom2,RaiseFundsFrom3,RaiseFundsFrom4]), name='medical_form'),
RAISE_FUNDS_MEDICAL_1. html
{% block content %}
{{ wizard.form.media }}
<p>Step {{ wizard.steps.step0 }} of {{ wizard.steps.count }}</p>
<form action="" method="post">{% csrf_token %}
<table>
{{ wizard.management_form }}
{% if wizard.form.forms %}
{{ wizard.form.management_form }}
{% for form in wizard.form.forms %}
{{ form }}
{% endfor %}
{% else %}
{{ wizard.form }}
{% endif %}
</table>
<input type="submit" value="{% trans "submit" %}"/>
</form>
{% endblock %}