views:

29

answers:

2

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.

A: 

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.

Lukasz Dziedzia
That's in the login template but on that template is a register link. That register template doesn't pick-up on {{ next }} putting it into the template code renders a type="hidden" name="next" value="".
ChronosLLC
can you post your view code for login?
Lukasz Dziedzia
I don't have a view for login since django-registration integrates/uses the contrib.auth.in django-registration's urs.py there is:from django.contrib.auth import views as auth_viewsand then in the patterns(''...)there's: url(r'^register/complete/$', direct_to_template, {'template': 'registration/registration_complete.html'}, name='registration_complete'), )I think my main problem is I don't know how to pass the extra_context to these views that ship with django-registration.
ChronosLLC
I was able to add in the proper code to get request.REQUEST visible in all templates though - but still have an issue with the email that gets sent out through the default backend, it's not picking up or passing along the next value.
ChronosLLC
+2  A: 

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.

zalew
Thanks Zalew-I figured I could do something like this but am looking to integrate facebook connect at some point so if the solution was only to do a custom backend then I'll just do that when adding in FB down the road. Thanks again guys for the answers they really did help. The Stackoverflow community has got some awesome people.JY
ChronosLLC
http://github.com/flashingpumpkin/django-socialregistration should help if you want to register with external accounts, cheers.
zalew