문제

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.

도움이 되었습니까?

해결책

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/$', 'accounts.views.custom_register', name='registration_register'),

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

다른 팁

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.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top