Flask before_request

What am I missing here? I'm trying to redirect the user when they use 'www' to the correct domain but it seems that I'm missing something because it's not working, it gives me error šŸ˜¦
@app.before_request
def redirect_to_domain():
FROM_DOMAIN = "www.marsman.pythonanywhere.com"
TO_DOMAIN = "marsman.pythonanywhere.com"
urlparts = urlparse(request.url)
if urlparts.netloc == FROM_DOMAIN:
urlparts_list = list(urlparts)
urlparts_list[1] = TO_DOMAIN
return redirect(urlunparse(urlparts_list), code=301)
@app.before_request
def redirect_to_domain():
FROM_DOMAIN = "www.marsman.pythonanywhere.com"
TO_DOMAIN = "marsman.pythonanywhere.com"
urlparts = urlparse(request.url)
if urlparts.netloc == FROM_DOMAIN:
urlparts_list = list(urlparts)
urlparts_list[1] = TO_DOMAIN
return redirect(urlunparse(urlparts_list), code=301)
3 Replies
Unknown User
Unknown Userā€¢3y ago
Message Not Public
Sign In & Join Server To View
~MARSMAN~
~MARSMAN~OPā€¢3y ago
~MARSMAN~
~MARSMAN~OPā€¢3y ago
why it cannot import before-request šŸ¤”
Want results from more Discord servers?
Add your server