Answer the question
In order to leave comments, you need to log in
Django. How to fix an error when saving a form?
There is a page displaying data of class 'List'. On this page there is a link to another page with a form for editing this data. When trying to save this data in any form, an error pops up:
class List(models.Model):
first = models.CharField(max_length=50)
second = models.CharField(max_length=50)
patronymic = models.CharField(max_length=50)
birthday = models.DateField()
phone = models.CharField(max_length=12)
def __str__(self):
return self.second
class ListForm(forms.ModelForm):
class Meta:
model = List
fields = ('first', 'second', 'patronymic', 'birthday', 'phone',)
def about_me(request, pk):
about = get_object_or_404(List, pk=pk)
template = 'job/about_me.html'
body = {'about': about}
return render(request, template, body,)
def edit_me(request, pk):
about = get_object_or_404(List, pk=pk)
if request.method == "POST":
form = ListForm(request.POST, instance=about)
if form.is_valid():
about = form.save(commit=False)
about.save()
return redirect('about_me', pk=about.pk)
else:
form = ListForm(instance=about)
return render(request, 'job/about_form.html', {'form': form})
urlpatterns = [
url(r'^(?P<pk>[0-9]+)/edit/$', views.edit_me, name='edit_me'),
url(r'^(?P<pk>[0-9]+)/$', views.about_me, name='about_me'),
]
{% extends 'job/base.html' %}
{% block content %}
<h1>Личная информация:</h1>
<p>Имя: {{ about.first }}</p>
<hr>
<p>Фамилия: {{ about.second }}</p>
<hr>
<p>Отчество: {{ about.patronymic }}</p>
<hr>
<p>Дата рождения: {{ about.birthday }}</p>
<hr>
<p>Номер телефона: {{ about.phone }}</p>
<hr>
<a class="btn btn-default" href="{% url 'job:edit_me' pk=about.pk %}"><span class="glyphicon glyphicon-pencil">Изменить</span></a>
{% endblock %}
{% extends 'job/base.html' %}
{% block content %}
<h1>Редактирование информации о себе</h1>
<form method="POST" class="edit-me">{% csrf_token %}
{{ form.as_p }}
<button type="submit" class="save btn btn-default">Сохранить</button>
</form>
{% endblock %}
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question