django - get_sucess_url 在 django 中如何工作?

标签 django django-urls django-class-based-views

我正在查看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/

相关文章:

python - 重构Django基于类的 View ,清理18个重复类。

python - Django 和 Python 6 兼容模块

Django 'str' 对象在表单提交时不可调用

python - Django url 没有事件与给定的查询匹配?

python - 如何将隐藏的输入值插入django中的数据库

django - 从 Django 中基于类的通用 View 将 request.user 对象发送到 ModelForm

sql - Django 中的数据库问题 : can't reset because of dependencies

python - Django 可以和 py2exe 一起使用吗?

Django SESSION_COOKIE_DOMAIN

解析url中特殊字符的Django问题