Why is my function not returning the object by its slug in the url?

'edit_holiday() got an unexpected keyword argument 'slug'

I would like that when clicking on the object, its slug is returned in the url, which I set automatically in the models, to be identical to the name.

Function in models.py:

@receiver (post_save, sender = Holiday)
def insert_slug(sender, instance, **kwargs):
    if not instance.slug:
        instance.slug = slugify(instance.HolidayReason)
        return instance.save()

My views.py to visualize object:

def edit_holiday(request, pk):
    if request.method == 'GET':
        objeto = Holiday.objects.filter(slug=pk).first()
        
        if objeto is None:
            return redirect(reverse('lista_holiday'))
        
        holiday_list = Holiday.objects.get(slug=pk)
        form = HolidayForm(instance=objeto)
        
        form_holidaycenter_factory = inlineformset_factory(Holiday, HolidayCenter, form=HolidayCenterForm)
        form_holidayloc_factory = inlineformset_factory(Holiday, HolidayLoc, form=HolidayLocForm)
        form_holidaycenter = form_holidaycenter_factory(instance=objeto, prefix='holidaycenter')
        form_holidayloc = form_holidayloc_factory(instance=objeto, prefix='holidayloc')
        
        context = {
            'form': form,
            'form_holidaycenter ': form_holidaycenter,
            'form_holidayloc ': form_holidayloc ,
            'holiday_list ': feriado_list,
        }
        
        return render(request, '../templates/feriado/form_.html', context)
    
    elif request.method == 'POST':
        objeto = Holiday.objects.filter(slug=pk).first()
        
        if objeto is None:
            return redirect(reverse('lista_holiday'))
        
        holiday_list = Holiday.objects.get(slug=pk)
        form = HolidayForm(request.POST, instance=objeto)
        
        form_holidaycenter_factory = inlineformset_factory(Holiday, HolidayCenter, form=HolidayCenterForm)
        form_holidayloc_factory = inlineformset_factory(Holiday, HolidayLoc, form=HolidayLocForm)
        form_holidaycenter = form_holidaycenter_factory(request.POST, instance=objeto, prefix='holidaycenter')
        form_holidayloc = form_holidayloc_factory(instance=objeto, prefix='holidayloc')
        
        
        if form.is_valid() and form_holidayloc .is_valid() and form_holidaycenter.is_valid():
            holiday= form.save()
            form_holidayloc.instance = holiday
            form_holidaycenter.instance = holiday
            form_holidayloc.save()
            form_holidaycenter.save()
            
            messages.success(request, "Feriado adicionado com sucesso!")
            return redirect(reverse('lista_holiday'))
        
        else: 
            context = {
                'form': form,
                'form_holidaycenter ': form_holidaycenter,
                'form_holidayloc ': form_holidayloc ,
                'holiday_list ': feriado_list,
            }
            
            return render(request, '../templates/feriado/form_holiday.html', context)

My urls.py:

from django.urls import path
from . import views
from django.contrib.auth.decorators import login_required
from .views import ListaFeriadoView, adicionar_feriado, editar_feriado, ExcluirFeriadoView
from . import views

urlpatterns = [
   path('lista_holiday/', login_required(ListaFeriadoView.as_view()), name='lista_holiday'),
   path('cadastrar_holiday/', login_required(views.adicionar_holiday), name='cadastrar_holiday'),
   path('edit_holiday/<slug:slug>/', login_required(views.edit_holiday), name='edit_holiday'),
   path('excluir_holiday/<slug:slug>', login_required(ExcluirFeriadoView.as_view()), name='excluir_feriado'),
]

but this error happens: TypeError at /feriado/edit_holiday/descobrimento-brasil/ edit_holiday() got an unexpected keyword argument 'slug'

I think you'd need to edit views.py as following:

def edit_holiday(request, pk):
    if request.method == 'GET':
        objeto = Holiday.objects.filter(slug, pk).first()
        
        if objeto is None:
            return redirect(reverse('lista_holiday'))
        
        holiday_list = Holiday.objects.get(slug =  pk) # invalid argument
        form = HolidayForm(instance=objeto)
        
        form_holidaycenter_factory = inlineformset_factory(Holiday, HolidayCenter, form=HolidayCenterForm)
        form_holidayloc_factory = inlineformset_factory(Holiday, HolidayLoc, form=HolidayLocForm)
        form_holidaycenter = form_holidaycenter_factory(instance=objeto, prefix='holidaycenter')
        form_holidayloc = form_holidayloc_factory(instance=objeto, prefix='holidayloc')
        
        context = {
            'form': form,
            'form_holidaycenter ': form_holidaycenter,
            'form_holidayloc ': form_holidayloc ,
            'holiday_list ': feriado_list,
        }
        
        return render(request, '../templates/feriado/form_.html', context)
    
    elif request.method == 'POST':
        objeto = Holiday.objects.filter(slug, pk).first()
        
        if objeto is None:
            return redirect(reverse('lista_holiday'))
        
        holiday_list = Holiday.objects.get(slug= pk) # invalid argument
        form = HolidayForm(request.POST, instance=objeto)
        
        form_holidaycenter_factory = inlineformset_factory(Holiday, HolidayCenter, form=HolidayCenterForm)
        form_holidayloc_factory = inlineformset_factory(Holiday, HolidayLoc, form=HolidayLocForm)
        form_holidaycenter = form_holidaycenter_factory(request.POST, instance=objeto, prefix='holidaycenter')
        form_holidayloc = form_holidayloc_factory(instance=objeto, prefix='holidayloc')
        
        
        if form.is_valid() and form_holidayloc .is_valid() and form_holidaycenter.is_valid():
            holiday= form.save()
            form_holidayloc.instance = holiday
            form_holidaycenter.instance = holiday
            form_holidayloc.save()
            form_holidaycenter.save()
            
            messages.success(request, "Feriado adicionado com sucesso!")
            return redirect(reverse('lista_holiday'))
        
        else: 
            context = {
                'form': form,
                'form_holidaycenter ': form_holidaycenter,
                'form_holidayloc ': form_holidayloc ,
                'holiday_list ': feriado_list,
            }
            
            return render(request, '../templates/feriado/form_holiday.html', context)

And, you'll need to change the argument 2 times at object.get(), because slugisn't a valid argument for the function object.get().

Back to Top