Setting default form value in template

1,963 views
Skip to first unread message

raj

unread,
Aug 25, 2011, 1:10:38 AM8/25/11
to Django users
I'm trying to set a default form value in the template, and not in the
forms.py file (because I'm trying to make the default value dynamic
depending on something that javascript will return).
So To test it, I have the following line in my template:

{{ form.first_name|default:"Enter Your first name" }}

The issue is that it doesn't work, And I can't figure out why. Am I
doing something illegal?
Help Please. Thank you.

Daniel Roseman

unread,
Aug 25, 2011, 2:08:09 AM8/25/11
to django...@googlegroups.com
No, that doesn't work at all, and never will. The "default" filter is for providing a value in case a particular context variable is empty, not for setting a form field's default value.

Your reasoning doesn't seem to make sense, in any case. Templates are rendered server-side, so Javascript can have no effect on what is rendered. Do this in the view.
--
DR.

raj

unread,
Aug 25, 2011, 12:13:05 PM8/25/11
to Django users
Let me be a bit more clear.
Lets take, for example, the linkedin javascript api. Now, when you log
into your linkedin account through my website, the website can access
certain parts of your profile using javascript. Now, when the user
first logs in, I want to be able store some information about the user
in my database. So, if the user decides to create an article or
something on my website, his article can be attributed to that
account. Otherwise, I don't see any other way to attribute data to
that user. I don't really know how to pass what the javascript return
back into the view.

raj

unread,
Aug 25, 2011, 12:49:08 PM8/25/11
to Django users
Actually, I think I'm figuring it out. Just a quick question though.
Is it possible to pass javascript values through the form attrs
attribute? For example:

class forms(forms.Form):
name = forms.TextInput(attrs={'size': 10, 'value': '<?
js=firstName ?>'',})

raj

unread,
Aug 26, 2011, 12:46:11 AM8/26/11
to Django users
Bump**

Mike Dewhirst

unread,
Aug 26, 2011, 2:08:46 AM8/26/11
to django...@googlegroups.com
I think you need a bit of R&D here. The general advice I've seen is to
get it working without javascript and then find a js solution.

I feel it is difficult. Back at the server you only have the data the
browser sends you. If I understand what you are saying, your problem is
to write some javascript which finds some value(s) which only appear in
the user's browser in the DOM or in the headers - not sure - and
manipulate the DOM to save it somewhere before the user hits [Submit].

I'm interested to see how you do this so if you figure it out please
post your solution.

Cheers

Mike

raj

unread,
Aug 30, 2011, 10:54:31 PM8/30/11
to Django users
Hey, figured it out for the most part. Had to use a little jquery to
get it to work.
This is what it looked like:
<head>
<script type = "text/javascript" src="/media/js/jquery.form-
defaults.js"></script>
<script type="text/javascript">
function displayProfiles(profiles) {
member = profiles.values[0];
$(document).ready(function(){
var fname = member.firstName;
var lname = member.lastName;
var id = member.id;
$('#fname').val(fname);
$('#lname').val(lname);
$('#linkedid').val(id);
});
}
</script>
</head>
<body>
<form>
<table>
{{ form.as_table }}
</table>
</form>
</body>
________________________

And in the forms.py file, I just set a widget for each of the forms,
and then as one of the attrs, I stated the id's (as shown in the
jquery)
Example:
last_name = forms.CharField(widget = forms.TextInput(attrs =
{'id':'lname'}), required=True)
Hope you like it. :)
Reply all
Reply to author
Forward
0 new messages