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>
|
</address>
|
||||||
{% endwith %}
|
{% endwith %}
|
||||||
</p>
|
</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>
|
<div>
|
||||||
<p><strong>All addresses</strong></p>
|
|
||||||
{% for address in customer.addresses.all %}
|
{% for address in customer.addresses.all %}
|
||||||
<p>
|
<p>
|
||||||
<address>
|
<address>
|
||||||
|
|||||||
@ -15,6 +15,7 @@
|
|||||||
</header>
|
</header>
|
||||||
<section class="checkout__address">
|
<section class="checkout__address">
|
||||||
<h3>Shipping address</h3>
|
<h3>Shipping address</h3>
|
||||||
|
<p>{{shipping_address.email}}</p>
|
||||||
<address>
|
<address>
|
||||||
{{shipping_address.first_name}}
|
{{shipping_address.first_name}}
|
||||||
{{shipping_address.last_name}}<br>
|
{{shipping_address.last_name}}<br>
|
||||||
|
|||||||
@ -33,7 +33,8 @@ urlpatterns = [
|
|||||||
# path('delete/', views.CustomerDeleteView.as_view(), name='customer-delete'),
|
# path('delete/', views.CustomerDeleteView.as_view(), name='customer-delete'),
|
||||||
|
|
||||||
path('orders/<int:order_pk>/', views.OrderDetailView.as_view(), name='order-detail'),
|
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):
|
def get_success_url(self):
|
||||||
return reverse('storefront:customer-detail', kwargs={'pk': self.object.pk})
|
return reverse('storefront:customer-detail', kwargs={'pk': self.object.pk})
|
||||||
|
|
||||||
|
|
||||||
class OrderDetailView(LoginRequiredMixin, DetailView):
|
class OrderDetailView(LoginRequiredMixin, DetailView):
|
||||||
model = Order
|
model = Order
|
||||||
template_name = 'storefront/order_detail.html'
|
template_name = 'storefront/order_detail.html'
|
||||||
@ -319,7 +320,27 @@ class OrderDetailView(LoginRequiredMixin, DetailView):
|
|||||||
context['customer'] = User.objects.get(pk=self.kwargs['pk'])
|
context['customer'] = User.objects.get(pk=self.kwargs['pk'])
|
||||||
return context
|
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
|
model = Address
|
||||||
pk_url_kwarg = 'address_pk'
|
pk_url_kwarg = 'address_pk'
|
||||||
template_name = 'storefront/address_form.html'
|
template_name = 'storefront/address_form.html'
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user