Search code examples
djangotemplatesurlhref

In Django, the button url is called unexpectedly, The url request is excuted differently


In Django, the button url is called unexpectedly, so I can not find the page

I tried click below button but it's not work

<button type="button" 
        class="btn btn-outline-info btn-sm float-right" 
        name="button" 
        onclick="location.href='{{fn_id}}/finisher/new'">
            post
</button>

result of url request is

enter image description here

It seems that there is a problem because I made the url request when I was on the detail page.

and below buuton is work

<button
    type="button"
    class="btn btn-outline-info btn-sm float-right"
    name="button" onclick="location.href='http://127.0.0.1:8000/bestlec/{{fn_id}}/finisher/new'"
    >
        post
</button>

but That causes problem Because it requests a local address

do you know how to slove it?? thanks for let me know~!


Solution

  • In your first example, I expect you're on page http://127.0.0.1:8000/bestlec/1. You've then given a relative url of 1/finisher/new which has been added to the current url to give http://127.0.0.1:8000/bestlec/1/1/finisher/new.

    If you want to give a relative url use finisher/new. If you need to go to other ids use /bestlec/{{fn_id}}/finisher/new or better still, use {% url 'url-name' fn_id %}