Add ability for customer to add their own shipping address
This commit is contained in:
parent
2ce2b59118
commit
89b40d639d
19
src/storefront/templates/storefront/address_create_form.html
Normal file
19
src/storefront/templates/storefront/address_create_form.html
Normal file
@ -0,0 +1,19 @@
|
||||
{% extends 'base.html' %}
|
||||
|
||||
{% block content %}
|
||||
<article>
|
||||
<header>
|
||||
<p><a href="{% url 'storefront:customer-detail' user.pk %}">← Back</a></p>
|
||||
<h1>Create Address</h1>
|
||||
</header>
|
||||
<section>
|
||||
<form method="post" action="{% url 'storefront:customer-address-create' customer.pk %}">
|
||||
{% csrf_token %}
|
||||
{{ form.as_p }}
|
||||
<p>
|
||||
<input type="submit" value="Create address">
|
||||
</p>
|
||||
</form>
|
||||
</section>
|
||||
</article>
|
||||
{% endblock %}
|
||||
@ -30,8 +30,13 @@
|
||||
</address>
|
||||
{% endwith %}
|
||||
</p>
|
||||
</section>
|
||||
<section>
|
||||
<h4>Your addresses</h4>
|
||||
<p>
|
||||
<a href="{% url 'storefront:customer-address-create' user.pk %}" class="action-button">+ New address</a>
|
||||
</p>
|
||||
<div>
|
||||
<p><strong>All addresses</strong></p>
|
||||
{% for address in customer.addresses.all %}
|
||||
<p>
|
||||
<address>
|
||||
|
||||
@ -15,6 +15,7 @@
|
||||
</header>
|
||||
<section class="checkout__address">
|
||||
<h3>Shipping address</h3>
|
||||
<p>{{shipping_address.email}}</p>
|
||||
<address>
|
||||
{{shipping_address.first_name}}
|
||||
{{shipping_address.last_name}}<br>
|
||||
|
||||
@ -33,7 +33,8 @@ urlpatterns = [
|
||||
# path('delete/', views.CustomerDeleteView.as_view(), name='customer-delete'),
|
||||
|
||||
path('orders/<int:order_pk>/', views.OrderDetailView.as_view(), name='order-detail'),
|
||||
path('addresses/<int:address_pk>/update/', views.AddressUpdateView.as_view(), name='address-update'),
|
||||
path('addresses/new/', views.CustomerAddressCreateView.as_view(), name='customer-address-create'),
|
||||
path('addresses/<int:address_pk>/update/', views.CustomerAddressUpdateView.as_view(), name='address-update'),
|
||||
])),
|
||||
|
||||
]
|
||||
|
||||
@ -309,6 +309,7 @@ class CustomerUpdateView(LoginRequiredMixin, UpdateView):
|
||||
def get_success_url(self):
|
||||
return reverse('storefront:customer-detail', kwargs={'pk': self.object.pk})
|
||||
|
||||
|
||||
class OrderDetailView(LoginRequiredMixin, DetailView):
|
||||
model = Order
|
||||
template_name = 'storefront/order_detail.html'
|
||||
@ -319,7 +320,27 @@ class OrderDetailView(LoginRequiredMixin, DetailView):
|
||||
context['customer'] = User.objects.get(pk=self.kwargs['pk'])
|
||||
return context
|
||||
|
||||
class AddressUpdateView(LoginRequiredMixin, UpdateView):
|
||||
class CustomerAddressCreateView(LoginRequiredMixin, CreateView):
|
||||
model = Address
|
||||
template_name = 'storefront/address_create_form.html'
|
||||
form_class = AccountAddressForm
|
||||
|
||||
def get_context_data(self, **kwargs):
|
||||
context = super().get_context_data(**kwargs)
|
||||
context['customer'] = User.objects.get(pk=self.kwargs['pk'])
|
||||
return context
|
||||
|
||||
def form_valid(self, form):
|
||||
customer = User.objects.get(pk=self.kwargs['pk'])
|
||||
self.object = form.save()
|
||||
customer.addresses.add(self.object)
|
||||
|
||||
return super().form_valid(form)
|
||||
|
||||
def get_success_url(self):
|
||||
return reverse('storefront:customer-detail', kwargs={'pk': self.kwargs['pk']})
|
||||
|
||||
class CustomerAddressUpdateView(LoginRequiredMixin, UpdateView):
|
||||
model = Address
|
||||
pk_url_kwarg = 'address_pk'
|
||||
template_name = 'storefront/address_form.html'
|
||||
@ -331,7 +352,7 @@ class AddressUpdateView(LoginRequiredMixin, UpdateView):
|
||||
return context
|
||||
|
||||
def get_success_url(self):
|
||||
return reverse('storefront:customer-detail',kwargs={'pk': self.kwargs['pk']})
|
||||
return reverse('storefront:customer-detail', kwargs={'pk': self.kwargs['pk']})
|
||||
|
||||
|
||||
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user