Django - Setting date as date input value

2024/9/30 13:22:28

I'm trying to set a date as the value of a date input in a form. But, as your may have guessed, it's not working.

Here's what I have in my template:

<div class="form-group"><label for="date" class="col-md-3 control-label">Date</label><div class="col-md-9"><input type="date" class="form-control" id="date" value="{{placement.date}}"></div>
</div>

And this is the view that calls it as well as the Placement model:

class Placement(models.Model):student = models.ForeignKey(Student)email = models.EmailField(max_length=254)fname = models.CharField(max_length=50)sname = models.CharField(max_length=50)cname = models.CharField(max_length=100)position = models.CharField(max_length=50)house = models.CharField(max_length=50, blank=True)street = models.CharField(max_length=50)town = models.CharField(max_length=50)county = models.CharField(max_length=50)postcode = models.CharField(max_length=8)phone = models.CharField(max_length=20)length = models.IntegerField(null=True)category = models.CharField(max_length=50)date = models.DateField(null=True)confirmed = models.BooleanField(default=False)completed = models.BooleanField(default=False)created = models.DateTimeField(null=True)def view_placement(request, placement_id):school = School.objects.get(pk=request.session['school'])context = {'school':school}if request.session['utype'] == 'a':context['user'] = Administrator.objects.get(pk=request.session['user'])context['placement'] = Placement.objects.get(pk=placement_id)return render(request, 'workxp/admin/view_placement.html', context)

But it doesn't display the date. Just an empty date input...

How can I fix this?

Thanks!

Answer

The HTML date should take the format YYYY-MM-DD. So you have to make a conversion using the {{ value|date:"D d M Y" }} command.

Your code will be:

 <input type="date" class="form-control" id="date" value="{{placement.date|date:"Y-m-d" }}">

HTML documentation here: http://www.w3.org/TR/html-markup/input.date.html#input.date.attrs.value

Django date documentation here: https://docs.djangoproject.com/en/1.7/ref/templates/builtins/#date

https://en.xdnf.cn/q/71083.html

Related Q&A

ReduceLROnPlateau gives error with ADAM optimizer

Is it because adam optimizer changes the learning rate by itself. I get an error saying Attempting to use uninitialized value Adam_1/lr I guess there is no point in using ReduceLRonPlateau as Adam wil…

How to make add replies to comments in Django?

Im making my own blog with Django and I already made a Comments system.. I want to add the replies for each comment (like a normal comments box) and I dont know what to do this is my current models.py …

Which Regular Expression flavour is used in Python?

I want to know which RegEx-flavour is used for Python? Is it PCRE, Perl compatible or is it ICU or something else?

Python regex: Including whitespace inside character range

I have a regular expression that matches alphabets, numbers, _ and - (with a minimum and maximum length).^[a-zA-Z0-9_-]{3,100}$I want to include whitespace in that set of characters.According to the Py…

Python - how can I override the functionality of a class before its imported by a different module?

I have a class thats being imported in module_x for instantiation, but first I want to override one of the classs methods to include a specific feature dynamically (inside some middleware that runs bef…

Calling a stateful LSTM as a functional model?

I have a stateful LSTM defined as a Sequential model:model = Sequential() model.add(LSTM(..., stateful=True)) ...Later, I use it as a Functional model:input_1, input_2 = Input(...), Input(...) output_1…

How to cluster Gantt bars without overlap?

Using create_gantt I have overlapping start and end dates: import plotly.plotly as py import plotly.figure_factory as ff import plotlydf = [dict(Task="Milestone A", Start=2017-01-01, Finish=2…

Fail to install lxml using pip

This is the command I used to install lxml:sudo pip install lxmlAnd I got the following message in the Cleaning Up stage:Cleaning up... Command /usr/bin/python -c "import setuptools, tokenize;…

Python 3.x list comprehension VS tuple generator

Is there any reason for memory, speed or whatever, that I would want to use:tuple(i for i in range(5000))instead of:[i for i in range(5000)]If I didnt mind the immutability of tuples

Using Sphinx with a distutils-built C extension

I have written a Python module including a submodule written in C: the module itself is called foo and the C part is foo._bar. The structure looks like:src/ foo/__init__.py <- contains the public …