Django表单未显示Datepicker和Timepicker

时间:2018-06-23 21:38:52

标签: django python-3.x django-models django-forms django-templates

嗨,Djangonauts,                    我是Django的新手。请原谅任何代码或逻辑错误。我正在使用以下存储库中的 django-bootstrap-datepicker-plus 将datepicker添加到我的代码中。 https://github.com/monim67/django-bootstrap-datepicker-plus。我已经完成了pip安装。我已经完成pip install django-bootstrap-datepicker-plus。我已经在设置中添加了它

INSTALLED_APPS = [    
    'bootstrap_datepicker_plus',
]

还添加了

BOOTSTRAP3 = {
    'include_jquery': True,
}

但是它不起作用。我下面有所有代码。我也无法在表单中添加DecimalField和IntegerField,因为这样会导致错误。但是现在我更专注于Datepicker。我已经附上了它应该如何显示以及如何显示的图像。我在这里做错了

Forms.py

from django import forms
from .models import Event
from bootstrap_datepicker_plus import DatePickerInput, TimePickerInput


class EventForm(forms.ModelForm):
    price: forms.DecimalField(decimal_places=2, max_digits=5)
    quantity: forms.IntegerField()
class Meta:
    model = Event
    fields = ('price', 'stock', 'date', 'time_from', 'time_to', 'event_choice')
    widgets = {
        'date': DatePickerInput(),
        'time_from': TimePickerInput(),
        'time_to': TimePickerInput(),

    }

def __init__(self, *args, **kwargs):
    super().__init__(*args, **kwargs)
    self.fields['stock'].label = "How many people can attend your event"
    self.fields['price'].label = "How much do members have to pay to attend"
    self.fields['event_choice'].label = "What kind of event is this?"
    self.fields['date'].label = "When do you plan to host your event"
    self.fields['time_from'].label = "What time does the event start"
    self.fields['time_to'].label = "What time does the event end"

Template.html

  {% extends 'base.html' %}
  {% block body %}
  {% load bootstrap3 %}
  {% block extrahead %}       {# Extra Resources Start #}
  {{ form.media }}            {# Form required JS and CSS #}
  {% endblock %}              {# Extra Resources End #}





<div class="form-group row">
     {{field.errors}}
     <form action="{% url 'event:new_event' slug=post.slug %}" method="post" >
         {% csrf_token %}
         {% bootstrap_form form %} 
         <input type="submit" value="Save" class="btn btn-success btn-sm" />
     </form>
</div>
{% endblock  %}

Models.py

class Event(models.Model):
    user = models.ForeignKey(User, related_name='seller')
    post = models.ForeignKey(Post, related_name='course')
    price = models.DecimalField(max_digits=5, decimal_places=2)
    stock = models.IntegerField((validators=[MinValueValidator(1), MaxValueValidator(35)]))
    date = models.DateField()
    time_from = models.TimeField()
    time_to = models.TimeField()

Base.html

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">[![enter image description here][1]][1]
    {% load staticfiles %}
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">
    <link rel="stylesheet" type="text/css" href="{% static 'style.css' %}">
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
    <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js" integrity="sha384-Tc5IQib027qvyjSMfHjOMaLkfuWVxZxUPnCJA7l2mCWNIpG9mGCD8wGNIcPD7Txa" crossorigin="anonymous"></script>
</head>

这就是它的工作方式 enter image description here

现在就是这样

enter image description here

1 个答案:

答案 0 :(得分:1)

您正在混合引导3和引导4。

  1. 在您的base.html中,有一个指向引导程序3.3.7的css链接(尽管我看不到您的template.html文件如何使用此文件)
  2. 在您的settings.py文件中,您正在使用BOOTSTRAP4
  3. 在template.html文件中,其声明为{% load bootstrap3 %}

文档指出Use BOOTSTRAP3 if you are using Bootstrap 3 [1],即您可能应该在设置文件中切换到BOOTSTRAP3。