Django-Oscar - Address Book not working on shipping-address
I'm having this problem that I mentioned in the title and I've been trying to fix it all morning, I've done some tests to find out where the error is coming from, I found out where it came from, but when I get to the file, I don't see any errors...
As you can see, the user is logged in, but he cannot find an address for this user, since I have already registered one.
See the code:
{% extends "oscar/checkout/checkout.html" %}
{% load i18n %}
{% block title %}
{% trans "Shipping address" %} | {{ block.super }}
{% endblock %}
{% block checkout_nav %}
{% include 'oscar/checkout/nav.html' with step=1 %}
{% endblock %}
{% block checkout_title %}{% trans "Shipping address" %}{% endblock %}
{% block order_contents %}{% endblock %}
{% block shipping_address %}
<div class="col-sm-12">
<div class="sub-header">
<h2>{% trans "Where should we ship to?" %}</h2>
</div>
{% if user.is_authenticated %}
<p>User logged in</p>
{% if addresses %}
<p>Addresses: {{ addresses }}</p>
<h3>{% trans "An address from your address book?" %}</h3>
<div class="choose-block">
<div class="row">
{% for address in addresses %}
{% block select_address_form %}
<div class="col-sm-6 d-flex">
<div class="card card-body bg-light">
<address>
{% block select_address_fields %}
{% for field in address.active_address_fields %}
<span>{{ field }}</span>{% if not forloop.first %}<br/>{% endif %}
{% endfor %}
{% endblock %}
</address>
<form action="{% url 'checkout:shipping-address' %}" method="post" id="select_shipping_address_{{ address.id }}" class="mb-0">
{% csrf_token %}
<input type="hidden" name="action" value="ship_to" />
<input type="hidden" name="address_id" value="{{ address.id }}" />
{% if address.is_default_for_shipping %}
<button type="submit" class="btn btn-success btn-large ship-address" data-loading-text="{% trans 'Saving...' %}"><i class="fas fa-check-circle"></i> {% trans "Ship to your default shipping address" %}</button>
{% else %}
<button type="submit" class="btn btn-primary btn-large ship-address" data-loading-text="{% trans 'Saving...' %}">{% trans "Ship to this address" %}</button>
{% endif %}
<div class="btn-group address-controls">
<a href="{% url 'checkout:user-address-update' pk=address.id %}" class="btn btn-secondary">{% trans "Edit address" %}</a>
<button class="btn btn-secondary dropdown-toggle" data-toggle="dropdown"></button>
<ul class="dropdown-menu">
<a href="{% url 'checkout:user-address-delete' pk=address.id %}" class="btn-remove-address nav-link">{% trans "Delete" %}</a>
</ul>
</div>
</form>
</div>
</div>
{% if forloop.counter|divisibleby:2 %}
</div><div class="row">
{% endif %}
{% endblock %}
{% endfor %}
</div>
</div>
<h3>{% trans "Or a new address?" %}</h3>
{% endif %}
<p>No addresses found</p>
{% endif %}
{% block new_address_form %}
<div class="card card-body bg-light">
<form action="{% url 'checkout:shipping-address' %}" method="post" id="new_shipping_address">
{% csrf_token %}
{% include "oscar/partials/form_fields.html" with form=form style='horizontal' %}
<div class="form-group row">
<div class="offset-sm-4 col-sm-8">
<button type="submit" class="btn btn-lg btn-primary" data-loading-text="{% trans 'Continuing...' %}">{% trans "Continue" %}</button>
{% trans "or" %} <a href="{% url 'basket:summary' %}">{% trans "return to basket" %}</a>
</div>
</div>
</form>
</div>
{% endblock %}
</div>
<script src="https://code.jquery.com/jquery-3.6.3.min.js" integrity="sha256-pvPw+upLPUjgMXY0G+8O0xUf+/Im1MZjXxxgOcBQBXU=" crossorigin="anonymous"></script>
<script>
$(document).ready(function(){
$("#id_postcode").on("blur", function(){
console.log('foi')
var cep = $(this).val();
$.ajax({
url: "https://viacep.com.br/ws/"+ cep +"/json/",
method: "GET",
success: function(data){
$("#id_line1").val(data.logradouro);
$("#id_line3").val(data.bairro);
$("#id_line4").val(data.localidade);
$("#id_state").val(data.uf);
}
});
});
});
</script>
{% endblock shipping_address %}
{% block shipping_method %}{% endblock %}
{% block payment_method %}{% endblock %}
Part of the views.py code:
class ShippingAddressView(CheckoutSessionMixin, generic.FormView):
template_name = 'oscar/checkout/shipping_address.html'
form_class = ShippingAddressForm
success_url = reverse_lazy('checkout:shipping-method')
pre_conditions = ['check_basket_is_not_empty',
'check_basket_is_valid',
'check_user_email_is_captured']
skip_conditions = ['skip_unless_basket_requires_shipping']
def get_initial(self):
initial = self.checkout_session.new_shipping_address_fields()
if initial:
initial = initial.copy()
# Convert the primary key stored in the session into a Country
# instance
try:
initial['country'] = Country.objects.get(
iso_3166_1_a2=initial.pop('country_id'))
except Country.DoesNotExist:
# Hmm, the previously selected Country no longer exists. We
# ignore this.
pass
return initial
def get_context_data(self, **kwargs):
ctx = super().get_context_data(**kwargs)
if self.request.user.is_authenticated:
# Look up address book data
ctx['addresses'] = self.get_available_addresses()
return ctx
···
What should I do so that when the user already has an address registered, it appears on this tab and he only selects the address?
As I put it in the code, I tried to find out where the error was coming from... I put a <p> tag after the user's authentication conditional to see if at least it was entering the block. Yes, it is.
I did the same thing for the address conditional, and this is where the problem lies, it doesn't identify the address even though it's already registered.
I went to check the views and didn't find any errors... at least I don't see them.