我正在尝试为我的URL
提供更个性化的设计,因此我希望每个用户都可以按照以下路径访问他们的个人资料:www.mysite/profile/myname
我已经尝试在 URL 上进行了尝试:
path('profile/<username>/', user_views.profile, name='profile')
但是当我去http://localhost:8000/profile/gg/
时,没有个人资料,错误是:
profile()收到了意外的关键字参数“用户名”
谢谢
views.py
def profile(request):
if request.method == 'POST':
u_form = UserUpdateForm(request.POST, instance=request.user)
p_form = ProfileUpdateForm(request.POST,
request.FILES,
instance=request.user.profile)
if u_form.is_valid() and p_form.is_valid():
u_form.save()
p_form.save()
messages.success(request, f'Your account has been updated!')
return redirect('profile')
else:
u_form = UserUpdateForm(instance=request.user)
p_form = ProfileUpdateForm(instance=request.user.profile)
context = {
'u_form': u_form,
'p_form': p_form
}
return render(request, 'users/profile.html', context)
答案 0 :(得分:0)
您需要在方法签名中指定参数,即更改
def profile(request):
至:
def profile(request, username):