django - 请求的用户配置文件出现在两个 url
问题描述
对于登录用户,个人资料 url 是
path('profile/', profile, name='profile'),
对于个人资料视图,网址是
re_path(r'^profile/(?P<pk>\d+)/$',UserProfileView,name='my_profile'),
但是当我们尝试通过 '/profile/user_pk' 访问登录用户时,它与两个 url 中存在的一个配置文件冲突
视图.py
@login_required
def profile(request):
args={'user':request.user}
return render(request,'account/profile.html',args)
def UserProfileView(request,pk):
if pk==request.user.pk:
raise(Http404)
else:
user = get_object_or_404(User,pk=pk)
return render(request,'account/profile.html',{'user':user})
如果请求的 pk 与配置文件 pk 相同,那么它会引发 Http 否则显示其他配置文件,但现在仍在工作,仍然显示所有配置文件。
解决方案
推荐阅读
- r - 如何在R中的数字之间插入虚线
- reactjs - 从数组数据reactjs有条件地渲染页面
- python - 文本变量更改时标签不更新(Tkinter)
- angular-material - 如何在将现有组件留在前台的同时使用 CDK 覆盖?
- c# - 改进 LINQ to EF 生成的 SQL 查询
- android - 如何使用地理围栏创建一个 Android 应用程序,当用户到达目的地时,它会自动获得奖励而不做任何事情?
- jaxb - 为 xjc 优化外部绑定 XML
- asp.net - Dotnet 新 - 缺少 webpack.js
- spring - JPQL - 连接 - MultipleTables 和空 ResultList
- mysql - 比较同一列MySQL的计数以获得百分比