How to make a logout model that redirects the user to the homepage while keeping the language of the user. For example: /home/fr/ instead of/home/en/?
How to make a logout model that redirects the user to the homepage while keeping the language of the user
class LogOut(Page):
def serve(self, request):
# Redirect to /home/fr/ not to /home/en/
return HttpResponseRedirect('????')
You can achieve this by accessing the user's current language from the request and constructing the redirect URL accordingly
I don't know if this is the best but I found the solution below. I still have to find how to allow the user to stay on the page from which he logged in, that is to say without redirection.
class LogIn(Page):
heading = models.CharField(default="We connect you with the world largest community", blank=True, max_length=275)
instructions = StreamField([
('line_1', blocks.CharBlock(default="Connect with your community via COGES",required=False, blank=True)),
('line_2', blocks.CharBlock(default="Discover the best experience with fellowship",required=False, blank=True)),
('line_3', blocks.CharBlock(default="Create your wishlist and enjoy to best experience",required=False, blank=True)),
], block_counts={
'line_1': {'max_num': 1},
'line_2': {'max_num': 1},
'line_3': {'max_num': 1},
}, max_num=3, blank=True)
form_title = models.CharField(default="Sign in to your account!",blank=True, max_length=100)
message = models.CharField(default="Nice to see you! Please log in with your account.", blank=True, max_length=100)
user_email_label = models.CharField(blank=True, max_length=50)
user_email_placeholder = models.CharField(default="E-mail",blank=True, max_length=50)
password_label = models.CharField(default="Password",blank=True, max_length=50)
keep_signed = models.CharField(default="keep me signed in", blank=True, max_length=100)
send_bttn_text = models.CharField(default="login",blank=True, max_length=50)
no_account_mess = models.CharField(default="Nice to see you! Please log in with your account.", blank=True, max_length=100)
connect_error_mess = models.CharField(default="Your login informations are incorrects. Please enter them again.", blank=True, max_length=100)
content_panels = Page.content_panels + [
MultiFieldPanel(
[
FieldPanel("heading"),
FieldPanel("instructions"),
FieldPanel("form_title"),
FieldPanel("message"),
],
heading="Login top infos",
classname="collapsible collapsed",
),
MultiFieldPanel(
[
FieldPanel("user_email_label"),
FieldPanel("user_email_placeholder"),
FieldPanel("password_label"),
FieldPanel("keep_signed"),
FieldPanel("send_bttn_text"),
FieldPanel("no_account_mess"),
],
heading="Login form content",
classname="collapsible collapsed",
),
]
def serve(self, request, *args, **kwargs):
response = super().serve(request)
active_lang_code = Locale.get_active().language_code
error = False
if len(request.POST) > 0:
user_email = request.POST.get("useremail") # form.cleaned_data['username']
user_password = request.POST.get("password") # form.cleaned_data["password"]
user = authenticate(username=user_email, password=user_password)
if user: # If the returned object is not None
auth_login(request, user) # we connect the user
# Redirect to blog index page
if active_lang_code == "en":
return HttpResponseRedirect('/')
else:
return HttpResponseRedirect('/{0}/'.format(active_lang_code))
else: # otherwise an error will be displayed
error = True
response.context_data['error'] = error
return response
#logout
class LogOut(Page):
def serve(self, request):
response = super().serve(request)
active_lang_code = Locale.get_active().language_code
auth_logout(request)
# Redirect to home page
if active_lang_code == "en":
return HttpResponseRedirect('/')
else:
return HttpResponseRedirect('/{0}/'.format(active_lang_code))
return response
If you're just going to the home page then return HttpResponseRedirect('/')
is all you need. This will forward the user onto the home page relevant to their activated locale. If that's not happening, there's something wrong with your site's translation setup.
If you need a specific path, for the site root page:
site = Site.find_for_request(request)
return HttpResponseRedirect(site.root_page.localized.url)