我正在研究Django 1.11
我想从视图中重定向用户。
myapp/accounts/urls.py
app_name = 'accounts'
urlpatterns = [
url(r'^$', views.ProfileView.as_view(), name='profile'),
url(r'^profile/', views.ProfileView.as_view(), name='profile')
]
并在myapp/accounts/views.py
from django.contrib.auth.models import User
# Create your views here.
from django.http import HttpResponseRedirect
from django.urls import reverse
from django.views.generic import TemplateView, UpdateView
class ProfileView(TemplateView):
template_name = 'accounts/profile.html'
class UpdateProfile(UpdateView):
model = User
fields = ['first_name', 'last_name']
template_name = 'accounts/update.html'
def get_object(self):
return self.request.user
def get_success_url(self):
messages.success(self.request, 'Profile Updated Successfully')
return HttpResponseRedirect(reverse('profile'))
这在重定向时出错。
NoReverseMatch at /accounts/update/
Reverse for 'profile' not found. 'profile' is not a valid view function or pattern name.
如何在更新个人资料详情后将用户重定向到profile
页面?
如何将用户重定向到其他应用的视图?
答案 0 :(得分:1)
首先,您需要提供unique names
。
urlpatterns = [
url(r'^$', views.ProfileView.as_view(), name='profile'),
url(r'^profile/', views.ProfileView.as_view(), name='detailprofile')
]
立即强>
def get_success_url(self):
messages.success(self.request, 'Profile Updated Successfully')
return reverse('accounts:profile')