Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
Adrafe-Rinzzler opened this issue Nov 13, 2015 · 16 comments

Comments

@Adrafe-Rinzzler
Copy link

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
Copy link
Owner

0xE111 commented Nov 13, 2015

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
Copy link
Author

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
Copy link
Owner

0xE111 commented Nov 13, 2015

Maybe this will help

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

@Adrafe-Rinzzler
Copy link
Author

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

@0xE111
Copy link
Owner

0xE111 commented Nov 13, 2015

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
Copy link
Author

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
Copy link
Author

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

@0xE111
Copy link
Owner

0xE111 commented Nov 13, 2015

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
Copy link
Owner

0xE111 commented Nov 13, 2015

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

@Adrafe-Rinzzler
Copy link
Author

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_urls__init__.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
Copy link
Author

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

@0xE111
Copy link
Owner

0xE111 commented Nov 13, 2015

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

@Adrafe-Rinzzler
Copy link
Author

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

@Adrafe-Rinzzler
Copy link
Author

updated...what changes did u make?

@Adrafe-Rinzzler
Copy link
Author

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
Copy link
Owner

0xE111 commented Dec 4, 2017

Time cures :)

@0xE111 0xE111 closed this as completed Dec 4, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants