今日1人次阅读了1/155篇文章  |    留言板  |    RSS订阅
有钱人终成眷属,没钱人亲眼目睹毒鸡汤

为django站点添加谷歌广告的ads.txt文件

2020-02-18     loonlog     2435     1

本文目录

随着Google Adsense采用“ 授权数字卖家”或ads.txt标准,使用Django设计的网站都需要在根目录下添加一个ads.txt文件。当然不是简单的直接放入文件夹就可以的,需要一些配置。

在这里,我们探讨了三种方法来正确提供此文件。

使用重定向

使用url和view常规Response响应方式返回ads.txt中的数据

直接使用nginx

第一种方法使用从Google下载的ads.txt原文件,并具有添加更多卖家和维护适当ads.txt 文件的灵活性。尽管第二种方法有点笨拙,并且只使用一行ads.txt,这也是最常见的情况。

三种方法都能解决Adsense红色警告消息:有收益损失风险 - 您需要纠正 ads.txt 文件存在的一些问题,以免严重影响您的收入。


首先下载ads.txt,来获取文件以及文件里面的信息

转到Adsense的首页,然后转到网站/我的网站并下载 ads.txt文件(如果没有为你的网站添加ads.txt,首页会一直提示你修正此问题,点击修正就能找到下载连接)。

它由如下一行组成:

google.com, pub-6259540306500530, DIRECT, f08c47fec0942fa0

这里面包换了你自己的id等信息,反正不用管,直接用。


1.重定向方法

1.1、静态文件夹

将ads.txt文件放在您的静态文件夹中,通常是/static。

1.2、重定向

静态文件将使用STATIC_URL指定的路径 提供,例如,porject/settings.py将具有以下内容:

STATIC_URL = "/static/"

这是引用位于STATIC_ROOT(运行后collectstatic)中的静态文件时要使用的URL 。问题在于它ads.txt需要位于网站的根目录 https://example.com/ads.txt中,而不是 https://example.com/static/ads.txt中。

为了解决这个问题,每次接收到/ads.txt请求时,使用project/urls.py中的RedirectView.as_view(url=staticfiles_storage.url("ads.txt"))重定向到ads.txt文件,也即是https://example.com/ads.txt重定向到https://example.com/static/ads.txt 。

"""equilang URL Configuration
"""
from django.contrib.staticfiles.storage 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 = [
//...
    path(
        "ads.txt",
        RedirectView.as_view(url=staticfiles_storage.url("ads.txt")),
    ),
] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)

上面的import django.contrib.staticfiles.storage.url,用于根据变量settings.STATICFILES_STORAGE的值选择合适的存储后端。

它还使用基于通用类的视图 django.views.generic.base.RedirectView 重定向到指定的URL。


注意

Google在查找ads.txt文件时支持重定向。


2.Response响应方法

2.1、Ads.txt视图

我们在myapp/views.py添加一个返回单行响应的视图:

from django.http import HttpResponse
from django.views import View
class AdsView(View):
    """Replace pub-0000000000000000 with your own publisher ID"""
    line  =  "google.com, pub-0000000000000000, DIRECT, f08c47fec0942fa0"
    def get(self, request, *args, **kwargs):
        return HttpResponse(line)

2.2、添加url

当有对/ads.txt的请求时 ,我们需要返回上述响应,在:myapp/urls.py添加如下内容


from django.urls import path
from myapp.views import AdsView
urlpatterns = [
    path('ads.txt', AdsView.as_view()),
]


3. Nginx方法

3.1、静态文件夹

将ads.txt文件放在您的静态文件夹中,通常是/static。

3.2配置Nginx

使用 location 指令(根据请求URI设置配置),我们定义了 别名 指令来为ads.txt文件提供服务。

在您的nginx网站虚拟主机配置中:

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

结论

上面的教程步骤可能与您的特定静态设置有所不同,但是总体思路非常简单。

现在,所有的https://example.com/ads.txt请求都将由https://example.com/static/ads.txt提供 。

我更喜欢第一种方法,即使用原始文件,因为您可以灵活地在应用程序源代码之外维护该文件,并根据需要添加或删除卖方。


参考:https://simpleit.rocks/python/django/how-to-add-ads-txt-to-django/


Django , 网站 , 博客 , nginx

为django站点添加谷歌广告的ads.txt文件
http://loonlog.com/2020/2/18/add-google-adasense-ads-txt-to-django/
    觉得有用?请点击页面顶部广告支持我!

您可能感兴趣的文章

发表评论(关于评论)

评论列表,共 1 条评论

  • 回复

    1楼

    我是来测试评论的!