Amit Pal
Amit Pal

Reputation: 11052

How to use the value of Jquery script in Django Form?

I have a follwoing Jquery code, which create a Time Picker:

<input class="time" type="text" name="start" id="start" />
$('input.time').timepicker();

And i want to use this time picker value which comes into the text in Django-Forms.

My models.py:

class UsertTime(models.Model):
user = models.ForeignKey(User, blank=False)
data = models.CharField(max_length=30)
timestamp = models.DateTimeField(blank=True, null=True)

my forms.py:

class UserTimeForm(forms.ModelForm):
    class Meta:
    model = UserTime

In my template.html:

<form action="" method="POST">
{{ form.as_p }}
<input type="submit" value="{% trans "Add " %}" /> <a href="{% url host_list %}">{% trans "Cancel" %}</a>
</form>

<input class="time" type="text" name="start" id="start" />
$('input.time').timepicker();

Now how should i fill the timestamp Field by the Jquery written input. Following is the refer link for the Jquery Code:

Link for the Jquery sample

Upvotes: 2

Views: 1872

Answers (3)

Skylar Saveland
Skylar Saveland

Reputation: 11464

I might also be misunderstanding; but, can you not just inspect the output of form.as_p and operate on that input instead of $('input.time')

$('#id_timestamp').timepicker()

Upvotes: 3

Chris Pratt
Chris Pratt

Reputation: 239300

Unless, I'm gravely misunderstanding you, just use val():

$('#id_timestamp').val($('#time').val())

Upvotes: 2

Anthony Mills
Anthony Mills

Reputation: 8784

Does this not work:

<input class="time" type="text" name="start" id="start" value="{{form.timestamp}}" />

Sorry, I'm not an expert in Django.

Upvotes: 2

Related Questions