【问题标题】:Django form not displaying Datepicker and TimepickerDjango 表单不显示 Datepicker 和 Timepicker
【发布时间】:2018-12-02 22:52:48
【问题描述】:

嗨,Djangonauts, 我是 Django 的新手。请原谅代码或逻辑中的任何错误。我正在使用以下存储库中的 django-bootstrap-datepicker-plus 将日期选择器添加到我的代码中。 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>

它应该是这样工作的

现在就是这样

【问题讨论】:

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


    【解决方案1】:

    您正在混合使用 bootstrap 3 和 bootstrap 4。

    1. 在您的 base.html 中有一个指向 bootstrap 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。

    【讨论】:

    • 嗨,马蒂亚斯,我把它改成了 Bootstrap3,但我仍然遇到同样的错误。 template.html 有一个 {% block body %}{% endblock %},它们在 template.html 中获取 base.html
    • 要使用 template.html 文件中的 base.html 文件,您需要在 template.html 的顶部编写 {% extends 'base.html' %}。此外,您还需要在 base.html 中您希望代码结束的位置有一个 {% block base%}{% endblock%}。
    • 谢谢你,对 forms.py 进行了更多的编辑,完成了工作,非常感谢。字段和小部件必须分开处理。任何说“领域”的东西。在小部件中不起作用,必须单独保存
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多