Reputation: 25
How to fix Page not found (404) error django2.0 for profile pages
this code
views code
'''
def profile(request, slug):
profile = Profile.objects.get(slug=slug)
context = {
'profile':profile,
}
return render(request, 'registration/profile.html' ,context)
''' and this urls.py
'''
from django.urls import path,re_path
from . import views
from django.contrib.auth.views import LoginView,logout #login
app_name='accounts'
urlpatterns = [
path(r'', views.home, name ='home'),
# path(r'^login/$', login, {'template_name':'registration/login.html'}),
path('login/', LoginView.as_view(), name="login"),
path(r'^logout/$', logout, name='logout'),
# path(r'^signup/$', views.register, name='register'),
path('signup/', views.register, name='signup'),
path(r'^(?P<slug>[-\w]+)/$', views.profile, name='profile'),
# path(r'^(?P<slug>[-\w]+)/edit$', views.edit_profile, name='edit_profile'),
]
'''
profile.html page in templates /registration folder
Upvotes: 0
Views: 51
Reputation: 308779
If you are using path()
then you shouldn't use regexes like r'^logout/$'
and r'^(?P<slug>[-\w]+)/$
.
Replace the following two URL patterns
path(r'^logout/$', logout, name='logout'),
path(r'^(?P<slug>[-\w]+)/$', views.profile, name='profile'),
with these:
path('logout/', logout, name='logout'),
path('<slug:slug>/', views.profile, name='profile'),
Upvotes: 1