Django 2:url 中存在多个 slugs

2024-01-04

我目前正在关注 django 教程sentdex https://www.youtube.com/watch?v=NqHX4eF2tw8。本教程正在克隆https://pythonprogramming.net/ https://pythonprogramming.net/。在本教程中,他在 url 中仅使用了一个 slug,但我认为拥有多个 slug 会更方便,因为 url 会更有条理。例如,pythonprogramming.net 上的教程链接如下所示:https://pythonprogramming.net/build-supercomputer-raspberry-pi/ https://pythonprogramming.net/build-supercomputer-raspberry-pi/。我希望它在我的项目中看起来像这样mydomain.com/data-analsyis/build-supercomputer/1/。网址将是domain/category/series/episode/, 代替domain/episode/.

# urls.py
from django.urls import path
from . import views

app_name = "main"

urlpatterns = [
    path('', views.homepage, name="homepage"),  # View categories
    path("register/", views.register, name="register"),
    path("logout/", views.logout_request, name="logout"),
    path("login/", views.login_request, name="login"),
    path("<slug:series_slug>/", views.slugs, name="series_blocks"),  # Views all the series in that category
    path("<slug:series_slug>/<slug:tutorial_slug>/", views.slugs, name="tutorial_blocks"),  # View all tutorials in series
    path("<slug:series_slug>/<slug:tutorial_slug>/<int:id>/", views.slugs, name="tutorial_blocks"),  # View spesific tutorial nr.
]

Note:我认为拥有 3 个不同的功能比views.slug

# Models.py
from django.db import models
from datetime import datetime


class TutorialCategory(models.Model):
    category = models.CharField(max_length=100)
    summary = models.CharField(max_length=200)
    slug = models.CharField(max_length=100)

    class Meta:
        verbose_name_plural = "Kategorier"

    def __str__(self):
        return self.category


class TutorialSeries(models.Model):
    series = models.CharField(max_length=200)
    category = models.ForeignKey(TutorialCategory, default=1, verbose_name="Kategori", on_delete=models.SET_DEFAULT)
    summary = models.CharField(max_length=200)
    slug = models.CharField(max_length=100)  # Not from tutorial, but this is the tutorial slug

    class Meta:
        verbose_name_plural = "Serier"

    def __str__(self):
        return self.series


class Tutorial(models.Model):
    title = models.CharField(max_length=200)
    content = models.TextField()
    published = models.DateTimeField("date published", default=datetime.now())
    series = models.ForeignKey(TutorialSeries, default=1, verbose_name="Serie", on_delete=models.SET_DEFAULT)
    id = models.AutoField(primary_key=True)  # This was originally a slug, but now it's the episode number

    def __str__(self):
        return self.title

.

# Views.py
def slugs(request, series_slug):
    categories = [c.slug for c in TutorialCategory.objects.all()]
    if series_slug in categories:
        match_series = TutorialSeries.objects.filter(category__slug=series_slug)
        series_urls = {}
        for m in match_series.all():
            part_one = Tutorial.objects.filter(series__series=m.series).earliest("published")
            series_urls[m] = part_one.id
        return render(request, "main/category.html", {"part_ones": series_urls})

    tutorials = [t.slug for t in Tutorial.objects.all()]
    if series_slug in tutorials:
        this_tutorial = Tutorial.objects.get(series_slug=series_slug)
        return render(request, "main/tutorial.html", {"tutorial": this_tutorial})

我最初遇到的问题是tutorials出现的不仅是那些来自series。仅显示series工作得很好。 但现在,在尝试解决这个问题之后,我真的不知道该怎么办。我已经改变了views.py很多,但现在它是原始的(来自 sendtex),并将教程 slug 更改为 Id 并为教程系列提供一个 slug。

我怎样才能得到views.py只显示tutorials具体来说series,等等。并且 url 中有多个 slugs?


首先,您应该为每个 url 模式创建一个专用视图:

# urls.py
urlpatterns = [
    ...
    path("<slug:category_slug>/", views.category_blocks, name="category_blocks"),  # Views all the series in that category
    path("<slug:category_slug>/<slug:series_slug>/", views.series_blocks, name="series_blocks"),  # View all tutorials in series
    path("<slug:category_slug>/<slug:series_slug>/<int:tutorial_id>/", views.episode_blocks, name="episode_blocks"),  # View spesific tutorial nr.
]

# views.py
def category_blocks(request, category_slug):
    # logic to display all tutorial with category is `category_slug`

def series_blocks(requests, category_slug, series_slug):
    # logic to display all tutorial with category is `category_slug` and series is `series_slug` 

def tutorial_blocks(requests, category_slug, series_slug, tutorial_id):
    # logic to display all tutorial with category is `category_slug`, series is `series_slug` and tutorial is `tutorial_id`

该逻辑与您已经编写的代码类似。不要忘记检查一下tutorial.series.slug match series_slug and series.category match category_slug

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Django 2:url 中存在多个 slugs 的相关文章

随机推荐