django page not found - The current path, users/login/, didn't match any of these

720 Views Asked by At

New to Django, doing a tutorial from the Python crashcourse book. I run into an error

Page not found (404) Request Method: GET Request URL: http://127.0.0.1:8000/users/login/?next=/topics/

Using the URLconf defined in learning_log.urls, Django tried these URL patterns, in this order:

admin/
[name='index']
topics/ [name='topics']
topics/<int:topic_id>/ [name='topic']
new_topic/ [name='new_topic']
new_entry/<int:topic_id>/ [name='new_entry']
edit_entry/<int:entry_id>/ [name='edit_entry']
login/ [name='login']
logout/ [name='logout']
register/ [name='register']
login/

The current path, users/login/, didn't match any of these.

You're seeing this error because you have DEBUG = True in your Django settings file. Change that to False, and Django will display a standard 404 page.


I have tried the solutions to the similar questions. Without any success. Thanks in advance for having a look at it:)

Basically what happens:

I add the code

@login_required

to the topics function in the views file for the learning_logs app. Which should redirect to a login page when user is not logged in. However I get the above mentioned error.

My users\urls file """Defines URL patterns for users"""

from django.urls import path
from django.contrib.auth import views as auth_views

from . import views

app_name = 'users'
urlpatterns = [
# Login page.
path('login/',auth_views.LoginView.as_view(template_name='users/login.html'),
    name='login'),

# Logout page
path('logout/', auth_views.LogoutView.as_view(), name='logout'),

# Registration page.
path('register/', views.register, name='register'),]

My learning_logs\urls file

""Defines URL patterns for learning_logs."""

from django.urls import path

from . import views

app_name = 'learning_logs'
urlpatterns = [
    # Home page.
    path('', views.index, name='index'),

    # Show all topics.
    path('topics/', views.topics, name='topics'),

    # Detail page for a single topic.
    path('topics/<int:topic_id>/', views.topic, name='topic'),

    # Page for adding a new topic
    path('new_topic/', views.new_topic, name='new_topic'),

    # Page for adding a new entry
    path('new_entry/<int:topic_id>/', views.new_entry, name='new_entry'),

    # Page for editing an entry
    path('edit_entry/<int:entry_id>/', views.edit_entry, name='edit_entry')
    ]

My settings file

***--snap***
# My settings
LOGOUT_REDIRECT_URL = '/'
LOGIN_URL = '/users/login/'

my learning_logs\views file**

***--snap***

@login_required
def topics(request):
    """Show all topics."""
    topics = Topic.objects.order_by('date_added')
    context = {'topics': topics}
    return render(request, 'learning_logs/topics.html', context)

***snap***

project Url file

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


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

There are 1 best solutions below

0
Hisham___Pak On

In your main urls.py you define,

path('', include('users.urls')),

which is empty and it instead should be,

path('users/', include('users.urls')),