gpt4 book ai didi

python - Django 2.0 slug urls.py 意外关键字参数 'slug'

转载 作者:行者123 更新时间:2023-11-30 22:19:45 24 4
gpt4 key购买 nike

该网站非常简单,只有 3 页,主页和 urls.py 中显示的第二个 url 效果很好,但是当我从主题转到单元页面时,我得到:

TypeError at /subjects/units/english

units() got an unexpected keyword argument 'slug'

我不知道为什么会收到此错误.. django 2.0 在 url 和路径中所做的新修改我阅读了文档,但我不明白 <slug>很好,,是吗<slug> or <slug:slug>我不明白,第一个和第二个的含义是什么??? .. 在模板文件夹 {% url 'units' item.slug %} 中单位是路径的名称,但是是 -- item.slug -- 对吗?如果有人向我解释它,我将非常感激。

models.py

class Unit(models.Model):
subject = models.ForeignKey(Subject, on_delete=models.CASCADE)
name = models.CharField(max_length=250)
description = models.TextField(blank=True, null=True)
archived = models.BooleanField(default=False)
slug = models.SlugField(max_length=250, unique=True)
created = models.DateTimeField(auto_now_add=True, null=True, blank=True)
updated = models.DateTimeField(auto_now=True, null=True, blank=True)
seo_name = models.CharField(max_length=60, blank=True, null=True)
seo_description = models.CharField(max_length=165, blank=True,
null=True)

def save(self, *args, **kwargs):
self.slug = slugify(self.name)
super(Unit, self).save(*args, **kwargs)

def __str__(self):
return self.name

views.py

from django.http import HttpResponse
from django.template import loader
from .models import Subject, Unit, Lesson

def units(request):
all_units = Unit.objects.filter(active=True).order_by('-id')[:]
template = loader.get_template('sheets/units.html')
context = {
'all_units': all_units
}
return HttpResponse(template.render(context, request))

urls.py

from django.urls import path
from .views import subjects, units, lessons

urlpatterns = [
path('subjects/', subjects, name='subjects'),
path('units/<slug:slug>', units, name='units'),
path('lessons/<slug:slug>', lessons, name='lessons'),
]

units.html

    <div class="blog-posts">
<div class="row text-center">
{% for item in all_units %}
<div class="col-sm-4 wow fadeInUp animated" data-wow-duration="1000ms" data-wow-delay="400ms"
style="visibility: visible;animation-duration: 1000ms;animation-delay: 400ms;animation-name: fadeInUp; margin-bottom: 15px">
<div class="post-thumb">
<a href="#"><img class="img-responsive" src="/static/img/blog/1.jpg" alt=""></a>
</div>
<div class="entry-header">
<h3><a href="{% url 'units' item.slug %}">{{ item.name }}</a></h3>
<span class="date">{{ item.created }}</span>
</div>
<div class="entry-content">
<p>{{ item.description|truncatechars:120 }}</p>
</div>
</div>
{% endfor %}
</div>
</div>

最佳答案

def units(request,slug):
all_units = Unit.objects.filter(active=True).order_by('-id')[:]
template = loader.get_template('sheets/units.html')
context = {
'all_units': all_units
}
return HttpResponse(template.render(context, request))

您还需要在 View 函数中传递 slug,因为您在 url 中提到了一个名为 slug 的参数

关于python - Django 2.0 slug urls.py 意外关键字参数 'slug',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49023207/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com