在django中动态设置默认表单值
我正在尝试找到一种动态设置默认表单值的方法。例如,如果我在网页上添加 facebook 登录功能,并且我可以从 facebook javascript 返回的内容中获取他的名字和姓氏,我希望能够将这些值设置为新的“value”参数给定的形式。 (基本上这样我就可以将用户放入我的数据库中) 我希望有某种
{{ form.firstname.default = Javascript.return.firstname }}
东西可以插入到模板中,但没有...... 有什么想法吗?谢谢。
编辑;;也许首先将信息传递到views.py 中会更好?但我该怎么做呢?我只是考虑在 javascript 字段中手写输入,这会很烦人......
I'm trying to find a way to dynamically set default form values. So for example, if I add a facebook login feature on my webpage, and I can get his first name and last name from what the facebook javascript returns, I want to be able to set these values as the new "value" parameter in the given form. (basically so I'm able to put the user in my database)
I was hoping that there is some sort of
{{ form.firstname.default = Javascript.return.firstname }}
that I can insert into a template but there isn't...
Any ideas? Thank you.
Edit;; Maybe it would be better to first pass in the information into a views.py? But how would I do this? I am just considering hand writing the inputs out in the javascript field, which would be annoying...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以设置模板变量的默认值:
假设 {{ Javascript.return.firstname }} 在该模板上下文中有效。
You can set the default value of a template variable:
assuming that {{ Javascript.return.firstname }} is valid in that template context.
Django 生成的代码都是服务器端的,它的模板不能使用页面加载时不可用的任何内容。您需要使用 javascript 来设置该值。
例如,如果您的表单如下所示:
您可以使用此 javascript(假设您使用的是 jQuery):
The code generated by Django is all server-side, its templates can't use anything which isn't available when the page is loaded. You need to use javascript to set the value.
For example, if your form looks like this:
You could use this javascript (assuming you're using jQuery):