Tuesday, August 30, 2011

Re: Setting default form value in template

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. :)
On Aug 26, 2:08 am, Mike Dewhirst <mi...@dewhirst.com.au> wrote:
> 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
>
> On 26/08/2011 2:46pm, raj wrote:
>
>
>
>
>
>
>
> > Bump**
>
> > On Aug 25, 12:49 pm, raj<nano.ri...@gmail.com>  wrote:
> >> 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 ?>'',})
>
> >> On Aug 25, 12:13 pm, raj<nano.ri...@gmail.com>  wrote:
>
> >>> 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.
> >>> On Aug 25, 2:08 am, Daniel Roseman<dan...@roseman.org.uk>  wrote:
> >>>> On Thursday, 25 August 2011 06:10:38 UTC+1, raj wrote:
> >>>>> 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.
> >>>> 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.

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To post to this group, send email to django-users@googlegroups.com.
To unsubscribe from this group, send email to django-users+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/django-users?hl=en.

No comments:

Post a Comment