Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
389 views
in Technique[技术] by (71.8m points)

django - Page is not found

I have the following urls:

urlspatterns=[  path('<str:urlsername>/', views.profile, name='profile'),
    path('login/', auth_views.LoginView.as_view(),name ='login'),
    path('logout/', auth_views.LogoutView.as_view()),
    path('password_change/', auth_views.PasswordChangeView.as_view(),name='password_change'),]

In views for the first url I have this:

def profile(request, username):
    user = get_object_or_404(User, username = username)
    posts = Post.objects.filter(author = user).order_by('-pub_date')
    return render(request,'profile.html', {'posts':posts,'user':user})

So when I go to the page login, logout or password_change I get the error "Page is not found"

Request Method: GET
Request URL:    http://127.0.0.1:8000/password_change/
Raised by:  posts.views.profile

But if I comment the profile view and its url everything works just fine. Why does the url 'logout' etc goes to profile view?

question from:https://stackoverflow.com/questions/65870606/page-is-not-found

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

I think password_change will match <str:urlsername> first. So the solution is change your urlspatterns to the code bellow.

urlspatterns=[  
    path('login/', auth_views.LoginView.as_view(),name ='login'),
    path('logout/', auth_views.LogoutView.as_view()),
    path('password_change/', auth_views.PasswordChangeView.as_view(),name='password_change'),
    path('<str:urlsername>/', views.profile, name='profile')
]


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...