Django 2.0 slug urls.py unexpected keyword argument 'slug'

the site is very simple just 3 pages ,, home and the second url shown in urls.py works great but when i go from the subjects to unit page i got:

TypeError at /subjects/units/english

units() got an unexpected keyword argument 'slug'

i have no idea why i'm getting this error .. the new modifications made in django 2.0 in url and path i read the documentation but i don't understand <slug> part well ,, is it <slug> or <slug:slug> i don't get it and what's the meaning of the first and the second one ??? .. in templates folder {% url 'units' item.slug %} units is the name of the path but is -- item.slug -- right ?? if any one explained it to me i would be very grateful.

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>

Upvotes: 1

Views: 5071

Answers (1)

Exprator
Exprator

Reputation: 27503

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))

you need to pass the slug in the view function also as you have mentioned a parameter in the url with the name slug

Upvotes: 3

Related Questions