How to set allowed host in Django

How to set allowed host In python (Django) is very important ,securing your web application is important, and one crucial aspect of security is setting the allowed hosts correctly.

This setting in settings.py specifies which host or domain names that are allowed to access your Django application.

same question you can be searching is also

By restricting access to specified hosts, you can prevent unauthorized users from accessing your site, thereby enhancing your Django application security.

In this article I’ll walk you through the process of setting allowed hosts in Django.

Steps of how to set allowed_host in Django

  1. Open your Django project’s settings.py file.
  2. Locate the ALLOWED_HOSTS variable in the settings.py file.
  3. Set ALLOWED_HOSTS to a list of strings representing the host/domain names that Django should allow.
  4. Add the appropriate host/domain names to the list or use “* “.
  5. Save the settings.py file
  6. Restart your Django server to apply the changes.

How to set allowed host

Step 1: Accessing Your Django Project Settings

Navigate to your Django project directory using your preferred terminal or command prompt.

How to set allow host in Django

Open the settings.py file located within your project directory, not the app you created .

This file contains various configurations for your Django project , don’t play with it .

Step 2: Locating the ALLOWED_HOSTS Settings

hosting

Within the settings.py file, locate the ALLOWED_HOSTS setting.liie the picture above, This setting is typically found near the top of the file and is initially set to an empty list ([ ]).

Step 3: Configuring Allowed Host

host

To configure allowed hosts, you will need to specify the domain names or IP addresses that are allowed to access your Django application.

Update the ALLOWED_HOSTS setting by adding the desired hostnames or IP addresses as strings within the list in the settings.py.

Allow host, Allow host settings, Host not allowed, How to set allowed host, You have to configure allow host, You may have to add localhost,you may need to add allow host

For example, if your domain name is “example.com” and you want to allow both the domain and its subdomains to access your Django application, you would add the following to your settings.py

ALLOWED_HOSTS = [‘example.com’]

Alternatively, if you want to allow access from any host, you can use the wildcard ‘*’

ALLOWED_HOSTS = [‘*’]

However, it’s important to use the wildcard ‘*’ with caution as it can potentially expose your application to security risks by allowing access from any host.

How to solve vs code not opening in  nethunter 

How to install termux on Android 

Step 4: Saving the Changes

After configuring the ALLOWED_HOSTS in settings.py, save the changes you have made in settings.py file.

Step 5: Restarting Your Django Application

To apply the changes, you need to restart your Django application.

If your application is running using the development server (python manage.py runserver).

You can stop the server by pressing Ctrl+C in the terminal/command prompt, and then restart it using the same command.

The application can equally restart it self after you have made some changes to it

Step 6: Testing Your Configuration

After your Django application is up and running, it’s important to test your configuration to ensure that only the specified hosts can access your site.

Try accessing your site from a different domain or IP address to verify that access is denied. That is it .

2 thoughts on “How to set allowed host in Django”

Leave a Comment