これは7時間以上の成功を収めています。今は私は理由を知らない。Django:オプションメニューからあらかじめ選択することはできません。POST時に
私は2つのフォームを持っています。this screenshotは、私が探しているフローのかなり良い指標を与えるはずです。
基本的に、ユーザーはBottleFormのドロップダウンメニュー(スクリーンショットに「1」としてマークされています)から既存の項目の1つを選択するか、Addボタンをクリックして、別のフォームBrandFormのモーダルを開きます(スクリーンショットで「2」とマークされています)、新しいブランド名を記入する必要があります。
私が欲しいのは、新しいブランド名がBrandFormで送信され、ページが再読み込みされると、そのブランドは既にドロップダウンメニューであらかじめ選択されているということです。無数のスレッドを読んだことがありますが、フィールドワークでinitial
を設定することに関する提案された答えはありません。
なぜ機能しないのですか?
# Add brand form
elif 'add_brand' in request.POST:
brand_name = request.POST['name'] # this is the brand name we submitted
context['form'] = BottleForm(initial={'brand': brand_name})
context['form2'] = BrandForm(request.POST)
the_biz = Business.objects.filter(owner=user.id).first()
if context['form2'].is_valid():
print("brand for valid!")
brand = context['form2'].save(commit=False)
brand.business = the_biz
brand.save()
return render(request, template_name="index.pug", context=context)
bottle_form.pug:
//- Create New Brand Modal
div.modal.fade.create-new-brand(tabindex="-1" role="dialog")
div.modal-dialog.modal-sm(role="document")
div.modal-content
div.modal-header
H3 Enter the Brand's name here:
div.modal-body
form.form-horizontal(method="post" action=".")
| {% csrf_token %}
| {{ form2.as_p }}
hr
btn.btn.btn-default(type="button" data-dismiss="modal") Close
input.btn.btn-primary(type="submit" value="submit")(name="add_brand") Add Brand
models.py:
class Brand(models.Model):
name = models.CharField(max_length=150, blank=False)
business = models.ForeignKey(Business, on_delete=models.CASCADE, related_name="brands")
permissions = (
('view_brand', 'View the brand'),
('del_brand', 'Can delete a brand'),
('change_brand', 'Can change a brand\'s name'),
('create_brand', 'Can create a brand'),
)
def __str__(self):
return self.name
class Bottle(models.Model):
name = models.CharField(max_length=150, blank=False, default="")
brand = models.ForeignKey(Brand, on_delete=models.CASCADE, related_name="bottles")
vintage = models.IntegerField('vintage', choices=YEAR_CHOICES, default=datetime.datetime.now().year)
capacity = models.IntegerField(default=750,
validators=[MaxValueValidator(2000, message="Must be less than 2000")
,MinValueValidator(50, message="Must be more than 50")])
slug = models.SlugField(max_length=550, default="")
@property
def age(self):
this_year = datetime.datetime.now().year
return this_year - self.vintage
permissions = (
('view_bottle', 'View the bottle'),
('del_bottle', 'Can delete a bottle'),
('change_bottle', 'Can change a bottle\'s name'),
('create_bottle', 'Can create a bottle'),
)
def __str__(self):
return self.name + " " + self.brand.name + " " + str(self.capacity) + " " + str(self.vintage)
def save(self, *args, **kwargs):
# if there's no slug, add it:
if self.slug == "":
self.slug = str(slugify(str(self.name)) + slugify(str(self.brand)) + str(self.vintage) + str(self.capacity))
super(Bottle, self).save(*args, **kwargs)
views.py(add_brand
はBrandFormのための提出の名前です)
詳細が必要な場合は、私にお知らせください。これは私を夢中にさせている。
私はDjangoがどのようにドロップダウンメニューとしてオプションを読み込んでいるのか分かりません。