Ajout d'un sitemap avec gestion des cours et des pages statiques, intégration d'une vue pour robots.txt, et mise à jour des modèles avec les URLs absolues pour améliorer le SEO.
This commit is contained in:
parent
e77ea7e20e
commit
cab7c07433
5 changed files with 62 additions and 1 deletions
|
|
@ -15,6 +15,9 @@ class Course(models.Model):
|
|||
def __str__(self):
|
||||
return self.name
|
||||
|
||||
def get_absolute_url(self):
|
||||
return f"/courses/{self.slug}-{self.id}/"
|
||||
|
||||
class Module(models.Model):
|
||||
name = models.CharField(max_length=200)
|
||||
slug = models.SlugField()
|
||||
|
|
@ -25,6 +28,9 @@ class Module(models.Model):
|
|||
enable = models.BooleanField(default=True)
|
||||
order = models.PositiveIntegerField()
|
||||
|
||||
def __str__(self):
|
||||
return self.name
|
||||
|
||||
class Lesson(models.Model):
|
||||
name = models.CharField(max_length=200)
|
||||
slug = models.SlugField()
|
||||
|
|
@ -43,6 +49,9 @@ class Lesson(models.Model):
|
|||
def __str__(self):
|
||||
return self.name
|
||||
|
||||
def get_absolute_url(self):
|
||||
return f"/courses/{self.module.course.slug}/{self.module.slug}/{self.slug}/"
|
||||
|
||||
|
||||
class Comment(models.Model):
|
||||
lesson = models.ForeignKey(Lesson, on_delete=models.CASCADE, related_name='comments')
|
||||
|
|
|
|||
|
|
@ -9,7 +9,7 @@ def list_courses(request):
|
|||
courses = Course.objects.all()
|
||||
return render(request, 'courses/list.html', {'courses': courses})
|
||||
|
||||
def show(request, course_name, course_id):
|
||||
def show(request, course_id):
|
||||
# Optimized course fetch with related author and profile (if present)
|
||||
course = get_object_or_404(
|
||||
Course.objects.select_related('author', 'author__profile'),
|
||||
|
|
|
|||
|
|
@ -46,6 +46,7 @@ INSTALLED_APPS = [
|
|||
'django.contrib.sessions',
|
||||
'django.contrib.messages',
|
||||
'django.contrib.staticfiles',
|
||||
'django.contrib.sitemaps',
|
||||
|
||||
'core',
|
||||
'courses',
|
||||
|
|
|
|||
29
devart/sitemap.py
Normal file
29
devart/sitemap.py
Normal file
|
|
@ -0,0 +1,29 @@
|
|||
from django.contrib import sitemaps
|
||||
from django.urls import reverse
|
||||
|
||||
# --- IMPORTS DEPUIS TES DIFFÉRENTES FEATURES ---
|
||||
from courses.models import Course
|
||||
from users.models import Profile
|
||||
|
||||
# --- SITEMAP : LES Cours ---
|
||||
class CourseSitemap(sitemaps.Sitemap):
|
||||
changefreq = "weekly"
|
||||
priority = 0.9
|
||||
|
||||
def items(self):
|
||||
return Course.objects.filter(enable=True) # Exemple de filtre
|
||||
|
||||
def location(self, item):
|
||||
# Assure-toi que ton modèle Course a bien une méthode get_absolute_url
|
||||
return item.get_absolute_url()
|
||||
|
||||
# --- SITEMAP : PAGES STATIQUES ---
|
||||
class StaticViewSitemap(sitemaps.Sitemap):
|
||||
priority = 0.5
|
||||
changefreq = "monthly"
|
||||
|
||||
def items(self):
|
||||
return ["home"] # Les noms de tes URLs
|
||||
|
||||
def location(self, item):
|
||||
return "https://partirdezero.com"
|
||||
|
|
@ -18,12 +18,34 @@ from django.conf import settings
|
|||
from django.conf.urls.static import static
|
||||
from django.contrib import admin
|
||||
from django.urls import path, include
|
||||
from django.http import HttpResponse
|
||||
from devart.sitemap import CourseSitemap, StaticViewSitemap
|
||||
from django.contrib.sitemaps.views import sitemap
|
||||
|
||||
# La vue pour le robots.txt
|
||||
def robots_txt(request):
|
||||
lines = [
|
||||
"User-agent: *",
|
||||
"Disallow: /admin/",
|
||||
"Disallow: /users/",
|
||||
"Allow: /",
|
||||
"Sitemap: https://partirdezero.com/sitemap.xml", # On indique déjà où sera le plan
|
||||
]
|
||||
return HttpResponse("\n".join(lines), content_type="text/plain")
|
||||
|
||||
sitemaps_dict = {
|
||||
'cours': CourseSitemap,
|
||||
'static': StaticViewSitemap,
|
||||
}
|
||||
|
||||
urlpatterns = [
|
||||
path('admin/', admin.site.urls),
|
||||
path('', include('home.urls')),
|
||||
path('courses/', include('courses.urls')),
|
||||
path('users/', include('users.urls')),
|
||||
|
||||
path('sitemap.xml', sitemap, {'sitemaps': sitemaps_dict}, name='django.contrib.sitemaps.views.sitemap'),
|
||||
path('robots.txt', robots_txt),
|
||||
]
|
||||
|
||||
if settings.DEBUG:
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue