Я новичок в Django и с трудом понимаю все формы Django. Я создаю форму, которая вводит пользователя. Пользователь может добавить несколько строк (javascript). У меня есть несколько вопросов.Показать значения модели в форме django (ModelChoiceField)
1. Как я могу использовать запрос для отображения параметров комбо для пользователя? В настоящее время я получаю выпадающий с объектами, а не его исходное значение
Мне нужно, чтобы отобразить значение item_combo
class NameForm(forms.Form):
# your_name = forms.CharField(label='Your name', max_length=100)
new_date = forms.DateField(initial=datetime.date.today, required=False, error_messages={'required': 'Your Name is Required'})
item_combo = forms.ModelChoiceField(Item.objects.all())
new_parc = forms.CharField(max_length=200, required=False)
new_vid = forms.CharField(max_length=50, required=False)
def get_name(request):
if request.method == 'POST':
form = NameForm(request.POST)
if form.is_valid():
data = form.cleaned_data
logger.info('FORM INFO')
logger.info('form info date is %s ', data)
new_date = form.cleaned_data['new_date']
new_parc = form.cleaned_data['new_date']
item_combo = form.cleaned_data['item_combo']
return HttpResponseRedirect('/thanks/')
else:
logger.error('Form is invalid. Errors are %s', form.errors)
else:
form = NameForm()
logger.error('Form has GET request. Errors are %s', form.errors)
return render(request, 'form.html', {'form': form})
form.html
<html>
{% load staticfiles %}
{% load static %}
{% include 'head.html' %}
<h1>New post</h1>
<form action="your-name" method="post">
{% csrf_token %}
<div class="fieldWrapper">
<label for="{{ form.new_date.id_for_label }}">Date</label>
{{ form.new_date }}
</div>
<div class="fieldWrapper">
<label for="{{ form.new_date.id_for_label }}">Voucher ID</label>
{{ form.new_vid }}
</div>
<div class="fieldWrapper">
<label for="{{ form.new_date.id_for_label }}">Particulars</label>
{{ form.new_parc }}
</div>
<div class="fieldWrapper">
<label for="{{ form.item_combo.id_for_label }}">Items</label>
{{ form.item_combo }}
</div>
<div class="fieldWrapper">
<label for="{{ form.item_combo.id_for_label }}">Burgers</label>
{{ form.burger_combo }}
</div>
<table>
<thead style="background-color:#9df0e0;color: #73879C">
<tr class="headings">
<th>
<input type="checkbox" id="check-all" class="flat">
</th>
<th class="column-title">Sr. No</th>
<th class="column-title">Code</th>
<th class="column-title">Particulars</th>
<th class="column-title">Qty</th>
<th class="column-title">Rate</th>
<th class="column-title">Amount</th>
<th class="bulk-actions" colspan="7">
<a class="antoo" style="color:#fff; font-weight:500;">Bulk
Actions (
<span class="action-cnt"> </span>) <i
class="fa fa-chevron-down"></i></a>
</th>
</tr>
</thead>
<tbody>
<tr class="even pointer" id='addr0'>
<td class="a-center ">
<input type="checkbox" class="flat" name="check0">
</td>
<td><input type="hidden" name="c0">1</td>
<td>
<select class="select2_single form-control" tabindex="-1" name="scode0">
<option value="">Select One</option>
{% for item in items %}
<option value="{{ item }}">{{ item }}</option>
{% endfor %}
</select>
</td>
<td class=" "><input type="text" name='part0' placeholder='Add particulars' class="form-control"/></td>
<td class=" "><input type="text" name='qty0' placeholder='Add Quantity ' class="form-control"/></td>
<td class=" "><input type="text" name='rate0' placeholder='Add rate' class="form-control"/></td>
<td class=" "><input type="text" name='am0' placeholder='Add amount' class="form-control"/></td>
</tr>
</tbody>
</table>
<input type="submit" value="Add more rows"/>
<input type="submit" value="Delete rows"/>
<input type="submit" value="Submit"/>
</form>
</html>