我正在查看django-registration
的源代码
。这是定义的基于类的 View 。我很难理解 get_success_url
的工作原理?
来自 documentation ,
get_success_url()
Determine the URL to redirect to when the form is successfully validated.
Returnsdjango.views.generic.edit.ModelFormMixin.success_url if it is provided;
otherwise, attempts to use the get_absolute_url() of the object.
但是在下面的示例代码中这是如何工作的:
为什么两个参数为空
?他们应该拿什么?
class ActivationView(BaseActivationView):
def activate(self, request, activation_key):
"""
Given an an activation key, look up and activate the user
account corresponding to that key (if possible).
After successful activation, the signal
``registration.signals.user_activated`` will be sent, with the
newly activated ``User`` as the keyword argument ``user`` and
the class of this backend as the sender.
"""
activated_user = RegistrationProfile.objects.activate_user(activation_key)
if activated_user:
signals.user_activated.send(sender=self.__class__,
user=activated_user,
request=request)
return activated_user
def get_success_url(self, request, user):
return ('registration_activation_complete', (), {})
最佳答案
这三个参数被传递给Django的reverse URL lookup ,特别是 django.core.urlresolvers.reverse 函数。 ()
(空元组)给出位置参数,{}
(空字典)给出关键字参数。所以最终通过的是:
reverse('registration_activation_complete', args=(), kwargs={})
您可以在 urls.py 中看到文件中的 registration_activation_complete
URL 不带任何参数(URL 只是 activate/complete/$
),这就是为什么这些内容为空。
关于django - get_sucess_url 在 django 中如何工作?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22643725/