django redirect to external url
To redirect to an external URL in Django, you can follow these steps:
- Import the
HttpResponseRedirect
class from thedjango.http
module. - In your view function or class, create an instance of
HttpResponseRedirect
and pass the external URL as an argument. - Return the instance of
HttpResponseRedirect
as the response.
Here's an example of how to implement this:
from django.http import HttpResponseRedirect
def redirect_to_external(request):
external_url = "https://www.example.com"
return HttpResponseRedirect(external_url)
In this example, we import the HttpResponseRedirect
class from the django.http
module. Then, in the redirect_to_external
view function, we set the external_url
variable to the desired external URL. Finally, we return an instance of HttpResponseRedirect
with external_url
as the argument.
When this view is requested, Django will redirect the user to the specified external URL.