如何在 Django 中将 slug 添加到 html 模板 url 标签?

how to add slug to html template url tag in Django?

我想要这样的 URL 路径:www.example.com/html/html-introduction.

现在这里有两个鼻涕虫htmlhtml-介绍。当我在 url 输入中手动输入 slug 时,它有效,但是当我传递到我的 html 模板中的按钮时,它不起作用并显示此错误

Reverse for 'programming-languages-category' with keyword arguments '{'slug': ''}' not found. 1 pattern(s) tried: ['getting\-started/(?P<prglangcat_slug>[-a-zA-Z0-9_]+)/$']

这是我的模板,其中有我想将 slug 传递到的按钮

<div>
    {% for p in prglangcat %}
      <a href="{% url 'base:programming-languages-category' slug=p.prglangcat_slug.slug=p.prg_slug.slug %}" >
        <h4>{{p.title}}</h4>
      </a>
    {% endfor %}
</div>

views.py

def index(request):
    prglangcat = ProgrammingLanguagesCategory.objects.all()
    context = {
        'prglangcat': prglangcat
    }
    return render(request, 'base/index.html', context)

def gettingStarted(request):
    prglangcat = ProgrammingLanguagesCategory.objects.all()
    context = {
        'prglangcat': prglangcat
    }
    return render(request, 'base/getting-started.html', context)

def programmingLanguageCategoryDetail(request, prglangcat_slug):
    prglangcat = ProgrammingLanguagesCategory.objects.get(slug=prglangcat_slug)

    context = {

        'prglangcat': prglangcat
    }
    return render(request, 'base/language-category.html', context)

def programmingLanguageTutorial(request, prglangcat_slug, prg_slug ):
    prglangcat = ProgrammingLanguagesCategory.objects.get(slug=prglangcat_slug)
    prglangtut = ProgrammingLanguageTutorial.objects.get(slug=prg_slug, prglangcat=prglangcat)

    context = {

        'prglangtut': prglangtut,
        'prglangcat': prglangcat
    }
    return render(request, 'base/tutorial-page.html', context)

urls.py

app_name = 'base'

urlpatterns = [
        path('', views.index, name="index"),
        path('getting-started/', views.gettingStarted, name="getting-started"),
        path('getting-started/<slug:prglangcat_slug>/', views.programmingLanguageCategoryDetail, name="programming-languages-category"),
        path('getting-started/<slug:prglangcat_slug>/<slug:prg_slug>/', views.programmingLanguageTutorial, name="tutorial-page"),
]

models.py

class ProgrammingLanguagesCategory(models.Model):
    title = models.CharField(max_length=100)
    icon = models.ImageField(upload_to='programming-category', default="default.jpg")
    description = models.TextField(default="Learn ...")
    slug = models.SlugField(unique=True)

    def get_absolute_url(self):
        return reverse('base:programming-languages-category', args=[self.slug])

    def __str__(self):
        return self.title

class ProgrammingLanguageTutorial(models.Model):
    prglangcat = models.ForeignKey(ProgrammingLanguagesCategory, on_delete=models.CASCADE, null=True)
    creator = models.ForeignKey(User, on_delete=models.CASCADE, null=True)
    slug = models.SlugField(unique=True)
    title = models.CharField(max_length=10000)
    description = models.TextField(null=True)
    image = models.ImageField(upload_to='Tutorial Image', null=True, blank=True)
    code_snippet = models.CharField(max_length=1000000000, null=True, blank=True)
    video_url = models.URLField(null=True, blank=True)
    views = models.IntegerField(default=0)
    likes = models.IntegerField(default=0)

    def __str__(self):
        return self.title

    def get_absolute_url(self):
        return reverse('base:tutorial-page', args=[self.slug])

您正在使用的模板中

<a href="{% url 'base:programming-languages-category' slug=p.prglangcat_slug.slug=p.prg_slug.slug %}" >

programming-languages-category url 仅将 slug 作为 URL 参数,因此您可能只想执行以下操作:

<a href="{% url 'base:programming-languages-category' slug=p.prglangcat.slug %}" >

请注意,slug=p.prglangcat_slug 中的 _ 已替换为 .,因为您要查找 prglangcat 关系并从 ProgrammingLanguagesCategory 模型,我们使用 . 在模板中执行此操作。