我正在为我的Django应用程序苦苦挣扎,甚至要问我关于StackOverflow的第一个问题
简而言之,我有一个表单,用户(农民)允许他在培养基上添加植物
如果农场主不需要一个无聊的选择框,只需写下几个字母,所有相关的结果就会在屏幕上弹出,那就很方便了。农场主会捡起植物,继续下一步。因为他有330种不同的种子,这不仅仅是一个花哨的功能
我能够构建一个“简单”的向导表单,我已经有了搜索引擎,我的字段填充了modelcoocefield()。。。我觉得我离得太近了
我也认为WizardForm可能不是我所做的正确方法,但我觉得我只是错过了一些东西
你们有什么建议吗
下面,您可以阅读我的代码的一些摘录。我将尝试清理混乱,并为您提供一个可读的代码
models.py
'''
From the model, the only field that interests this question is the second one, id_graine (graine means seed).
'''
class Lot(models.Model):
id = models.AutoField(primary_key = True)
id_graine = models.ForeignKey('Graine', on_delete = models.PROTECT, related_name = 'id_graine', null = True, blank = True)
nom_culture_incubation = models.ForeignKey('Culture', on_delete = models.PROTECT, related_name = 'nom_culture_incubation', null = True, blank = True)
nom_culture = models.ForeignKey('Culture', on_delete = models.PROTECT, related_name = 'nom_culture', null = True, blank = True)
etat_lot = models.CharField('état', choices = EtatLotsChoix.choices, max_length = 50, null = True, blank = True)
quantite_lot = models.PositiveSmallIntegerField('quantité', null = True, blank = True)
semis_date = models.DateField('date de semis', null = True, blank = True)
phase_lunaire_semis = models.CharField('phase lunaire de semis', max_length = 4, blank = True)
constellation_semis = models.CharField('constellations de semis', max_length = 7, blank = True)
germination_date = models.DateField('date de germination', null = True, blank = True)
plantaison_date = models.DateField('date de plantaison', null = True, blank = True)
phase_lunaire_plantation = models.CharField('phase lunaire de plantation', choices = PhasesLunairesChoix.choices, max_length = 4, blank = True)
constellation_plantation = models.CharField('constellations de plantation', choices = ConstellationChoix.choices, max_length = 7, blank = True)
culture_introduction_date = models.DateField('date d\'introduction', null = True, blank = True)
floraison_date = models.DateField('date de floraison', null = True, blank = True)
recolte_date = models.DateField('date de récolte', null = True, blank = True)
def __int__(self):
return self.id
class Meta:
verbose_name = 'Lot'
verbose_name_plural = 'Lots'
views.py
class AjoutLotWizard(SessionWizardView):
template_name = 'wizardforms/ajout_lot_seme.html'
def get_context_data(self, form, **kwargs):
'''
I told you that I was able to provide choices through a ModelChoiceField. But this approach
didn't pay off well. I also had a successful attempt by providing a context to the template
with those informations.
'''
if self.request.GET.get('search_value') != None:
search_value = self.request.GET.get('search_value')
search_results = Graine.objects.filter(espece_graine__contains = search_value)
context = super(AjoutLotWizard, self).get_context_data(form = form, **kwargs)
if (self.steps.current == '0') & (self.request.GET.get('search_value') != None):
context.update({'search_results': search_results})
return context
else:
return context
在views.py中,我还尝试用get_form_kwargs()传递数据,get_form_step_data(),用自定义属性覆盖get()
使用get_form_kwargs的方法很有前途,因为一旦我能够将我的值存储在变量中,不幸的是,我无法在WizardView中按照我的意愿操作它们
forms.py
class AddLotSemeStep1(forms.ModelForm):
'''
This file might not be accurate. I did a mess on that part by rewriting __init__().
But from what I remember, there wasn't more within the form when I use get_context_data
to provide choices anyway with the template.
'''
class Meta:
model = Lot
fields = ['id_graine']
labels = {
'id_graine': 'Graine'
}
widgets = {
'id_graine': forms.RadioSelect()
}
template.html
{% load static %}
{% load i18n %}
{% block content %}
{% include 'components/_base.html' %}
<main class="wizard-form-main">
<h2>Ajout de lot semé - le 1</h2>
<p>Step {{ wizard.steps.step1 }} of {{ wizard.steps.count }}</p>
<form action="" method="GET" name="searchbar">
<input type="text" name="search_value">
<button type="submit">Rechercher</button>
</form>
<form action = "" method = "POST" name="wizardform">
{% csrf_token %}
<table>
{{ wizard.management_form }}
{% if wizard.form %}
{{ wizard.form.management_form }}
{{ wizard.form }}
{% else %}
{% for result in search_results %}
<p>
<label>
<input type="radio" name="chope_id" value="{{result.id}}">
{{result.espece_graine}} {{result.variete_graine}}, ({{result.provenance}} {{result.annee_de_recolte|cut:".0"}})
</label>
<input type="submit" value="Ajouter cette graine">
</p>
{% endfor %}
{% endif %}
</table>
{% if wizard.steps.prev %}
<!--<button name="wizard_goto_step" type="submit" value="{{ wizard.steps.first }}">{% trans "first step" %}</button>-->
<button name="wizard_goto_step" type="submit" value="{{ wizard.steps.prev }}">{% trans "prev step" %}</button>
{% endif %}
<input type = "submit" value = "étape suivante">
</form>
</main>
{% endblock %}
感谢您的关注,如果您设法阅读了所有内容
你们好,那个浏览过这个页面的家伙。如果你也在寻找一个多页表单,它有一个特定的步骤,比如我的搜索栏,你可以这样做
我将把我的原始代码存放在这里。这应该已经有帮助了。一旦我的Django应用程序完成,我会花时间解释
相关问题 更多 >
编程相关推荐