# model
class UpdateUserDataForm(forms.ModelForm):
class Meta:
model = User
fields = ['phone', 'address']
# view
@login_required
def checkout(request):
user_phone = request.user.phone
user_address = request.user.address
data_form = UpdateUserDataForm(
request.POST,
instance=request.user,
initial={'phone': user_phone, 'address': user_address}
)
context = {'form': data_form, 'phone': user_phone, 'address': user_address}
if request.method == 'POST':
if data_form.is_valid():
data_form.save()
return redirect('home')
else:
return render(request, 'cart/checkout.html', context=context)
return render(request, 'cart/checkout.html', context=context)
# html
{% extends 'pizza/base.html' %}
{% load static %}
{% block content %}
<h3>Оставьте ваш номер телефона и адрес доставки. Мы свяжемся с вами в течении 10 минут !</h3>
<form method="post" enctype="multipart/form-data">
{% csrf_token %}
<p>
<label>Номер телефона: </label>
{{ form.phone }}
</p>
<p>
<label>Адрес доставки: </label>
{{ form.address }}
</p>
<input type="submit" value="Заказать">
</form>
{% endblock %}