【发布时间】:2018-07-07 22:37:20
【问题描述】:
我是 Django 2.0 的新手,在访问我的个人资料页面视图时遇到此错误。它正在使用像path('users/<int:id>') 这样的网址,但我希望网址像path('<username>')。不确定到底是什么问题。我希望你能帮忙。
#views.py
class ProfileView(views.LoginRequiredMixin, generic.DetailView):
model = models.User
template_name = 'accounts/profile.html'
#urls.py
urlpatterns = [
path('', HomePageView.as_view(), name='home'),
path('signup', SignUpView.as_view(), name='signup'),
path('login', LoginView.as_view(), name='login'),
path('logout', logout_view, name='logout'),
path('<username>', ProfileView.as_view(), name='profile')
]
#base.html
<ul class="dropdown-menu">
<li><a href="{% url 'accounts:profile' user.username %}">View Profile</a></li>
<li><a href="#">Edit Profile</a></li>
</ul>
【问题讨论】:
-
你确定用户有
username吗?{% url 'accounts:profile' "bob" %}有效吗?