Redirects
Redirect users to different pages from component methods.
Syntax
self.redirect_to(url)
The redirect is performed client-side - the browser navigates to url via
window.location after the response is applied. It isn't an HTTP redirect, so
there's no 301/302 status involved.
Examples
Basic Redirect
class LoginForm(Component):
email: str = ""
password: str = ""
def submit(self):
user = authenticate(self.email, self.password)
if user:
login(self.request, user)
self.redirect_to('/dashboard/')
else:
self.errors = {"email": "Invalid credentials"}
Named URLs with Arguments
from django.urls import reverse
class PostEditor(Component):
post_id: int
def view(self):
self.redirect_to(reverse('posts:detail', args=[self.post_id]))