I have a model.py:
class usercommand(models.Model):
user = models.ForeignKey(User, blank=False)
a_field = models.PositiveIntegerField(null=True, blank=True)
timestamp = models.DateTimeField(blank=True, null=True)
Related to i use create Django model form with user Input hidden field. forms.py:
class ApplicationForm(forms.ModelForm):
class Meta:
model = userCommand
widgets = {
'user': forms.HiddenInput()
}
As you can see that there are three fields available for take the input. But i filled the user field in views.py:
def views_method(request, object_id):
if request.method == 'POST':
form = ApplicationForm(request.POST)
if form.is_valid():
form_inst = form.save(commit=False)
form_inst.user = request.user
form_inst.save();
return HttpResponseRedirect(reverse('user_list'))
else:
form = ApplicationForm()
extra_context = {
'form':ApplicaitonForm(initial={'user': request.user.pk})
}
return direct_to_template(request, 'template/remote.html',
extra_context=extra_context)
Now i used in my remote.html:
<form action="" method="POST">
{{ form.as_p }}
<input type="submit" value="{% trans "Add " %}" />
</form>
What i need to fill the timestamp field from template level by using the following picker written in Jquery:
<input class="time" type="text" name="start" id="start" />
$('input.time').timepicker();
But i am not able to understand, How should i do it? because i am using {{form.as_p}}. How will i populate the froms timestamp fields with this Jquery code.
If i am not wrong then we can do this by following code :
$('#id_timestamp').val($('#time').val())
where id_timestamp is the id of timestamp field in ApplicationForm. But how should i write the code, So that can i can also fill the rest field in ApplicationForm at template level.
1 Answer