出于某种原因,Redirect认为我对视图'clients.views.teacher_profile'的调用是一个URL,将其直接放在地址栏中,如下所示:
如何将其链接到视图而不将其视为URL?
注意:我更改了一些设置以适应django-allauth。
我的代码:
#views.py
def teacher_profile(request, username):
user = get_object_or_404(User, username=username)
context = {
'user':user,
'teacher':user.teacher,
}
return render(request, 'clients/teacher_profile.html', context)
def edit_profile(request):
teacher = get_object_or_404(Teacher, user=request.user)
if request.method == 'POST':
form = TeacherForm(request.POST, instance=teacher)
if form.is_valid():
teacher = form.save(commit=False)
teacher.user = request.user
teacher.save()
return redirect('clients.views.teacher_profile', username=request.user.username)
else:
form = TeacherForm(instance=teacher)
return render(request, 'clients/edit_profile.html', {'form':form})
#urls.py
urlpatterns = [
url(r'^list/$', views.teacher_list, name='teacher_list'),
url(r'^(?P<username>[\w.@+-]+)/$', views.teacher_profile, name='teacher_profile'),
url(r'^accounts/settings/$', views.edit_profile, name='edit_profile'),
]
答案 0 :(得分:3)
请勿在重定向调用中使用视图的模块路径;使用您在网址格式中明确定义的名称。
return redirect('teacher_profile', username=request.user.username)