Answer the question
In order to leave comments, you need to log in
How to update a Django model?
Faced some misunderstanding in updating person data. There is an authorized user who edits his profile. With this whore, I slip the form on him:
@login_required
def profile(request):
# проверяем запрос с аякса
if request.method == "POST" and request.is_ajax():
person_form = PersonProfileForm(request.POST)
# проходим валидацию формы
if person_form.is_valid():
try:
# результат операции
data = {
'success': _('Профиль обновлен!')
}
except:
data = {
'try': True
}
else:
# результат операции
data = {
'errors': person_form.errors
}
# позвращаем результат в JSON
return HttpResponse(json.dumps(data))
else:
# регистрируем форму
person_form = PersonProfileForm()
# рендеринг шаблона
return render(request, 'person/profile.html', {'form': person_form})
class PersonProfileForm(forms.ModelForm):
"""
Создание новой персоны
"""
first_name = forms.CharField(label='Ваше имя', required=True)
last_name = forms.CharField(label='Ваша фамилия', required=True)
class Meta:
model = Person
fields = ['first_name', 'last_name', 'country']
labels = {
'country': 'От куда Вы'
}
help_texts = {
'first_name': _('Обязательно указывайте все данные реальные!'),
}
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