Hello! I am pretty new with Django, and now the problem I am facing is related to the long load times I am having when loading a form (maybe the solution to this could be applied not only to forms for sure) because I have a field that uses the cities_light cities to save city / state / country where a person is located, this is to register clients in a DB, so the model.py I am using is something like this:
class Client(models.Model):
id_number = models.CharField(max_length=20, primary_key=True, null=False)
# many other fields...
res_city = models.ForeignKey('cities_light.City', on_delete=models.SET_NULL, null=True, blank=True, default=None)
and the form where I am calling it is like this:
class AddClientForm(forms.ModelForm):
class Meta:
model = Client
exclude = ['salesman_refer']
labels = {
# some labels...
}
res_city = s2forms.ModelSelect2Widget()
def __init__(self, *args, **kwargs):
super(AddClientForm, self).__init__(*args, **kwargs)
self.helper = FormHelper()
self.helper.layout = Layout(
# a crispy forms layout
)
the view that is calling this form is this:
@login_required
def clients_create(request):
if request.method == 'POST':
form = AddClientForm(request.POST)
if form.is_valid():
User = get_user_model()
client = form.save(commit=False)
client.salesman_refer = User.objects.get(username=request.user.username).userprofile
client.save()
return redirect('/dashboard/clients/')
else:
return render(request, 'clients/new_client.html',
{'form': form})
else:
form = AddClientForm()
return render(request, 'clients/new_client.html',
{'form': form})
and finally the new_client.html template where it is loading…
{% extends 'core/base.html' %}
{% load static %}
{% block title %} Nuevo Cliente {% endblock %}
{% block content %}
<div class = 'py-1 px-1'>
{% load tailwind_filters %}
{% load crispy_forms_tags %}
<form method="post" action=".">
{% csrf_token %}
{% crispy form %}
<!--{{ form|crispy }}-->
</form>
</div>
{% endblock %}
{% block scripts %}
<script>
$("#id_res_city").select2({
closeOnSelect: true,
minimumInputLength: 3,
caches: true,
maximumSelectionLength: 10,
});
</script>
{% endblock %}
{% block stylesheets %}
<style>
.select2-container{
border: 1px solid #e2e8f0;
border-radius: .5rem;
line-height: 1.5;
padding: .5rem 1rem .5rem 1rem;
background-color: white;
border-width: 1px;
}
.select2-container--default .select2-selection--single{
border: none;
}
body {
--iuw-background: white !important;
--iuw-border-color: rgb(209 213 219 ) !important;
--iuw-color: #{$dashgray} !important;
--iuw-placeholder-text-color: #{$dashgray} !important;
--iuw-dropzone-background: white !important;
--iuw-image-preview-border: rgb(209 213 219) !important;
--iuw-image-preview-shadow: rgba(171, 170, 170, 0.3);
--iuw-add-image-background: #{$dashlight} !important;
--iuw-add-image-color: #{$dashgray} !important;
}
.select2-selection__arrow {
display: none;
}
</style>
{% endblock %}
I am using selec2, I tried to use djangoselect2, but I couldn’t make it works, also I expected that the caches: true will keep the options in the cache memory to be used again after an user register, but it is not the case… probably I am not configuring well my cache in settings.py:
CACHES = {
'default': {
'BACKEND': 'django.core.cache.backends.memcached.PyMemcacheCache',
'LOCATION': '0.0.0.0:11211',
},
# … default cache config and others
"select2": {
"BACKEND": "django_redis.cache.RedisCache",
"LOCATION": "127.0.0.1:6379/2",
"OPTIONS": {
"CLIENT_CLASS": "django_redis.client.DefaultClient",
}
}
}
SELECT2_CACHE_BACKEND = "select2"
any suggestion and help wil be welcomed! Thanks ^^
btw: any source to keep learning django (besides documentation) that could help me strength my basis and improve, will be awesome!