Django项目请求robots.txt返回404错误的技术求助
问题
访问website.com/robots.txt时始终返回404错误。
相关文件
config > urls.py
from django.conf import settings from django.contrib import admin from django.urls import path, include, re_path from django.views.generic import TemplateView from django.conf.urls.static import static from config import views from django.conf.urls import handler404, handler500, handler403, handler400 handler404 = views.handler404 handler500 = views.handler500 urlpatterns = [ path('admin/', admin.site.urls), path('accounts/', include('allauth.urls')), path('accounts/', include('django.contrib.auth.urls')), path('', include('pages.urls')), path('plants/', include('plants.urls')), path('robots.txt',TemplateView.as_view(template_name='robots.txt', content_type='text/plain')), ]
config > views.py
from django.http import JsonResponse, Http404, HttpResponse from django.shortcuts import render def handler404(request, exception=None): return render(request, '404.html', status=404) def handler500(request): return render(request, '500.html', status=500)
templates > robots.txt
User-Agent: * Disallow: /admin/ Disallow: /accounts/
部分文件夹结构
project | |---config | | | |---urls.py | |---views.py | |---templates | |---pages | |---about.html | |---contact.html | |---404.html |---robots.txt
已尝试的解决方案(均无效)
- 将handler404函数的参数从
exception=None改为仅exception - 将robots.txt的视图和URL规则迁移至pages应用,并将robots.txt文件放到pages模板文件夹中
- 移除URL规则中的
content_type='text/plain'参数 - 查阅多篇教程仍未找到问题根源
内容的提问来源于stack exchange,提问作者OfSorts
相关产品推荐
相关产品推荐

