I can use this code to detect if the user is in America
ip, is_routable = get_client_ip(request)
ip2 = requests.get('http://ip.42.pl/raw').text
if ip == "127.0.0.1":
ip = ip2
Country = DbIpCity.get(ip, api_key='free').country
widgets.py
If the user is American I want to pass information to the template bootstrap_datetimepicker.html.
I am really unsure how to add information about the users country to the below code (which I got from another website).
class BootstrapDateTimePickerInput(DateTimeInput):
template_name = 'widgets/bootstrap_datetimepicker.html'
def get_context(self, name, value, attrs):
datetimepicker_id = 'datetimepicker_{name}'.format(name=name)
if attrs is None:
attrs = dict()
attrs['data-target'] = '#{id}'.format(id=datetimepicker_id)
# attrs['data-target'] = '#{id}'.format(id=datetimepicker_id)
attrs['class'] = 'form-control datetimepicker-input'
context = super().get_context(name, value, attrs)
context['widget']['datetimepicker_id'] = datetimepicker_id
return context
bootstrap_datetimepicker.html
I want to run a different JQuery function for American users.
{% if America %}
<script>
$(function () {
$("#{{ widget.datetimepicker_id }}").datetimepicker({
// format: 'DD/MM/YYYY/YYYY HH:mm:ss',
format: 'MM/DD/YYYY',
changeYear: true,
changeMonth: false,
minDate: new Date("01/01/2015 00:00:00"),
});
});
</script>
{% else %}
<script>
$(function () {
$("#{{ widget.datetimepicker_id }}").datetimepicker({
// format: 'DD/MM/YYYY/YYYY HH:mm:ss',
format: 'DD/MM/YYYY',
changeYear: true,
changeMonth: false,
minDate: new Date("01/01/2015 00:00:00"),
});
});
</script>
{% endif %}