【发布时间】:2020-10-03 20:25:37
【问题描述】:
我有两个独立的用户,我希望一些用户能够修改其他用户的个人资料。我现在面临的问题是如何获取用户 ID。我对此有两个意见。纵断面图,编辑纵断面图。我试图将整个配置文件 html 包装在一个表单中,我不确定这是否可行。 大多数示例仅显示如何编辑当前(登录)用户的个人资料。
指向特定用户个人资料的链接 (http://127.0.0.1:8000/dashboard/profile/dfrtgy-ehehh/16) 个人资料页面有一个指向编辑个人资料页面的链接。
个人资料html
<li>
<a href="{% url 'editprofile' %}"><img class="nav-items" src="{% static 'images/lab.svg'%}" alt=""><span>Edit profile</span>
</a>
</li>
views.py
def edit_profile(request):
if request.method == 'POST':
profile = Profile.objects.get(pk=pk)
form = EditProfileForm(request.POST, instance=request.user.profile)
if form.is_valid():
form.save()
return redirect(f'/dashboard/profile/{request.user.profile.slug}/{request.user.pk}')
else:
form = EditProfileForm(instance=request.user.profile)
args = {'form': form}
return render(request, 'core/editprofile.html', args)
def profile(request, slug, pk):
profil = Profile.objects.get(slug=slug)
profile = Profile.objects.get(pk=pk)
context = {'profile': profile, 'profil': profil}
return render(request, 'dashboard/profile.html', context)
urls.py
urlpatterns = [
path('', dashboard, name='dashboard'),
path('profile/', profile, name='profile'),
path('profile/<str:slug>/<int:pk>', profile, name='profilepk'),
path('edit_profile/', edit_profile, name='editprofile'),
【问题讨论】: