[Answer]-Using Django Templates, how can I access values from my model in the JavaScript header?

1๐Ÿ‘

I guess eventobject.datetime must be a string or python datetime object.

If it is a string, change your code to :

variable1= "{{ eventobject.datetime }}" ; 

If it is a datetime object, use django template filter :

variable1= "{{ eventobject.datetime|date:"D d M Y" }}"

https://docs.djangoproject.com/en/dev/ref/templates/builtins/#date

If you do not put the quotes, the javascript statement looks like:

variable1 = 2013-05-30 ;

which is INCORRECT javascript syntax, the quotes are needed.

๐Ÿ‘คDhruvPathak

0๐Ÿ‘

Per Django docs, you can use a date filter to format date:

variable1= '{{ eventobject.datetime|date:"d-m-Y" }}';

Additionally, you can also set DATE_FORMAT (or DATETIME_FORMAT) setting to set a project-wide default for displaying such values.

๐Ÿ‘คDrwhite

Leave a comment