我试图用jquery datepicker渲染django ModelForm,现在知道怎么做了吗?
models.py
....
class ProductsTbl(models.Model):
model_number = models.CharField(max_length=255, blank=True, null=True)
name = models.CharField(max_length=255, blank=True, null=True)
material = models.TextField(blank=True, null=True)
release = models.DateTimeField(blank=True, null=True)
...
forms.py
from django import forms
from django.forms import ModelForm
from .models import ProductsTbl,Upload
class ProductsTblForm(ModelForm):
class Meta:
model = ProductsTbl
fields = ('model_number','name','feature', 'material','release',)
edit_thing.html
<link rel="stylesheet" href="//code.jquery.com/ui/1.11.4/themes/smoothness/jquery-ui.css">
<script src="//code.jquery.com/jquery-1.10.2.js"></script>
<script src="//code.jquery.com/ui/1.11.4/jquery-ui.js"></script>
<link rel="stylesheet" href="/resources/demos/style.css">
<script>
$(function() {
$( "#datepicker" ).datepicker({ dateFormat: 'yy-mm-dd' });
});
</script>
{% extends 'base.html' %} {% block title %}
Edit {{ thing.name }} - {{ block.super }} {% endblock title %}
{% block content %}
<h1>Edit "{{ thing.name }}"</h1>
<form role="form" action="" method="post">
{% csrf_token %}
{{ form.as_p }}
<br>
<button type="submit">Submit</button>
<a href="{% url 'edit_thing_uploads' slug=thing.slug %}"> Edit images</a>
</form>
{% endblock %}
我想我必须在forms.py中做一些事情,让
版本的
有datepicker功能,according to the link here
现在只是一个普通的HTML
答案 0 :(得分:2)
在下面的代码行中,#datepicker
指的是您要应用datepicker的元素的ID,在这种情况下,它不会是datepicker
,因为它将是django提供的默认ID,我猜测它应该是#id_release
,但是你必须检查输入元素以找出它应该是什么
$( "#datepicker" ).datepicker({ dateFormat: 'yy-mm-dd' });
由于auto_id
,Django默认为字段创建了ID通过将id_预先添加到表单字段名称来生成id属性值。但是,如果您要更改id约定或完全删除HTML id属性和标记,则此行为是可配置的。