Passing Variable From Template.html To By URL (using A File Input Form)

- 1 answer

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 and retrieved in the template by getJSON:

<script type="text/javascript">
$(function() {
    $.getJSON('available_properties', function(data) {
        var options = [];
        for(var i in
            var prop =[i];
            options.push('<option value="'+prop+'">'+prop+'</option>');         
        $("#properties > select").html(options.join(''));

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!">

How can I pass the property value directly in the URL path that must be called on the submit form?



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>\*)/$', 'appname.viewname'),

in your url, and:

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

in of your app. This is documented in Part 3 of the django tutorial, as you can find it here:

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:

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

where the get works as:

To put the querystring into request.POST, you have to configure your like this:

urlpatters = patterns('',

That way everything after /upload/ will be passed to request.POST