Home > Software engineering >  Django url template tag refers to url with trailing question mark
Django url template tag refers to url with trailing question mark

Time:03-05

This is a question concerning a trailing question mark that gets appended to the end of the URL rendered by Django's url template tag. My goal is to have this trailing question mark removed, yet I do not know how.

On my site's main page, I have a button - a form HTML tag, really - that directs me to another link of the website.

The form looks like:

<form action={% url 'my_index' %}>
   <input type="submit" value="some value" />
</form>

When clicking on that button, I am directed to the URL corresponding to the my_index view. The problem, however, is that at the end of that URL, there is a "/?" that is appended - that is, the URL is in the form "http://127.0.0.1:8000/my_app/?". I want to remove this /? at the end and also want to understand why this trailing question mark got appended.

My urls.py pointed to by the ROOT_URLCONF IS

urlpatterns = [
    path('', include('my_app.urls')),
]

And my urls.py within my_app app looks like:

urlpatterns = [
    path('', views.index, name='my_index'),
]

Any advice is appreciated.

CodePudding user response:

you are making an GET request (default for a form) with just a submit button but without an input value. After the ? you would normally see the input values that you send with the GET request like

http://127.0.0.1:8000/my_app/?my_value=12345

As your form is empty there is no parameter appended. You could use a

<a href='{% url 'my_index' %}'> 

if you just want to call a page without any parameter

CodePudding user response:

You are sending an empty form. The default method of your form is GET. So the “?” is necessary to send the form...

Why don’t you use a normal link instead an empty form?

  • Related