Closed Adrafe-Rinzzler closed 6 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=...">
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
}
)
Maybe this will help
queryset = Product.objects.filter(category__in=instance.get_descendants(include_self=True)) if instance else Product.objects.all()
It won't as it shows all products if instance is none...why the instance is becoming none when sending the query?
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/)
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
The resolution fails when I append that query string....wonder why?
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 ...
Ah, I think I got it... Still write the output
list index out of range
Traceback: File "C:\Python27\lib\site-packages\django\core\handlers\base.py" in get_response
Exception Type: IndexError at /category/women Exception Value: list index out of range
the resolution fails when category/women/?color=blue
I've made some changes, try to update 116590e22a271e5609b4a1c3607af03dccade8ab
OK Alex....just keep this issue open for sometime...if incase
updated...what changes did u make?
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>
Time cures :)
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