"Noreversematch at/" on Django, when I try to put a link on the button

Asked

Viewed 3,644 times

1

When I try to put a link on a button appears a page with error of NoReverseMatch at/, I don’t know what else I do, I want to put this link to go to a app registration of users that is called login.

<button href="{% url "login.views.registrar" %}" class="btn btn-danger">Cadastre-se</button>

Exception Type: Noreversematch Exception Value:

Reverse for '' with Arguments '()' and keyword Arguments '{}' not found. 0 >Pattern(s) tried: []

  • Try removing quotes from tags url. Ex.: {% url article.views.post_new %} instead of {% url "article.views.post_new" %}. Behold that question on Soen for more details (if this is the problem, comment on whether it worked, and I put it as an answer, I think it depends on the version). By the way, when you ask a question please include the relevant code snippets, not just an external link. In the case of an error involving exceptions, a stack trace usually helps too. Feel free to [Edit] your question if you want/can.

  • It didn’t work by taking out the quotes. Thanks for the hint of formatting, I think it’s like this now right? : D

  • I noticed that in the your urls.py there is no standard referencing login.views.registrar, nor an inclusion of login/urls.py (as you did with the article - url(r'', include('article.urls'))). If no Roteia URL for that view, so there’s no way to link to it. Try including it in your urls.py [main] something like url(r'login/', include('login.urls')) (only one example).

  • 2

    Worked perfectly @mgibsonbr. ;<a href="{% url "login.views.registrar" %}" class="btn btn-danger">Cadastre-se</a> In the main urls url(r'login/', include('login.urls')) With some modifications on the outside, such as the template link in views. return render(request, "login/registrar.html", {"form": UserCreationForm() })

1 answer

1

A mistake NoReverseMatch is a consequence of Django not being able to find a URL in his mapping system that solves for a given view, either in the template (tag url) or in Python code (reverse). In your case, the error is giving when trying to form a URL to login.views.registrar, then it is necessary to see if there is any pattern in the urls.py But for this one view.

In his complete example (in original review question) I see that there is nothing in the urls.py main (project) route to any view of login:

urlpatterns = [
    url(r'^admin/', include(admin.site.urls)),
    url(r'', include('article.urls'))
]

How do you already own one urls.pyin the app login, with a mapping to the view desired:

urlpatterns = [
    url(r'^registrar/new/$', views.registrar, name='registrar'),
]

So all you need to do is add a route to the app login, as you did to article. The URL type is yours to choose, but by way of example, assuming your site is on http://example.com/django/ and you configure your urlpatterns that way:

urlpatterns = [
    url(r'^admin/', include(admin.site.urls)),
    url(r'', include('article.urls'))
    url(r'login/', include('login.urls'))
]

Then the tag {% url "login.views.registrar" %} will be replaced by:

http://example.com/django/login/registrar/new/

And this URL will serve the result of view login.views.registrar. Feel free to choose another route if you wish, the important thing is that some routing to the view exists in order to avoid the NoReverseMatch.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.