How to set allowed host in Django

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

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

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.

Step 1: Accessing Your Django Project Settings

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

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

Within the settings.py file, locate the ALLOWED_HOSTS setting.follow 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

To configure allowed hosts, you will need to specify the domain names or IP addresses that you want to allow 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.

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.Whatbthis wildcard means is that anyone host, domain can access your site anytime . But thank for your host . For example in Python any where if you like put it a wildcard, ‘*’ your app will never open, except you added your domain name precisely.

Incase you are on vs code android or nethunter and your vs code is having problems you can check this guide to help and also help you to install termux on your android phone. 

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 you used before python manage.py runserver

Though in some cases the application can equally restart it self after you have made some changes to it. That means it will reload.

It is important to configure your host if not you may see host not allowed or you may need to add host just like explained in stackoverflow

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 .

FAQS

Adding Allowed Hosts:

Q: How to give an allowed host/add an allowed host?

A: Edit settings.py and add your domain(s) to the ALLOWED_HOSTS list (e.g., ['yourdomain.com']).

Q: How to add an IP in allowed host?

A: You can include IP addresses alongside domain names (e.g., ['127.0.0.1', 'yourdomain.com']).

Q: How to configure a database in Django?

A: Refer to Django’s official documentation for database configuration steps specific to your database engine (e.g., MySQL, PostgreSQL).

Q: Django ALLOWED_HOSTS example?

A: settings.py contains various configurations for your Django project, including ALLOWED_HOSTS and database settings

Q: How to integrate database with Django? / How to set up MySQL database in Django?

A: Follow Django’s database setup guide for your chosen database engine.

Q: How to configure database in Python? / How to configure SQL server with Django?

A: While Django handles database interactions, general database configuration might involve creating databases and users within your chosen database management system.

Q: What values can I use for ALLOWED_HOSTS?

A: You can specify individual domain names (e.g., [‘yourdomain.com’, ‘www.yourdomain.com’]) or use a wildcard (‘*’) for development purposes (caution: not recommended for production!).

Q: What happens if I don’t set ALLOWED_HOSTS?

A: In development mode (with DEBUG=True), it’s not strictly required. However, it’s strongly recommended for security reasons even in development.

However in production mode (with DEBUG=False), Django will raise an error and refuse to start if ALLOWED_HOSTS is not configured.so you must set allowed host in production.

Q.Why is allow host important?

Because of security issues

Leave a Comment