Search code examples
djangoangularjsdjango-rest-frameworkipdjango-csrf

Allow all ips in CSRF_TRUSTED_ORIGIN django


How to allows all/ any ips in CSRF_TRUSTED_ORIGIN of django Backend django restapi are running and frontend is on angular in one system and we are trying to access with system ip in another system, i am able to access frontend and while accessing backend POST method API's are not working it's showing not found in csrf trusted origins. In settings.py i made get dynamic ips.

import socket
def get_ipaddress(): 
    
   host_name = socket.gethostname()
   ip_address = socket.gethostbyname(host_name) 
   return "http://"+ip_address+":4200"

ALLOWED_HOSTS=["*"]
CSRF_TRUSTED_ORIGINS=[get_ipaddress()]

Tried to use csrf_excempt , but it's not working. Version of django4.0.1, Angular 16


Solution

  • socket.gethostbyname(host_name.local)
    

    When used the above line in the get_ipaddress, it got worked