Categories: Programming

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.

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

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

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 .

benny

I am graduate of Mathematics and Mechatronics Engineering. I am a lover of Python. I love writing about tech.

Recent Posts

What are the common characteristics of philosophy and science?

Within the broad field of human research, philosophical and scientific inquiry has had a particularly…

1 week ago

What is the relationship between philosophy and science

In our world, there are two major approaches to understanding things: philosophy and science. They…

1 week ago

How to Make Your Mac Faster: Clearing Cache in Simple Steps

Is your Mac feeling sluggish? One easy fix might be to clear its cache. Cache…

2 weeks ago

How to Show Hidden Files on a Mac

How to show Hidden files on Your Mac? Have you ever wondered what's hiding on…

2 weeks ago

How to Uninstall Apps on MacBook ?

Uninstall apps on MacBook is another thing a Mac user should be worried of in…

2 weeks ago

How hackers uses Nethunter Kali linux to hack Facebook

Disclaimer; This tutorial is for self awareness for you to know how hackers hack your…

2 weeks ago

This website uses cookies to enhance your browsing experience, personalize content and ads, provide social media features, and analyze traffic. By clicking "Accept", you consent to the use of cookies and similar technologies as outlined in our Cookie Policy and Privacy Policy. We will NEVER sell your data Note: By accepting, you agree to our Cookie Policy, Privacy Policy, and Ads Consent. You can manage your preferences or opt-out in your browser settings.

Read More