Регистрация данных по множественному выбору django
Я новичок и я пытался создать форму зарегистрированных данных в базе данных, все поля зарегистрированы хорошо, вместо полей множественного выбора.
from.py
class SavePost(forms.ModelForm):
user = forms.IntegerField(help_text = "User Field is required.")
title = forms.CharField(max_length=250,help_text = "Title Field is required.")
description = forms.Textarea()
dep = forms.Textarea()
class Meta:
model= Post
fields = ('user','title','description','file_path', 'file_path2', 'dep')
HTML
<div class="form-group mb-3 ">
<label for="exampleFormControlSelect2"> multiple select</label>
<select multiple class="form-control" id="dep" value={{ post.title }}>
<option>Process</option>
<option>Product</option>
<option>Equipment</option>
</select>
</div>
<div class="form-group mb-3 ">
<label for="title" class="control-label">Title</label>
<input type="text" class="form-control rounded-0" id="title" name="title" value="{{ post.title }}" required>
</div>
<div class="form-group mb-3">
<label for="description" class="control-label">Description</label>
<textarea class="form-control rounded-0" name="description" id="description" rows="5" required>{{ post.description }}</textarea>
спасибо заранее
Вам нужно поле выбора, напишите его следующим образом,
class SavePost(forms.ModelForm):
CHOICES = [('Process', 'Process'), ('Product', 'Product'),
('Equipment', 'Equipment')]
dep = forms.ChoiceField(choices=CHOICES)
user = forms.IntegerField(help_text = "User Field is required.")
title = forms.CharField(max_length=250,help_text = "Title Field is required.")
description = forms.Textarea()
class Meta:
model= Post
fields = ('user','title','description','file_path', 'file_path2', 'dep')
Ознакомьтесь с документацией Django
Вам не нужно будет создавать select в шаблоне. Я полагаю, что вы вызываете for "post" из view.py. Поэтому вам просто нужно будет создать поле в шаблоне с {{post.dep}}.