简体   繁体   中英

datepicker Is not showing?

I am developing an application with Django and I need to choose date and time. I am trying to do it with 'bootstrap_datepicker_plus' , but the calendar does not appear when I show the form.

settings.py

INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'hungryFalconryApp',
    'rest_framework',
    'bootstrap4',
    'bootstrap_datepicker_plus',
]

BOOTSTRAP4 = {
    'include_jquery': True,
}

forms.py

from django import forms
from bootstrap_datepicker_plus import DateTimePickerInput

class DateForm(forms.ModelForm):

    class Meta:
        model = Comedero
        fields = ('dias', 'horas')

        widgets = {
            'dias': DateTimePickerInput(),
            'horas': DateTimePickerInput(),
        }

views.py

def programar_comederos(request, nombre):
    if request.method == "POST":
        form = DateForm(request.POST)
        print(nombre)
        if(form.is_valid()):
            comedero = form.save(commit=False)
            print(comedero.dias)
            print(comedero.horas)
    else:
        form = DateForm()
    return render(request, 'hungryFalconryApp/programar_comederos.html',{'nombre': nombre, 'form':form})

template.html

{% load bootstrap4 %}
{% bootstrap_css %}
{% bootstrap_javascript jquery='full' %}
{% bootstrap_messages %}
{{ form.media }}
{% block content %}
<form action="" method="post" class="form">

            {% csrf_token %}
            {% bootstrap_form form %}
            {% bootstrap_button "Guardar" button_type="submit" button_class="btn-primary" %}
        </form>
{% endblock %}

I have already solved. In django-bootstrap-datepicker-plus it indicates putting {{form.media}} after this code but {{form.media}} must go inside the tag

you can also use Datepicker too, Datepicker is jqueryUI, its faster and also reliable

see example here

you just cdn or jqueryUI file in your project

jqueryUI CDN

<script
  src="https://code.jquery.com/ui/1.12.0/jquery-ui.js"
  integrity="sha256-0YPKAwZP7Mp3ALMRVB2i8GXeEndvCq3eSl/WsAl1Ryk="
  crossorigin="anonymous"></script>

then assign Id to that particular field

<script>
   $("#datepicker").datepicker({ dateFormat: 'dd M yy' });

</script> 

" datepicker " is html id you also change date format from dateFormat

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM