Home > Back-end >  DJANGO cant navigate to page using a hyperlink but typing page address works fine
DJANGO cant navigate to page using a hyperlink but typing page address works fine

Time:02-20

doing a simple django project with 2 apps one for blogs and the other is for users both work fine but i cant navigate using the hyperlink in this page

<p>
<a href="{% url 'learning_logs:home' %}">Home</a>
<a href="{% url 'learning_logs:topics' %}">Topics</a>
{% if user.is_authenticated %}
    Hello, {{ user.username }}.
    <a href="{% url 'users:logout' %}">logout</a>
{% else %}
    <a href="{% url 'users:login' }">login</a>
{% endif %}

urls.py in users

    from django.urls import URLPattern, path
    from django.contrib.auth.views import LoginView
    from . import views
    app_name = 'users'
    urlpatterns = [
    path('login/', LoginView.as_view(template_name='login.html'), name='login'),
    path('logout/', views.logout_view, name='logout'),
    ] 

main urls of project

from django.contrib import admin
from django.urls import path, include

urlpatterns = [
    path('admin/', admin.site.urls),
    path('', include('learning_logs.urls', namespace='learning_logs')),
    path('users/', include('users.urls', namespace='users')),
    ]

error message

Page not found (404)
Request Method: GET
Request URL:    http://127.0.0.1:8000/{% url 'users:login' }
Using the URLconf defined in learning_log.urls, Django tried these URL patterns, in this order:

admin/
[name='home']
topics [name='topics']
topics/<topic_id> [name='topic']
new_topic [name='new_topic']
new_entry/<topic_id> [name='new_entry']
edit_entry/<entry_id> [name='edit_entry']
users/
The current path, {% url 'users:login' }, didn’t match any of these.

it shows the page if i type /users/login/ in the address bar

CodePudding user response:

You forgot % in login inside your html file. Change your html code from

<a href="{% url 'users:login' }">login</a>

To

<a href="{% url 'users:login' %}">login</a>

CodePudding user response:

Remove the quotes

{% if user.is_authenticated %}
   <a href="{% url users:logout %}">logout</a>
{% else %}
   <a href="{% url users:login %}">login</a>
{% endif %}
  • Related