通过URL将变量从template.html传递到views.py(使用文件输入表单)

发布于 2024-10-20 23:30:51 字数 1045 浏览 0 评论 0原文

也许这是一个非常愚蠢的问题,但我是 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

少女情怀诗 2024-10-27 23:30:51

实际上你有两种方法可以解决这个问题。一种是解析 url,另一种是将属性作为请求参数传递。

第一个可以这样实现:

urlpatterns = patterns('',
    (r'^/(?P<property>\*)/

在您的 url 中,以及:

def your_view(request,property):
    #your view stuff here 

在您应用程序的views.py 中。这记录在 django 教程的第 3 部分中,您可以在这里找到它: http: //docs.djangoproject.com/en/1.2/intro/tutorial03/

但是对于您的问题,第二个解决方案可能更好。您可以创建一个标准的 url-querystring (正如您的文章建议您知道如何使用 js 来做到这一点),如下所示:

/upload/?property=value&someotherproperty=othervalue  

并获取传递的值,如下所示:

def your_view(request):
    property = request.POST.get('property',None)
    someotherproperty = request.POST.get('someotherproperty',None)  

get 的工作方式为:
request.POST.get(property_name,fallback_value)

要将查询字符串放入 request.POST,您必须像这样配置 urls.py:

urlpatters = patterns('',
    (r'^upload/

这样 /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

,'appname.viewname'), )

这样 /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:

urlpatterns = patterns('',
    (r'^/(?P<property>\*)/

in your url, and:

def your_view(request,property):
    #your view stuff here 

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:

/upload/?property=value&someotherproperty=othervalue  

and get the passed values like this:

def your_view(request):
    property = request.POST.get('property',None)
    someotherproperty = request.POST.get('someotherproperty',None)  

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:

urlpatters = patterns('',
    (r'^upload/

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

,'appname.viewname'), )

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文