1

My code is below

template looks like this

  <td><button><a href="{%  url 'testschema' allschema.schema_name %}"> Test</a></button></td>
   <td><button><a href="{%  url 'deleteschema' allschema.schema_name %}"> Delete</a></button></td>

url patterns

urlpatterns = [
    path('<int:id>/', views.confighome, name='config'),
    path('<str:schmid>/', views.deleteschema, name='deleteschema'),
    path('te<str:schmid>/', views.testschema, name='testschema')

]

views.py

def deleteschema(request,schmid):
    some code
    return redirect('/configuration/'+str(request.session["project_id"]))

def testschema(request,schmid):
   some code
    return redirect('/configuration/'+str(request.session["project_id"]))

Whenever I click on the Testbutton its actually calling the delete function

Any idea why this happening Since I used named url parameters

Thanks in advance

1
  • The url will always match the second path, since each string that starts with te is a string. Commented Nov 18, 2019 at 14:41

1 Answer 1

1

The url will always match the second path(..), since each string that starts with te is a string. You therefore better make the URLs non-overlapping, as in that no URL that matches the second path(..) can match the third path(..). Regardless what URL the {% url 'testschema' allschema.schema_name %} thus generates, if the browser sends a request with that URL, it will be matched by the second path(..).

For example:

urlpatterns = [
    path('<int:id>/', views.confighome, name='config'),
    path('de<str:schmid>/', views.deleteschema, name='deleteschema'),
    path('te<str:schmid>/', views.testschema, name='testschema')
]

or perhaps more convenient:

urlpatterns = [
    path('<int:id>/', views.confighome, name='config'),
    path('<str:schmid>/delete/', views.deleteschema, name='deleteschema'),
    path('<str:schmid>/test/', views.testschema, name='testschema')

]
Sign up to request clarification or add additional context in comments.

2 Comments

then why are we giving the named url in the template .?. I am new to django, so still learning
@user3048099: to calculate the URL in reverse. But if there is a "collision" that does not work of course. Except for the URL, there is nothing that is in the template to direct it to the proper URL. The name is only used to "calculate" how the URL should look like so to speak.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.