0

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?

green69
  • 1,670
  • 3
  • 17
  • 21

1 Answers1

1

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

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

marue
  • 5,588
  • 7
  • 37
  • 65
  • Actually I was trying to use the first of your solution. But how can I pass the variable value in the URL of the template? If I write "action=/upload/action/?property=value" I have no interpolation of the value and the server search for a pattern named "upload/action/?property=value". – green69 Mar 08 '11 at 15:36
  • I tried to use "action/{{ document.getElementById('property').value }}" but it gives me an error. Suggestions? – green69 Mar 08 '11 at 15:38
  • I have updated the code to reflect your first comment. Note that i changed the upload url to just /upload/ instead of /upload/action/. I am not quite sure what you are talking about in your second comment. Are you trying to pass a value from django to the template, or from the browser to django? – marue Mar 08 '11 at 16:08
  • You can only set the "action" of the uploadbutton to the correct url value, as shown above. How you do that is more of a javascript issue. For further clearance on uploading files via ajax have a look at this: http://stackoverflow.com/questions/543926/is-it-possible-to-ajax-a-file-upload/543927#543927. The Django code i listed in my answer should work when you point the upload to the correct url. Can you give an example on the urls your js code generates? And maybe you can post the error messages django throws, that would help a lot. – marue Mar 08 '11 at 16:51
  • You're right, it's a matter of javascript. I just let the action fields of form empty, and have written a javascript function that defined the URL of the form action based on the chosen model. Thanks a lot! – green69 Mar 09 '11 at 11:48