I'm trying to implement a login functionality to a small Django project but when I go to the page with signup/signin form I get the 404 error. I am using Django default user model and auth forms.
here are the views:
def signup(request):
if request.user.is_authenticated:
return redirect('/')
if request.method == 'POST':
form = UserCreationForm(request.POST)
if form.is_valid():
form.save()
username = form.cleaned_data.get('username')
password = form.cleaned_data.get('password1')
user = authenticate(username=username, password=password)
login(request, user)
return redirect('/')
else:
return render(request, 'signup.html', {'form': form})
else:
form = UserCreationForm()
return render(request, 'signup.html', {'form': form})
def signin(request):
if request.user.is_authenticated:
return render(request, 'homepage.html')
if request.method == 'POST':
username = request.POST['username']
password = request.POST['password']
user = authenticate(request, username=username, password=password)
if user is not None:
login(request, user)
return redirect('/')
else:
form = AuthenticationForm(request.POST)
return render(request, 'signin.html', {'form': form})
else:
form = AuthenticationForm()
return render(request, 'signin.html', {'form': form})
def log_out(request):
logout(request)
return redirect('/')
here are the urls for my views:
urlpatterns = [
path('', views.PostList.as_view(), name='home'),
path('<slug:slug>/', views.post_detail, name='post_detail'),
path('signup/', views.signup, name='register'),
path('signin/', views.signin, name='signup'),
path('log_out/', views.log_out, name='log_out'),
]
project urls:
urlpatterns = [
path('admin/', admin.site.urls),
path('', include('blog_backend.urls'))
]
full error message:
Page not found (404)
Request Method: GET
Request URL: http://127.0.0.1:8000/signin/
Raised by: blog_backend.views.post_detail
No Post matches the given query.
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.
path('<slug:slug>/', views.post_detail, name='post_detail'),
path('signup/', views.signup, name='register'),
You have the URL pattern for post_detail
above the signup
pattern, so signup/
is being handled by the post_detail
view.
The post_detail
view tries to find a post with slug='signup'
, and gives a 404 error because it doesn't exist.
You can fix the problem by changing the post_detail
URL so that it doesn't match signup
, for example:
path('blog/<slug:slug>/', views.post_detail, name='post_detail'),
path('signup/', views.signup, name='register'),
Or switch the order of your URL patterns, so that the authentication URLs match first:
path('signup/', views.signup, name='register'),
path('signin/', views.signin, name='signup'),
path('log_out/', views.log_out, name='log_out'),
path('<slug:slug>/', views.post_detail, name='post_detail'),
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.