简体   繁体   English

Django重定向将视图视为URL

[英]Django Redirect treating view as a URL

For some reason, Redirect thinks my call to a view 'clients.views.teacher_profile' is a URL, putting it directly in the address bar as shown: 出于某种原因,Redirect认为我对视图“ clients.views.teacher_profile”的调用是一个URL,将其直接放在地址栏中,如下所示:

Page Not Found Screenshot 找不到页面截图

How do I link it to the view and not treat it as a URL? 如何将其链接到视图而不将其视为URL?

Note: I have altered some settings to accommodate django-allauth. 注意:我已经更改了一些设置以适应django-allauth。

My code: 我的代码:

#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'),
]

Don't use the view's module path in the call to redirect; 不要在调用中使用视图的模块路径进行重定向; use the name which you explicitly defined in the url pattern. 使用您在网址格式中明确定义的名称。

return redirect('teacher_profile', username=request.user.username)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在Django模板视图中重定向url? - How to redirect url in Django Template View? Django - 基于 Class 的通用视图 - “没有 URL 重定向到” - Django - Class Based Generic View - “No URL to redirect to” (DJANGO) 如何从视图重定向到另一个 URL? - (DJANGO) How to be redirect to another URL from a view? Django Generic查看UpdateView重定向URL和更新的slug - Django Generic View UpdateView redirect URL with updated slug Django - 从 javascript 传递数据以查看并重定向到新的 url - Django - Pass data from javascript to view & redirect to a new url Django不会从一个视图/ URL重定向到另一个 - Django does not redirect from one view/url to another 如何从视图重定向到 Django 中的根 URL? - How do I redirect from a view to a root URL in Django? Django / Python:如何创建从一个视图/ URL到另一个视图/ URL的重定向? - Django/Python: How to create redirect from one view/url to another? 在成功URL重定向不适用于django-allauth中的自定义视图 - On Success URL redirect not working for custom view in django-allauth “在Django视图中,不安全的重定向到具有协议&#39;content-type&#39;”的URL - “Unsafe redirect to URL with protocol 'content-type'” error in Django view
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM