通过URL将变量从template.html传递到views.py(使用文件输入表单)
也许这是一个非常愚蠢的问题,但我是 django 的新手。
我有一个特定的 django 模板,其中包含一组可以选择的属性。在上传的文件上,我必须根据所选属性执行不同的处理,因此我想将所选属性值从模板传递到视图。属性在 view.py 中硬编码,并通过 getJSON 在模板中检索:
<script type="text/javascript">
$(function() {
$.getJSON('available_properties', function(data) {
var options = [];
for(var i in data.properties)
{
var prop = data.properties[i];
options.push('<option value="'+prop+'">'+prop+'</option>');
}
$("#properties > select").html(options.join(''));
});
});
</script>
然后上传文件表单触发操作:
<form onsubmit="return checkfile();" prop="return get_prop();" action="calculate/<property_value_goes_here>" enctype="multipart/form-data" method="POST">
{% csrf_token %}
<input type="file" id="uploadfile" name="uploadfile" size="30">
<input type="submit" id="calculate" value="Calculate!">
</form>
如何直接在必须在提交表单上调用的 URL 路径中传递属性值?
Maybe this is a very silly question but I'm new using django.
I have a certain django template with a set of properties that can be chosen. On an uploaded file I have to perform a different treatment depending on the chosen property, so I want to pass the chosen property value from the template to the view. Properies are hard coded in view.py and retrieved in the template by getJSON:
<script type="text/javascript">
$(function() {
$.getJSON('available_properties', function(data) {
var options = [];
for(var i in data.properties)
{
var prop = data.properties[i];
options.push('<option value="'+prop+'">'+prop+'</option>');
}
$("#properties > select").html(options.join(''));
});
});
</script>
Then an uploading file form trigger the action:
<form onsubmit="return checkfile();" prop="return get_prop();" action="calculate/<property_value_goes_here>" enctype="multipart/form-data" method="POST">
{% csrf_token %}
<input type="file" id="uploadfile" name="uploadfile" size="30">
<input type="submit" id="calculate" value="Calculate!">
</form>
How can I pass the property value directly in the URL path that must be called on the submit form?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
实际上你有两种方法可以解决这个问题。一种是解析 url,另一种是将属性作为请求参数传递。
第一个可以这样实现:
这样 /upload/ 之后的所有内容都将传递给 request.POST
, 'appname.viewname'), )在您的 url 中,以及:
在您应用程序的views.py 中。这记录在 django 教程的第 3 部分中,您可以在这里找到它: http: //docs.djangoproject.com/en/1.2/intro/tutorial03/
但是对于您的问题,第二个解决方案可能更好。您可以创建一个标准的 url-querystring (正如您的文章建议您知道如何使用 js 来做到这一点),如下所示:
并获取传递的值,如下所示:
get 的工作方式为:
request.POST.get(property_name,fallback_value)
要将查询字符串放入 request.POST,您必须像这样配置 urls.py:
这样 /upload/ 之后的所有内容都将传递给 request.POST
Actually you have two ways to solve that problem. One is parsing the url, the other one is passing the properities as request parameters.
The first one can be implemented like this:
That way everything after /upload/ will be passed to request.POST
, 'appname.viewname'), )in your url, and:
in views.py of your app. This is documented in Part 3 of the django tutorial, as you can find it here: http://docs.djangoproject.com/en/1.2/intro/tutorial03/
But for your problem the second solution is probably the better on. You can create a standard url-querystring (as your post suggests you know how to use js to do that) like this:
and get the passed values like this:
where the get works as:
request.POST.get(property_name,fallback_value)
To put the querystring into request.POST, you have to configure your urls.py like this:
That way everything after /upload/ will be passed to request.POST