Support Our Site

To ensure we can continue delivering content and maintaining a free platform for all users, we kindly request that you disable your adblocker. Your contribution greatly supports our site's growth and development.

How to Add ads.txt to Your Django Project: Boost Ad Revenue with Simple Implementation

2 min read

If you're looking to maximize your ad revenue and monetization efforts in your Django project, adding ads.txt is a crucial step, ads.txt is a text file that helps prevent unauthorized ad inventory from being sold on your website, ensuring that only trusted advertisers can display ads.

By adding this file to your Django project, you can enhance the credibility of your ad inventory and protect your revenue stream.

In this article, we will guide you through the process of adding ads.txt to your Django project, providing you with a step-by-step approach to ensure a seamless implementation. By following our instructions, you'll not only safeguard your ad revenue but also gain the trust of advertisers, leading to potential collaborations with reputable brands.

Let's dive in and make the most of this essential implementation!

Method 1

The conventional approach to this is to use a RedirectView, but I don't like that, so I take a different approach. I store the ads.txt file in the static folder, then read its contents and send it in the HTTP response.

views.py 

import os
from django.conf import settings
from django.http import HttpResponse

def ads_txt_view(request):
    with open(os.path.join(settings.STATIC_ROOT, 'ads.txt')) as file:
        file_content = file.readlines()
    return HttpResponse(file_content, content_type="text/plain")
urls.py 

path("ads.txt", ads_txt_view, data-title="ads_txt" alt="ads_txt"),

Save the files and run the server, you should be able to see yours ads.txt file in http://localhost:8000/ads.txt

Method 2 - Using RedirectView

For this make sure you have static files configured if not read the following article first - Configuring Static Files in Django

urls.py 

from django.views.generic import RedirectView
from django.contrib.staticfiles.storage import staticfiles_storage

urlpatterns = [
// other urls...

path(
        "ads.txt",
        RedirectView.as_view(url=staticfiles_storage.url("ads.txt")),
    ),
]

With this change, every request to https://example.com/ads.txt will be served by https://example.com/static/ads.txt.

Method 3 using web-server

Serving the ads.txt file directly from the webserver is always an option. Here are the steps on how to do it with Nginx and Apache.

Nginx

Locate the server block that corresponds to the domain or website where you want to serve the ads.txt file. Inside the server block, add the following location block to serve the ads.txt file:

location = /ads.txt {
    alias /path/to/your/ads.txt;
}

Save the Nginx configuration and restart nginx to apply the changes.

Now, when you access the URL http://example.com/ads.txt, Nginx will serve the ads.txt file from the specified location. 

Apache

Locate the block or the specific block that corresponds to the domain or website where you want to serve the ads.txt file. Inside the appropriate block, add the following Alias directive to serve the ads.txt file:

Alias "/ads.txt" "/path/to/your/ads.txt"
Save the changes and restart the server. 


DJANGO

Latest Articles

Latest from djangocentral

How to Use Subquery() in Django With Practical Examples

In the realm of web development, Django stands as a powerful and versatile framework for building robust applications. One of the key aspects of developing efficient and optimized web applications is handling database queries effectively. In this article…
Read more →

4 min read

DRF Serializer: Handling OrderedDict and Converting It to a Dictionary or JSON

In Django Rest Framework (DRF) tests, when you access serializer.data, you might encounter an OrderedDict instead of a regular dictionary. This behavior is intentional and reflects the design of DRF's serialization process.Understanding the Problem The u…
Read more →

3 min read

Django Rest Framework CheetSheet: Mastering API Development

Django Rest Framework (DRF) is a powerful toolkit that makes building robust and scalable web APIs with Django a breeze. Whether you're a seasoned Django developer or a newcomer, having a comprehensive cheat sheet at your disposal can be a game-changer. …
Read more →

5 min read

How to Perform NOT Queries in Django ORM

In Django, performing NOT queries allows you to exclude certain records from the query results based on specific conditions. The NOT operator, represented by the tilde (~) when used in conjunction with the Django ORM's Q object, helps you construct compl…
Read more →

3 min read