I created a sitemap as follows, but nothing is displayed inside the sitemap URL.
How can I fix the problem?
Thank you
setting.py
INSTALLED_APPS = [
'django.contrib.sitemaps',
]
sitemaps.py
from django.contrib.sitemaps import Sitemap
from django.shortcuts import reverse
from riposts.models import Posts
class RiSitemap(Sitemap):
priority = 0.5
changefreq = 'daily'
def get_queryset(self):
posts = self.kwargs.get('posts')
return Posts.objects.filter(status="p")
def lastmod(self, obj):
return obj.updated
def location(self, item):
return reverse(item)
urls.py
from django.contrib.sitemaps.views import sitemap
from .views import home_page
from riposts.sitemaps import RiSitemap
sitemaps = {
'posts':RiSitemap,
}
urlpatterns = [
path('', home_page, name="home"),
path("sitemap.xml", sitemap, {"sitemaps": sitemaps}, name="sitemap"),
]
sitemap image
Related
I'm trying to implement a Sitemap for my app and I get the error
"Reverse for 'index' not found. 'index' is not a valid view function or pattern name."
even though these views are set up.
What could cause this? I have no problem with dynamic sitemaps, just the static one.
My views.py
def front_page(request):
return render(request, 'django_modules_testing_app/front_page.html', {})
def about_us(request):
return render(request, 'ihgr_app/about_us.html', {})
def index(request):
return render(request, 'django_modules_testing_app/index.html', {})
urls.py
from django.urls import path
from . import views
from . import blocks
from django.contrib.sitemaps.views import sitemap
from .sitemaps import DjangoModulesTestingApp
app_name = 'django_modules_testing_app'
sitemaps = {
'main_app':DjangoModulesTestingApp
}
urlpatterns = [
path('', views.front_page, name='front_page'),
path('', views.index, name='index'),
path('', views.about_us, name='about_us'),
path('category_details/<slug:slug>/', blocks.category_details, name='category_details'),
path('search_website/', views.SearchWebsite.as_view(), name='search_website'),
path('sitemap.xml', sitemap, {'sitemaps': sitemaps},name='django.contrib.sitemaps.views.sitemap')
]
sitemaps.py
from django.contrib.sitemaps import Sitemap
from django.urls import reverse
class StaticSitemap(Sitemap):
changefreq = 'weekly'
priority = 0.8
protocol = 'http'
def items(self):
return ['front_page','index','about_us']
def location(self, item):
return reverse(item)
You've to specify your app name inside reverse(...) like this
class StaticSitemap(Sitemap):
changefreq = 'weekly'
priority = 0.8
protocol = 'http'
def items(self):
return ['front_page','index','about_us']
def location(self, item):
return reverse(f"django_modules_testing_app:{item}")
my project was running ok I used my admin page at it was all all right today I tried to open it and it gives a Page not found (404)
No Product matches the given query.
Request Method: GET
Request URL: http://127.0.0.1:8000/admin
Raised by: store.views.product_detail
No Product matches the given query.
Request Method: GET
Request URL: http://127.0.0.1:8000/admin
Raised by: store.views.product_detail
I havent touched the store app or project files at all at it was waking just fine yesterday now i cannot access admin page
project urls
from django.conf import settings
from django.conf.urls.static import static
from django.contrib import admin
from django.urls import include, path
urlpatterns = [
path('admin/', admin.site.urls),
path('', include('store.urls', namespace='store')),
path('basket/', include('basket.urls', namespace='basket')),
path('account/', include('account.urls', namespace = 'account')),
]
if settings.DEBUG:
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
store urls
from django.urls import path
from . import views
app_name = 'store'
urlpatterns = [
path('', views.product_all, name='product_all'),
path('<slug:slug>', views.product_detail, name='product_detail'),
path('shop/<slug:category_slug>/', views.category_list, name='category_list'),
]
store views
from urllib import request
from django.shortcuts import get_object_or_404, render
from store.context_processors import categories
from .models import Category, Product
def product_all(request):
products = Product.products.all()
return render(request, 'store/home.html', {'products': products})
def category_list(request, category_slug=None):
category = get_object_or_404(Category, slug=category_slug)
products = Product.objects.filter(category=category)
return render(request, 'store/products/category.html', {'category': category, 'products': products})
def product_detail(request, slug):
product = get_object_or_404(Product, slug=slug, in_stock=True)
return render(request, 'store/products/single.html', {'product': product}) ```
context_processors.py wicht i have includet in my project settings
from .models import Category
def categories(request):
return {
'categories': Category.objects.all()
}
This is the code that causes the error
path('<slug:slug>', views.product_detail, name='product_detail'),
Change it to
path('detail/<slug:slug>/', views.product_detail, name='product_detail'),
The product_detail just overwrites the admin URL
I have two apps in my Django project:basket and store. In root url files I configured the urls.py like this:
from django.contrib import admin
from django.urls import path, include
from django.conf import settings
from django.conf.urls.static import static
urlpatterns = [
path('admin/', admin.site.urls),
path('', include('store.urls', namespace='store')),
path('basket/', include('basket.urls', namespace='basket')),
store/urls.py:
from django.urls import path
from . import views
app_name = 'store'
urlpatterns = [
path('', views.all_products, name='all_products'),
path('<slug:slug>/', views.product_detail, name='product_detail'),
path('category/<slug:category_slug>/',
views.category_list, name='category_list')
]
basket/urls.py:
from django.urls import path
from . import views
app_name = 'basket'
urlpatterns = [
path('', views.basket_summary, name='basket_summary'),
path('add/', views.basket_add, name='basket_add'),
]
I am getting an error:
Page not found(404)
Request Method:GET
Request URL:http://127.0.0.1:8000/basket/
Raised by:store.views.product_detail
this is my store/views.py:
from django.shortcuts import get_object_or_404, render
from .models import *
def product_detail(request, slug):
product = get_object_or_404(Product, slug=slug, in_stock=True)
context = {
'product': product,
}
return render(request, 'store/products/detail.html', context)
please help me solve this problem i've been stuck on this for a long time.
Try this way if you want to avoid 404 error:
from django.shortcuts import get_object_or_404, render
from .models import *
def product_detail(request, slug):
try:
product = Product.objects.get(slug=slug, in_stock=True)
context = {
'product': product,
}
except Product.DoesNotExist:
raise ValidationError('Product Does not exist ')
return render(request, 'store/products/detail.html', context
)
I want to add (/) in site map of Django. I have used following code to generate sitemap in django
my url.py is
from django.contrib.sitemaps.views import sitemap
from myApp.sitemaps import staticSitemap , mySitemap
sitemaps = {
'staticSitemap':staticSitemap,
'mySitemap':mySitemap
}
urlpatterns = [
path('admin/', admin.site.urls),
path('sitemap.xml', sitemap, {'sitemaps': sitemaps} ),
path('<slug:slug>/',apps.switcher, name='calc_detail'),
]
my sitemap file is like below
from django.contrib.sitemaps import Sitemap
from django.shortcuts import reverse
from .models import SingleCalculator
class staticSitemap(Sitemap):
changefreq = "weekly"
priority = 0.9
def items(self):
return ['home','about','contact','privacy']
def location(self, item):
return reverse(item)
class mySitemap(Sitemap):
changefreq = "weekly"
priority = 0.7
def items(self):
return SingleCalculator.objects.all()
def lastmod(self,obj):
return obj.lastmod
Sitemap is generating now like following URL in loc
<loc>https://sitename.com/absolute-difference-calculator</loc>
I want (/) after the end of url. How can I do this?
In model class SingleCalculator add get_absolute_url function:
def get_absolute_url(self):
return f'/{self.slug}/'
In the following image on the left side is the google server and on the right side is the django server:
The google server does not show any logs about it, at first sight, the xml is rendered by the server but it doesn't show the links of the sitemap,
sitemap.py
from django.contrib.sitemaps import Sitemap
from django.core.urlresolvers import reverse
from datetime import datetime
class BasicSitemap(Sitemap):
def __init__(self, names):
self.names = names
def items(self):
return self.names
def changefreq(self, obj):
return 'weekly'
def lastmod(self, obj):
return datetime.now()
def location(self, obj):
return reverse(obj)
urls.py
from django.conf.urls import url, include
from django.contrib import admin
from django.views.generic import TemplateView
from django.shortcuts import render_to_response
from django.template import RequestContext
from sitemap import BasicSitemap
from django.contrib.sitemaps.views import sitemap
sitemaps = {
'pages': BasicSitemap(['about', 'home', 'team', 'contacto', 'courses', 'services'])
}
urlpatterns = [
url(r'^', include('apps.home.urls')),
url(r'^', include('apps.about.urls')),
url(r'^', include('apps.contact.urls')),
url(r'^', include('apps.services.urls')),
url(r'^', include('apps.team.urls')),
url(r'^', include('apps.client.urls')),
url(r'^', include('apps.course.urls')),
url(r'^admin/', admin.site.urls),
url('^robots.txt', TemplateView.as_view(template_name='robots.txt', content_type='text/plain')),
url(r'^sitemap\.xml$', sitemap, {'sitemaps': sitemaps}, name='django.contrib.sitemaps.views.sitemap')
]
def handler404(request):
response = render_to_response('404.html', {},
context_instance=RequestContext(request))
response.status_code = 404
return response
def handler500(request):
response = render_to_response('500.html', {},
context_instance=RequestContext(request))
response.status_code = 500
return response
Things I've done:
run the aplication in different os (linux windows)
tried different django versions < 1.9.7
The weird thing about this is that I had a prev version of this site wich was deployed at January this year and it doesn't have that problem, I used logic, but this new app engine seems to work differently.
Any ideas?? a link to the sitemap page