【问题标题】:How to fix Page not found (404) error django2.0 for profile pages如何修复页面未找到(404)错误django2.0的个人资料页面
【发布时间】:2020-04-22 08:59:45
【问题描述】:

如何修复页面未找到(404)错误 django2.0 的配置文件页面

这段代码

查看代码

'''

def profile(request, slug):

    profile = Profile.objects.get(slug=slug)

    context = {

        'profile':profile,
    }

    return render(request, 'registration/profile.html' ,context)

''' 和这个 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页面

【问题讨论】:

  • 请在提问时包含完整的错误信息。目前我们无法确定是哪个 URL 为您提供了 404。

标签: python django


【解决方案1】:

如果您使用的是path(),那么您不应该使用像r'^logout/$'r'^(?P&lt;slug&gt;[-\w]+)/$ 这样的正则表达式。

替换以下两种网址格式

path(r'^logout/$', logout, name='logout'),
path(r'^(?P<slug>[-\w]+)/$', views.profile, name='profile'),

这些:

path('logout/', logout, name='logout'),
path('<slug:slug>/', views.profile, name='profile'),

【讨论】:

  • 非常感谢您解决问题
猜你喜欢
  • 2015-06-22
  • 1970-01-01
  • 2015-11-04
  • 2012-09-15
  • 1970-01-01
  • 2014-10-09
  • 2015-10-08
  • 2015-03-30
  • 1970-01-01
相关资源
最近更新 更多