在 django-registration 中传播 ?next=

发布于 2024-09-08 07:48:06 字数 248 浏览 6 评论 0原文

我有一个具有 @login_required 装饰器的视图,并且在拉出页面时,如果您没有登录,它确实会将您发送到带有正确的登录表单?next=/url/you/tried/to/see

我的问题是如何将该 ?next 值传递给后续表单和激活电子邮件,以便当用户完成激活过程时,他们将被重定向到他们最初尝试访问的视图。

我的 Python'foo 和 Django'foo 很弱,所以请保留 5 岁孩子可以理解的答案;)

提前谢谢大家。

I have a view that has the @login_required decorator and upon pulling up the page, if you are not logged in it does send you to the login form with the proper ?next=/url/you/tried/to/see

My problem is how to I pass along that ?next value to the subsequent forms and activation email so that when the user completes the activation process they are redirected to the view they originally tried to get to.

My Python'foo and Django'foo is weak, so please keep the answer to something a 5 year old could follow ;)

Thanks all in advanced.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

喵星人汪星人 2024-09-15 07:48:06

内置视图默认处理此问题。确保您的登录页面模板中有这段代码:

Built in view handles this by default. Make sure you have this piece of code: <input type="hidden" name="next" value="{{ next }}" /> in your template of login page.

一枫情书 2024-09-15 07:48:06

您可以执行此操作

You can do this <a href="{% url 'app:sign_up' %}?next={{ next }}">

梦一生花开无言 2024-09-15 07:48:06

这很简单,只需保存 url 并编写您自己的后端重写 2 个方法即可。

accounts/models.py

class UserNext(models.Model):
    user = models.OneToOneField(User)
    url = models.CharField(max_length=255, blank=True, null=True)

accounts/nextbackend.py:

from registration.backends.default import DefaultBackend
from django.core.urlresolvers import resolve
from accounts.models import UserNext

class NextBackend(DefaultBackend):

    def register(self, request, **kwargs):
            username, email, password = kwargs['username'], kwargs['email'], kwargs['password1']
            if Site._meta.installed:
                site = Site.objects.get_current()
            else:
                site = RequestSite(request)
            new_user = RegistrationProfile.objects.create_inactive_user(username, email,
                                                                        password, site)
            signals.user_registered.send(sender=self.__class__,
                                         user=new_user,
                                         request=request)

            next, created = UserNext.objects.get_or_create(user=new_user)
            next.url = request.GET.get('next',None) # or POST, don't know how you want to pass it
            next.save()
            return new_user

    def post_activation_redirect(self, request, user):
        next = UserNext.objects.get(user=user)
        view, args, kwargs = resolve(next.url)
        return (view, args, kwargs)

用户这个^后端作为accounts/views.py中的注册后端:

def custom_register(request):
            return register(request, backend='accounts.nextbackend.NextBackend',)

urls.py:

url(r'^accounts/register/

应该可以工作,没有测试它,即时编写。显然需要添加一些导入。

, 'accounts.views.custom_register', name='registration_register'),

应该可以工作,没有测试它,即时编写。显然需要添加一些导入。

It's easy, just save the url and write your own backend overriding 2 methods.

accounts/models.py

class UserNext(models.Model):
    user = models.OneToOneField(User)
    url = models.CharField(max_length=255, blank=True, null=True)

accounts/nextbackend.py:

from registration.backends.default import DefaultBackend
from django.core.urlresolvers import resolve
from accounts.models import UserNext

class NextBackend(DefaultBackend):

    def register(self, request, **kwargs):
            username, email, password = kwargs['username'], kwargs['email'], kwargs['password1']
            if Site._meta.installed:
                site = Site.objects.get_current()
            else:
                site = RequestSite(request)
            new_user = RegistrationProfile.objects.create_inactive_user(username, email,
                                                                        password, site)
            signals.user_registered.send(sender=self.__class__,
                                         user=new_user,
                                         request=request)

            next, created = UserNext.objects.get_or_create(user=new_user)
            next.url = request.GET.get('next',None) # or POST, don't know how you want to pass it
            next.save()
            return new_user

    def post_activation_redirect(self, request, user):
        next = UserNext.objects.get(user=user)
        view, args, kwargs = resolve(next.url)
        return (view, args, kwargs)

user this^ backend as your registration backend in accounts/views.py:

def custom_register(request):
            return register(request, backend='accounts.nextbackend.NextBackend',)

urls.py:

url(r'^accounts/register/

Should work, didn't test it, written on the fly. Obviously will need adding some imports.

, 'accounts.views.custom_register', name='registration_register'),

Should work, didn't test it, written on the fly. Obviously will need adding some imports.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文