3

I have this code :

My view :

members_pj(request,lesc,p,np):
    ...

My templates :

... href="{% url 'members_pj' lesc p np %}" ...

And my urls :

url(r'^lesps/(?P<np>\w+)/members','members_pj', name="memberspj"), ...

In fact, I don't want there are paramaters lesc and p in url... just np. How is it done?

Thank's

YassVegas
  • 169
  • 5
  • 18

1 Answers1

3

Django's url tag reverses a view name and parameters to a url, which means the url config has to be consistent with the view's parameters. What you currently have I think would result in a NoReverseMatch django error. To avoid that, change your url config to:

url(r'^lesps/(?P<lesc>\w+)/(?P<p>\w+)/(?P<np>\w+)/members','members_pj', name="memberspj"), ...

That will make the link resolve to the proper view via the url config.

However, the next thing is that you don't want the parameters to show up in your url. Generally, GET requests (which is what are issued when you click on a link) do not have a body, which means everything is in either headers or the url. If you want to send that information through, but not have it in the url, you'll need to use javascript to construct a request. Here's an example:

In your view:

<a href="{% url 'members_pj' np %}" data-lesc="{{lesc}}" data-p="{{p}}">
    Content
</a>

In javascript:

// From http://stackoverflow.com/a/133997/1467342
function post(path, params, method) {
    method = method || "post"; // Set method to post by default if not specified.

    // The rest of this code assumes you are not using a library.
    // It can be made less wordy if you use one.
    var form = document.createElement("form");
    form.setAttribute("method", method);
    form.setAttribute("action", path);

    for(var key in params) {
        if(params.hasOwnProperty(key)) {
            var hiddenField = document.createElement("input");
            hiddenField.setAttribute("type", "hidden");
            hiddenField.setAttribute("name", key);
            hiddenField.setAttribute("value", params[key]);

            form.appendChild(hiddenField);
         }
    }

    document.body.appendChild(form);
    form.submit();
}

var links = document.querySelectorAll('a');
for (var i = 0; i < links.length; i++) {
    links[i].addEventListener('click', function (evt) {
        evt.preventDefault();
        post(this.getAttribute('href'), {
            lesc: this.getAttribute('data-lesc'),
            p: this.getAttribute('data-p')
        });
    });
}
jstaab
  • 2,386
  • 21
  • 34