0

Right now I have a very long URL for my results which is basically a long list of parameters. I navigate to the results by press of a button and (JavaScript):

window.location.href = ... URL with parameters here ...;

Catching them like this (views.py):

def Results(request,parameter1,...,parameterN)

How do I send these same parameters not inside the URL and how do I catch them in views?

EDIT:

To clarify, in a an optimal world I'd like to send the parameters hidden from the user and search engine crawlers so that the user/crawler sees only the URL:

www.example.com/results

And I do something like:

def Results(request):
    parameter1 = ???
    parameter2 = ???
    ...
    parameterN = ???
user1581390
  • 1,900
  • 5
  • 25
  • 38
  • Hard to say without a clearer example (imo), Why do you need so many parameters? are they all required? – Sayse Feb 16 '16 at 13:24
  • 1
    What exactly you want and also specify Method which you are using (GET or POST). – Kamlesh Feb 16 '16 at 13:32
  • @Sayse all the parameters are required. The results page makes a computation using all of them (physics data). – user1581390 Feb 16 '16 at 13:39
  • @Kjjassy I just paste the code to change the URL inside the button code that works on a click. I am not sure if this is GET or POST how do I tell? (I don't want to use the same method, I want them to be passed HIDDEN from the user). – user1581390 Feb 16 '16 at 13:41
  • Are you using jquery or anything or just VanillaJS? – Sayse Feb 16 '16 at 13:43
  • `HIDDEN` -> so use a html form and put your values hidden in the form and submit the form. Then access values using `request.POST.get('name', None);` – Mehran Torki Feb 16 '16 at 13:45
  • @user1581390 would you mind check my answer? – Mehran Torki Feb 19 '16 at 10:20
  • @MehranTorki there is a small typo a '=' is missing here: value""/>. Apart from that this is exactly what I wanted. – user1581390 Feb 20 '16 at 15:15

2 Answers2

5

When you need to hide parameters, you should send POST request. To send parameters using POST method, you can send them using html form element or javascript. For example you can pass parameters with form like this :

<form id="my_form" action="/results" method="POST">
    {% csrf_token %}
    <input type="hidden" name="param1" value=""/>
    <input type="hidden" name="param2" value=""/>
    <input type="hidden" name="param3" value=""/>
</form>

Populate hidden input elements in javascript like this:

var my_form = document.forms['my_form'];
my_form.elements["param1"].value = "Some Value1";
my_form.elements["param2"].value = "Some Value2";
my_form.elements["param3"].value = "Some Value3";

And to submit the form in javascript :

document.getElementById("my_form").submit();

Then access parameters in view :

def Results(request):
    if request.method == "POST":
        parameter1 = request.POST.get("param1", None)
        parameter2 = request.POST.get("param2", None)
        parameter3 = request.POST.get("param3", None)
    else:
        # handle get request

Read here if you want to send POST request using javascript: Sending an HTTP Post using Javascript triggered event

Community
  • 1
  • 1
Mehran Torki
  • 977
  • 1
  • 9
  • 37
1

You access the url parameters in the GET dictionary

www.example.com/?parm1=baz&&parm2=baz

In the view:

def ViewFunction(request):   
   parm1 = request.GET['parm1']
   parm2 = request.GET['parm2']

If these parameters are optional, you will want to use a default, like:

parm1 = request.GET.get('parm1',None)

Note that the GET parameters are not safe. You should escape them properly before using them in the code. An easy solution is to use a django form, pass the GET values to the form, and then use the form.cleaned_data.

When you have too many parameters, check your view and urls, maybe you can convert some parameters to a url captured args.

Aviah Laor
  • 3,620
  • 2
  • 22
  • 27