Home > Net >  Reverse for 'category-detail' with arguments '('',)' not found
Reverse for 'category-detail' with arguments '('',)' not found

Time:07-28

I get this error when I try accessing my home page. I've tried reading similar solutions but I can't directly relate them to my case. I'm clearly missing something here but I can't see it.

this is the traceback error message:

Traceback (most recent call last):
  File "C:\Users\Muhumuza-Ivan\AppData\Local\Programs\Python\Python310\lib\site-packages\django\core\handlers\exception.py", line 55, in inner    
    response = get_response(request)
  File "C:\Users\Muhumuza-Ivan\AppData\Local\Programs\Python\Python310\lib\site-packages\django\core\handlers\base.py", line 197, in _get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
  File "C:\Users\Muhumuza-Ivan\Desktop\JobPortal\jobapp\views.py", line 68, in index
    return render(request, 'index.html', context)
  File "C:\Users\Muhumuza-Ivan\AppData\Local\Programs\Python\Python310\lib\site-packages\django\shortcuts.py", line 24, in render
    content = loader.render_to_string(template_name, context, request, using=using)
  File "C:\Users\Muhumuza-Ivan\AppData\Local\Programs\Python\Python310\lib\site-packages\django\template\loader.py", line 62, in render_to_string 
    return template.render(context, request)
  File "C:\Users\Muhumuza-Ivan\AppData\Local\Programs\Python\Python310\lib\site-packages\django\template\backends\django.py", line 62, in render  
    return self.template.render(context)
  File "C:\Users\Muhumuza-Ivan\AppData\Local\Programs\Python\Python310\lib\site-packages\django\template\base.py", line 175, in render
    return self._render(context)
  File "C:\Users\Muhumuza-Ivan\AppData\Local\Programs\Python\Python310\lib\site-packages\django\template\base.py", line 167, in _render
    return self.nodelist.render(context)
  File "C:\Users\Muhumuza-Ivan\AppData\Local\Programs\Python\Python310\lib\site-packages\django\template\base.py", line 1000, in render
    return SafeString("".join([node.render_annotated(context) for node in self]))
  File "C:\Users\Muhumuza-Ivan\AppData\Local\Programs\Python\Python310\lib\site-packages\django\template\base.py", line 1000, in <listcomp>
    return SafeString("".join([node.render_annotated(context) for node in self]))
  File "C:\Users\Muhumuza-Ivan\AppData\Local\Programs\Python\Python310\lib\site-packages\django\template\base.py", line 958, in render_annotated       
    return self.render(context)
  File "C:\Users\Muhumuza-Ivan\AppData\Local\Programs\Python\Python310\lib\site-packages\django\template\defaulttags.py", line 472, in render
    url = reverse(view_name, args=args, kwargs=kwargs, current_app=current_app)
  File "C:\Users\Muhumuza-Ivan\AppData\Local\Programs\Python\Python310\lib\site-packages\django\urls\base.py", line 88, in reverse
    return resolver._reverse_with_prefix(view, prefix, *args, **kwargs)
  File "C:\Users\Muhumuza-Ivan\AppData\Local\Programs\Python\Python310\lib\site-packages\django\urls\resolvers.py", line 802, in _reverse_with_prefix  
    raise NoReverseMatch(msg)
django.urls.exceptions.NoReverseMatch: Reverse for 'category-detail' with arguments '('',)' not found. 1 pattern(s) tried: ['jobapp/category/(?P<slug>[-a-zA-Z0-9_] )/\\Z']

Index.html The error in the browser directly points to the HumanResource option

<option><a href="{% url 'category-detail' Nursing.slug %}"></a><span >{{Nursing.title}}</span></option>
<option><a href="{% url 'category-detail' Education.slug %}"></a><span >{{Education.title}}</span></option>
<option><a href="{% url 'category-detail' HumanResource.slug %}"></a><span >{{HumanResource.title}}</span></option>
<option><a href="{% url 'category-detail' NGO.slug %}"></a><span >{{NGO.title}}</span></option>
<option><a href="{% url 'category-detail' Computing.slug %}"></a><span >{{Computing.title}}</span></option>
<option><a href="{% url 'category-detail' Medical.slug %}"></a><span >{{Medical.title}}</span></option>

views.py

def index(request):
    form = SearchForm()

    elitejobs = Jobs.objects.filter(company__title='EliteJobsToday')[:5]
    alljobs = Jobs.objects.filter(company__title='alljobspo')[:5]

    context = {}
    context['elitejobs'] = elitejobs
    context['alljobs'] = alljobs
  
    context['Manager'] = Category.objects.get(title='Manager')
    context['Engineer'] = Category.objects.get(title='Engineer')
    context['Architect'] = Category.objects.get(title='Architect')
    context['SoftwareDeveloper'] = Category.objects.get(title='SoftwareDeveloper')
    context['Insurance'] = Category.objects.get(title='Insurance')
    context['Uncategorised'] = Category.objects.get(title='Uncategorised')
   
    context['form'] = form

    if request.method == 'POST':
        form = SearchForm(request.POST)
        if form.is_valid():
            search = form.cleaned_data.get('title')

            jobs = []
            if len(search.split())>1:
                search_list = search.split()
                item_list = []
                for item in search_list:
                    a_list = Jobs.objects.filter(title__icontains=item)
                    for x in a_list:
                        item_list.append(x)
                [jobs.append(x) for x in item_list if x not in jobs]        
                
                return render(request, 'job-list.html', {'jobapp': jobs})
            else:
                jobs = Jobs.objects.filter(title__icontains=search)

                return render(request, 'job-list.html', {'jobapp': jobs})
        else:
            messages.error(request, 'Error Processing Your Request')
            context['form'] = form
            return render(request, 'index.html', context)
    
    return render(request, 'index.html', context)

def category_detail(request, slug):
    the_category = Category.objects.get(slug=slug)
    context = {}

    jobs = Jobs.objects.filter(category__slug=slug)[:20]
    context['jobs'] = jobs
    context['the_category'] = the_category

    return render(request, 'category-detail.html', context)

urls.py

  path('jobapp/category/<slug:slug>/', job_views.category_detail, name='category-detail'),

CodePudding user response:

make sure the value passed to category-detail is not null so you can do something like the following in every line in template

 {% if Nursing.slug %}
    <option><a href="{% url 'category-detail' Nursing.slug %}"></a>
<span >{{Nursing.title}}</span></option>
   {% endif %}
 {% if Education.slug %}
    <option><a href="{% url 'category-detail' Education.slug %}"></a><span >{{Education.title}}</span></option>
{% endif %}

   {%if HumanResource.slug %} 
    <option><a href="{% url 'category-detail' HumanResource.slug %}"></a><span >{{HumanResource.title}}</span></option>
    <option><a href="{% url 'category-detail' NGO.slug %}"></a><span >{{NGO.title}}</span></option>
{% endif %}
 {% if Computing.slug %}
    <option><a href="{% url 'category-detail' Computing.slug %}"></a><span >{{Computing.title}}</span></option>
{% endif %}
{% if  Medical.slug %}
    <option><a href="{% url 'category-detail' Medical.slug %}"></a><span >{{Medical.title}}</span></option>
{% endif %}

but better solution for this is to prevent null value for slug field

  • Related