用于静态反应路由的Django站点地图(错误:noreversematch at /sitemap.xml/)

发布于 2025-02-11 21:21:40 字数 2563 浏览 0 评论 0原文

我想实现静态反应路由的站点地图,并且会得到此错误:

找不到“联系”的反向。 “联系人”不是有效的视图功能或模式名称。

我添加了我的React路由React> React-Routes ,然后将它们附加到root urls.py file

settings中的urlpatterns:< /strong>

   REACT_ROUTES = [
        'contact',
        'politique-de-confidentialite',
        'mentions-legales',
        'cookies',
        .
        .
    
    ]  
  INSTALLED_APPS = [
        'corsheaders',
        'django.contrib.admin',
        'django.contrib.auth',
        'django.contrib.contenttypes',
        'django.contrib.sessions',
        'django.contrib.messages',
        'django.contrib.staticfiles',
        'django.contrib.sites',
        'django.contrib.sitemaps',
        'rest_framework',
        .
        .
    ]
    
    SITE_ID = 1
    TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [os.path.join(BASE_DIR, 'ess')],
        'APP_DIRS': True,
        'OPTIONS': {
            'context_processors': [
                'django.template.context_processors.debug',
                'django.template.context_processors.request',
                'django.contrib.auth.context_processors.auth',
                'django.contrib.messages.context_processors.messages',
            ],
        },
    },
]
    

    STATIC_URL = '/static/'
    STATIC_ROOT = basepath + '/app/static'
    STATICFILES_DIRS = [os.path.join(BASE_DIR, 'ess', 'static')]

urls:

from django.conf import settings
from django.contrib.sitemaps.views import sitemap
from .sitemaps import  StaticViewSitemap

react_routes = getattr(settings, 'REACT_ROUTES', [])

sitemaps = {
    'static': StaticViewSitemap
}

urlpatterns = [
    path('sitemap.xml/', sitemap, {'sitemaps': sitemaps}, name='django.contrib.sitemaps.views.sitemap'),
    re_path(r'^$', TemplateView.as_view(template_name='index.html')),

]

for route in react_routes:
    urlpatterns += [
        re_path('{}'.format(route), TemplateView.as_view(template_name='index.html'))
    ]

sitemaps.py:

from django.contrib.sitemaps import Sitemap
from django.urls import reverse


class StaticViewSitemap(Sitemap):

    def items(self):
        return ['contact']
    
    def location(self, item):
        return reverse(item)

我不知道我缺少什么,请帮助

i want to implement sitemap for static react routes and I get this error:

Reverse for 'contact' not found. 'contact' is not a valid view function or pattern name.

NoReverseMatch at /sitemap.xml

I added a list of my react routes REACT-ROUTES in settings.py and append them to urlpatterns in the root urls.py file

settings :

   REACT_ROUTES = [
        'contact',
        'politique-de-confidentialite',
        'mentions-legales',
        'cookies',
        .
        .
    
    ]  
  INSTALLED_APPS = [
        'corsheaders',
        'django.contrib.admin',
        'django.contrib.auth',
        'django.contrib.contenttypes',
        'django.contrib.sessions',
        'django.contrib.messages',
        'django.contrib.staticfiles',
        'django.contrib.sites',
        'django.contrib.sitemaps',
        'rest_framework',
        .
        .
    ]
    
    SITE_ID = 1
    TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [os.path.join(BASE_DIR, 'ess')],
        'APP_DIRS': True,
        'OPTIONS': {
            'context_processors': [
                'django.template.context_processors.debug',
                'django.template.context_processors.request',
                'django.contrib.auth.context_processors.auth',
                'django.contrib.messages.context_processors.messages',
            ],
        },
    },
]
    

    STATIC_URL = '/static/'
    STATIC_ROOT = basepath + '/app/static'
    STATICFILES_DIRS = [os.path.join(BASE_DIR, 'ess', 'static')]

urls :

from django.conf import settings
from django.contrib.sitemaps.views import sitemap
from .sitemaps import  StaticViewSitemap

react_routes = getattr(settings, 'REACT_ROUTES', [])

sitemaps = {
    'static': StaticViewSitemap
}

urlpatterns = [
    path('sitemap.xml/', sitemap, {'sitemaps': sitemaps}, name='django.contrib.sitemaps.views.sitemap'),
    re_path(r'^

sitemaps.py :

from django.contrib.sitemaps import Sitemap
from django.urls import reverse


class StaticViewSitemap(Sitemap):

    def items(self):
        return ['contact']
    
    def location(self, item):
        return reverse(item)

I don't know what I am missing, please help

, TemplateView.as_view(template_name='index.html')), ] for route in react_routes: urlpatterns += [ re_path('{}'.format(route), TemplateView.as_view(template_name='index.html')) ]

sitemaps.py :

I don't know what I am missing, please help

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

伴我老 2025-02-18 21:21:40

我通过在我的react build build文件夹中添加stitemap.xml文件解决了问题

,然后在urlpatterns中添加

path('sitemaps.xml/',TemplateView.as_view(template_name='sitemap.xml', content_type='text/xml'),

I solved the problem by adding a sitemap.xml file inside my react build folder

then added in urlpatterns

path('sitemaps.xml/',TemplateView.as_view(template_name='sitemap.xml', content_type='text/xml'),
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文