简体   繁体   中英

How can I fix the No Reverse Match in django?

I'm having this error and I don't know why, here's my code.

from django.urls import path
from basic_app import views

app_name = 'basic_app'

urlpatterns = [
    path('relative/', views.relative, name = 'relative'),
    path('other/',views.other,name='other')
]
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <h1>WELCOME TO RELATIVE URL TEMPLATES</h1>
    <a href="{% url 'basic_app: other' %}">Link</a>
    
</body>
</html>

I'm not sure why I have this error because im putting the format as it is, or is there any updates in it?

I'm guessing it has something to do with the syntax. I'm not sure if you need to specify the app name in the html, as the view handles that.

If you just change the href to "{% url 'other' %}" I think it should work.

However I think it's better to have the name of the app in the name of the path which is what I think you wanted to do so if you change

path('other/',views.other,name='other')

to

path('other/',views.other,name='basic_app: other')

it should also work

As Tilen Pintarič said, your syntax is probably incorrect.

However, if you change your href to "{% url 'other' %}", it could cause an error because your url is namespaced.

From the django docs

If you'd like to retrieve a namespaced URL, specify the fully qualified name:
{% url 'myapp:view-name' %}



Instead, you should try removing the space between 'basic_app' and 'other'.
Edit your template tag from

<a href="{% url 'basic_app: other' %}">Link</a> to

<a href="{% url 'basic_app:other' %}">Link</a>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM