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


def robots_txt(request):
    # 构建robots.txt文件的完整路径
    robots_path = os.path.join(settings.BASE_DIR, 'static', 'robots.txt')

    # 尝试打开并读取文件内容
    try:
        with open(robots_path, 'r') as file:
            content = file.read()
        response = HttpResponse(content, content_type='text/plain')
        return response
    except FileNotFoundError:
        # 如果文件不存在，返回404错误
        return HttpResponseNotFound("Robots.txt not found")
