我是Django 2.0的新手,在访问我的profile页面视图时遇到了这个错误。它使用的是path('users/<int:id>')
这样的url,但我希望url像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>
为什么不简单地改变你的路径:
然后在html中执行以下操作:
^{pr2}$另一种方法是:
在你的配置文件模板中使用请求.用户访问用户数据
编辑:
尝试重写
get_object
方法,如here所述您需要告诉您的视图使用}来实现这一点,或者重写{}。例如:
username
作为查找字段。您可以通过在模型上定义slug_field
和{第一个决定在模型查找中使用哪个字段;第二个决定从URL模式使用什么变量。在
相关问题 更多 >
编程相关推荐