How to add ads.txt to Django as requested by Google Adsense

Serve Adsense in Django
Image: Serve Adsense in Django (License: CC-BY-SA Marcelo Canina)

Three methods to add ads.txt

Last modified:
Tag: adsense


As Google Adsense adopted the Authorized Digital Sellers, or ads.txt standard, each Django website needs an ads.txt file at root.

Ads.txt is a simple, flexible, and secure method for publishers and distributors to declare who is authorized to sell their inventory, improving transparency for programmatic buyers.

Here we explore three alternatives to serve this file correctly.

  1. With a redirect or
  2. Return a single line response
  3. Serve directly from nginx

The first method uses the file you download from Google, and has the flexibility of adding more sellers and maintaining a proper ads.txt file. While the second approach is a bit hacky and it just serves a single line of ads.txt, which is also the most common scenario.

This will solve the infamous Adsense warning message: Earnings at risk - One or more of your sites does not have an ads.txt file

Download ads.txt

Go to your Adsense’s homepage, then sites/my-sites and download the ads.txt file.

It will consist of a single line like this:, pub-0000000000000000, DIRECT, f08c47fec0942fa0

where pub-0000000000000000 is your own publisher ID.

1. Redirect method

1.2 Static folder

Put the ads.txt file in your static folder, typically it would be /static.

1.3 Redirect

Your static files will be served using the path specified at STATIC_URL, for example, in your app/ you would have something like:

STATIC_URL = "/static/"

This is the URL to use when referring to static files located in STATIC_ROOT (after running collectstatic). The problem with this is that ads.txt needs to be located at the root of your website and not at

To address this we do a redirect RedirectView.as_view(url=staticfiles_storage.url("ads.txt")) each time there is a request to /ads.txt, in app/

"""equilang URL Configuration
from import staticfiles_storage
from django.urls import include, path
from django.views.generic.base import RedirectView
from django.conf import settings
from django.conf.urls.static import static

urlpatterns = [
] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)

The above import, is used to select the right storage backend, based in settings.STATICFILES_STORAGE variable’s value.

It also uses the Generic Class Based View django.views.generic.base.RedirectView to redirect to the specified URL.


Google supports redirects when looking for the ads.txt file.

Google supports a single HTTP redirect to a destination outside the original root domain (for example, re-directs to See the IAB update.

Multiple redirects are also supported, as long as each redirect location remains within the original root domain. For example: redirecting to

2. Response method

2.1 Ads.txt view

We add a view that returns a single line response, In myapp/

from django.http import HttpResponse
from django.views import View

class AdsView(View):
    """Replace pub-0000000000000000 with your own publisher ID"""
    line  =  ", pub-0000000000000000, DIRECT, f08c47fec0942fa0"
    def get(self, request, *args, **kwargs):
        return HttpResponse(line)

2.2 URL

Then we need to return the above response when there is request to /ads.txt, so in myapp/

from django.urls import path

from myapp.views import AdsView

urlpatterns = [
    path('ads.txt', AdsView.as_view()),

3. Nginx method

3.1 Static folder

Put the ads.txt file in your static folder, typically it would be /static.

3.2 Configure nginx

Using the location directive, which sets configuration depending on a request URI, we define an alias directive to serve the ads.txt file.

In your nginx website virtual host configuration:

location /ads.txt {
    alias /staticfiles/ads.txt;


The above tutorial steps may differ a bit from your specific static settings but the general idea is pretty simple.

Now every request to will be served by

I prefer the first method, using the original file, as you can have the flexibility of maintaining that file outside the app source code, adding or removing sellers as you need.

Happy Adsensing! 🤑


Marcelo Canina
I'm Marcelo Canina, a developer from Uruguay. I build websites and web-based applications from the ground up and share what I learn here.
comments powered by Disqus

Description of a simple method to add the requested Google Adsense's ads.txt file to a website root location.

Clutter-free software concepts.
Translations English Español

Except as otherwise noted, the content of this page is licensed under CC BY-NC-ND 4.0 . Terms and Policy.

Powered by SimpleIT Hugo Theme