Answer the question
In order to leave comments, you need to log in
How to correctly implement record editing in django database using forms?
I'm trying to implement editing a record in a django db using a form.
I tried many different options given on the Internet, but none worked.
What is my mistake?
The class that implements the form:
class SluzebkaSnabForm(forms.ModelForm):
class Meta:
model = SluzSnab
fields = ['sls_manage1', 'sls_date1', 'sls_vyp1', 'sls_manage2', 'sls_date2', 'sls_vyp2']
widgets = {
# 'sls_date': forms.DateInput(format='%d.%m.%Y', attrs={
# 'readonly': 'readonly',
# 'class': 'sl_name',
# 'size': '50'
# }),
'sls_manage1': forms.Select(),
'sls_date1': forms.DateInput(format='%d.%m.%y'),
'sls_vyp1': forms.Select(),
'sls_manage2': forms.Select(),
'sls_date2': forms.DateInput(format='%d.%m.%y'),
'sls_vyp2': forms.Select(),
}
class SluzSnab(models.Model):
sls_date = models.DateField('Дата создания служебной записки', blank=True, null=True)
sls_no = models.CharField(max_length=20, verbose_name='Номер служебной записки')
sls_zak = models.CharField(max_length=20, verbose_name='Номер заказа')
sls_kol = models.IntegerField(verbose_name='Количество изделий')
sls_con_choice = [
('ivanov', 'Иванов'),
('petrov', 'Петров'),
]
sls_con = models.CharField(max_length=20, choices=sls_con_choice, verbose_name='Ответственный конструктор')
sls_stat = [
('vyp', 'Выполнено'),
('nevyp', 'Не выполнено'),
]
sls_ei_choice = [
('sht', 'Штуки'),
('kg', 'Килограммы'),
]
sls_meneg_choice = [
('pantushin', 'Пантюшин'),
('admin', 'Комиссаров А.В.'),
]
sls_name1 = models.CharField(max_length=500, verbose_name='Позиция1', blank=True, null=True)
sls_ed1 = models.CharField(max_length=20, choices=sls_ei_choice, verbose_name='Единицы измерения', blank=True, null=True)
sls_kol1 = models.IntegerField(verbose_name='Количество', blank=True, null=True)
sls_vsego1 = models.IntegerField(blank=True, null=True, verbose_name='Всего (на заказ)')
sls_manage1 = models.CharField(max_length=200, choices=sls_meneg_choice, verbose_name='Ответственный менеджер', blank=True, null=True)
sls_date1 = models.DateField('Дата поставки', blank=True, null=True)
sls_vyp1 = models.CharField(max_length=20, choices=sls_stat, verbose_name='Отметка о выполнении', blank=True, null=True)
sls_name2 = models.CharField(max_length=500, verbose_name='Позиция2', blank=True, null=True)
sls_ed2 = models.CharField(max_length=20, choices=sls_ei_choice, verbose_name='Единицы измерения', blank=True, null=True)
sls_kol2 = models.IntegerField(verbose_name='Количество', blank=True, null=True)
sls_vsego2 = models.IntegerField(blank=True, null=True, verbose_name='Всего (на заказ)')
sls_manage2 = models.CharField(max_length=200, choices=sls_meneg_choice, verbose_name='Ответственный менеджер', blank=True, null=True)
sls_date2 = models.DateField('Дата поставки', blank=True, null=True)
sls_vyp2 = models.CharField(max_length=20, choices=sls_stat, verbose_name='Отметка о выполнении', blank=True, null=True)
login_required(redirect_field_name='login')
def sluzebki(request):
sluz_snab = SluzSnab.objects.order_by('-sls_no')
# sluz_snab = SluzSnab.objects.order_by('-sls_no')
return render(request, 'sluzebki.html', {'sluz': sluz_snab})
@login_required(redirect_field_name='login')
def sluzwno(request, pk):
sluz = get_object_or_404(SluzSnab, pk=pk)
# sluz = SluzSnab.objects.get(id=sid)
if request.method == 'POST':
sform = SluzebkaSnabForm(request.POST, instance=sluz)
if sform.is_valid():
sluz = sform.save(commit=False)
sluz.save()
# messages.success(request, 'Настройки успешно обновлены!')
return redirect('sluzwno', pk=sluz.pk)
else:
sform = SluzebkaSnabForm(instance=sluz)
return render(request, 'sluzwno.html', {'sluz': sluz, 'sform': sform})
{% extends 'base.html' %}
{% block title %}Служебка №{{ sluz.0.sls_no }}{% endblock %}
{% block body %}
<div class="line_links">
/ <a href="{% url 'zurnaly' %}">служебное</a> / <a href="{% url 'sluzebki' %}">Служебные записки</a> /
<a href="{% url 'sluzwno' pk=sluz.pk %}">{{ sluz.sls_no }}</a>
</div>
<form action="{% url 'sluzwno' pk=sluz.pk %}" name="sluz">
{% csrf_token %}
{{ sform.non_field_errors }}
{{ sform.as_p }}
<button type="submit">Добавить запись в журнал</button>
</form>
{% endblock %}
Answer the question
In order to leave comments, you need to log in
Thank you all for your help! I found my mistake myself: it was necessary to put method="post" in the template in the tag
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question