0xE111 / django-mptt-urls

Creating hierarchical URLs in Django associated with django-MPTT models
MIT License
36 stars 5 forks source link

Reverse for 'categories' with arguments '( path, instance)' and keyword arguments '{}' not found. 1 pattern(s) tried: ['category/(?P<path>.*)'] #9

Closed Adrafe-Rinzzler closed 6 years ago

Adrafe-Rinzzler commented 8 years ago

a href="{% url 'categories' path instance %}?query={{query.value}}">{{ query.value }}

why above error is coming when i am passing path and instance from the url ?

i have included path and instance in the context from the view

path = instance.get_absolute_url() instance = instance

0xE111 commented 8 years ago

The error message is quite verbose: your url rule 'category/(?P<path>.*)' receives only 1 keyword argument (path), so the tag should look like this

{% url 'categories' path=path %}

By the way, you are doing something strange. Please read django-mptt-urls documentation again, especially

class Category(MPTTModel):
    ...
    def get_absolute_url(self):
        return reverse('gallery', kwargs={'path': self.get_path()})

The better approach for your code is this

<a href="{{ instance.get_absolute_url }}?query=...">
Adrafe-Rinzzler commented 8 years ago

thanks alex....i tried it ...but throws this error..'NoneType' object has no attribute 'get_descendants'

somehow the instance becomes none....

here is the view

def category(request, path, instance, extra): if instance: instance.save()

queryset = Product.objects.filter(category__in=instance.get_descendants(include_self=True))

color = request.GET.get('query')

if query:
    queryset = queryset.filter(optiongroup__color=query)

return render(
    request,
    'products/prodgrid.html',
    {
        'instance': instance,
        'children': instance.get_children() if instance else Category.objects.root_nodes(),
        'extra': extra,
        'queryset': queryset
    }
)
0xE111 commented 8 years ago

Maybe this will help

    queryset = Product.objects.filter(category__in=instance.get_descendants(include_self=True)) if instance else Product.objects.all()
Adrafe-Rinzzler commented 8 years ago

It won't as it shows all products if instance is none...why the instance is becoming none when sending the query?

0xE111 commented 8 years ago

If resolution fails, instance will be None.

So, probably the ?P<path>.* part is empty (you open a 'root' page) or something is going wrong. If you're using v2.0.2 check whether your url ends up with slash (ie /products/slug1/slug2/)

Adrafe-Rinzzler commented 8 years ago

No its not ending with that.....though I have given as u said in the link ...when I click on it ...reloads the page with no instance

Adrafe-Rinzzler commented 8 years ago

The resolution fails when I append that query string....wonder why?

0xE111 commented 8 years ago

Insert this in your views.py and tell me the output:

def category(request, path, instance, extra):
    print('Path: [{}]'.format(path))
    slug = path.split('/')[-2]
    print('Slug: [{}]'.format(slug))
    if slug:
        for candidate in Product.objects.filter(slug=slug):
            print('Candidate slug: [{}]'.format(candidate.slug))
   # ... your code following ...
0xE111 commented 8 years ago

Ah, I think I got it... Still write the output

Adrafe-Rinzzler commented 8 years ago

list index out of range

Traceback: File "C:\Python27\lib\site-packages\django\core\handlers\base.py" in get_response

  1. response = wrapped_callback(request, _callback_args, *_callback_kwargs) File "C:\Users\lenovo\Desktop\plump\Plumpin\src\mptt_urlsinit.py" in call
  2. return self.view(_args, *_kwargs) File "C:\Users\lenovo\Desktop\plump\Plumpin\src\products\views.py" in category
  3. slug = path.split('/')[-2]

Exception Type: IndexError at /category/women Exception Value: list index out of range

Adrafe-Rinzzler commented 8 years ago

the resolution fails when category/women/?color=blue

0xE111 commented 8 years ago

I've made some changes, try to update 116590e22a271e5609b4a1c3607af03dccade8ab

Adrafe-Rinzzler commented 8 years ago

OK Alex....just keep this issue open for sometime...if incase

Adrafe-Rinzzler commented 8 years ago

updated...what changes did u make?

Adrafe-Rinzzler commented 8 years ago

what happens when i m sending request to category function from another template ...say header.html which has search box to query on category objects....then that path resolution fails as there is no instance for it.....i tried with request.get_full_path to get the path of that particular page....but not getting passed.
form method = "GET" action = "{% url 'categories' path=request.get_full_path %} "> input type= "text" name='q' style="height:45px; width:400px; padding:8px" value = '{{ request.Get.q }}'placeholder="Search"/> button type="submit" class="btn btn-default" aria-label="Left Align" style="border-radius: 0px; background-color:#333333; width:50px; border-color:#333333; padding: 15px 12px; margin-top:1px;"> /button> /form>

0xE111 commented 6 years ago

Time cures :)