How does Django get id on url? - django

When i clicked my menu url only gets category name and slug cant reach id how can i solve that ?
This is the url i get
def category_products (request,id,slug):
category=Category.objects.all()
products=Product.objects.filter(category_id=id)
context={'products':products,'category':category,'slug':slug, }
return render(request, 'kiliclar.html', context)
urlpatterns = [
path('category/<int:id>/<slug:slug>/', views.category_products,name='category_products'),
]
template
{% recursetree category %}
<li class="dropdown">
{{ node.title }}
{% if not node.is_leaf_node %}
<ul class="dropdown-menu">
<li>{{ children }}</li>
</ul>
{% endif %}

How does Django get id on url?
You need to supply it when you generate the "href" in the template.
The URL pattern says:
path('category/<int:id>/<slug:slug>/',
views.category_products,
name='category_products')
So the URL in the href in the template needs to look the same as the pattern:
href="/category/{{ node.id }}/{{ node.slug }}"
Or better still use the 'url' template function to expand the url from the pattern:
href="{% url category_products id=node.id slug=node.slug %}"
Here is a similar Q&A:
How to add url parameters to Django template url tag? to give you another example.

You need to add one more argument, it's id.
{{ node.title }}
It should be;
{{ node.title }}

Related

How to set href value in html using python

I have been trying to solve this problem: i want to set url to html a tag using loop. I tried this way. But it gives me error which is "Reverse for 'i.menuResolve' not found. 'i.menuResolve' is not a valid view function or pattern name".
In case, "i.menuResolve" returns url which is '/sales/profile' etc.
{% for i in userMenus %}
<li>
<a href="{% url 'i.menuResolve' %}" ></a>
</li>
{% endfor %}
Please help if anybody knows this error?
If you have a method or property that returns a URL, you don't need to use Django's {% url %} template tag. That template tag passes the arguments to Django's reverse() function, but you don't need to do that if you've already got the URL.
Give this a try:
{% for i in userMenus %}
<li>
<a href="{{ i.menuResolve }}" ></a>
</li>
{% endfor %}

Django pass kwargs through URL

The purpose of this section of code is to show all of the requests to join a group in a template similar to that shown below:
Request 1 | Add | Delete
Request 2 | Add | Delete
Request 3 | Add | Delete
....
What I have thought to do is to to make the 'add' and 'delete' button href's to a function in the view. However I am wondering what the proper way to pass a **kwarg from a template to a view. Else if there is any better way to accomplish this?
template
{% for asking in requested %}
<a href={% url 'group_judge_request' group_profile.slug decision=0 %}>Cut {{ asking.user.username }}</a>
<a href={% url 'group_judge_request' group_profile.slug decision=1 %}>Keep {{ asking.user.username }}</a>
{% endfor %}
url
url(r'^judge_request/(?P<gslug>[\w-]+)$',
group_judge_request,
kwargs={'decision':'decision'},
name='group_judge_request'),
view group_judge_restart
def group_judge_request(request, gslug, decision):
view group_requested_invites
def group_requested_invites(request, gslug):
....
requested = GroupRequestToJoin.objects.filter(group=group_profile.group, checked=False)
return render(request, "groups/group_requested_invites.html", {
'requested' : requested,
})
Error:
Don't mix *args and **kwargs in call to reverse()!
I don't think there is a way to pass *kwargs like this from the template using the built-in url template tag.
There are two ways you can do this, one is to create two separate urls and pass the decision as a kwarg to the view:
urls.py
url(r'^judge_request_cut/(?P<gslug>[\w-]+)$',
group_judge_request,
kwargs={'decision': 0},
name='group_judge_request_cut'),
url(r'^judge_request_keep/(?P<gslug>[\w-]+)$',
group_judge_request,
kwargs={'decision': 1},
name='group_judge_request_keep'),
template
{% for asking in requested %}
<a href={% url 'group_judge_request_cut' group_profile.slug decision=0 %}>Cut {{ asking.user.username }}</a>
<a href={% url 'group_judge_request_keep' group_profile.slug decision=1 %}>Keep {{ asking.user.username }}</a>
{% endfor %}
Or you could pass the integer as a parameter:
urls.py
url(r'^judge_request/(?P<gslug>[\w-]+)/(?P<decision>\d{1})$',
group_judge_request,
name='group_judge_request'),
template
{% for asking in requested %}
<a href={% url 'group_judge_request' group_profile.slug 0 %}>Cut {{ asking.user.username }}</a>
<a href={% url 'group_judge_request' group_profile.slug 1 %}>Keep {{ asking.user.username }}</a>
{% endfor %}
I think you want to use a url query. So your url will be as follows:
Cut {{asking.user.username }}
You can then go on to list the queries using:
request.META['QUERY_STRING']

Combine built-in tags in templates with variables

I want to combine the built-in tag: {% url %} with a dynamic url which I parse with {{ url_value }}
I tried doing: {% url 'urlname' url_value %}, but it didn't work
This is the url:
url(r'^(?P<slug>[^/]+)/$', 'reviews.views.single_product', name='product_detail'),
{{url_value }} just represents the slug
I think it should be like:
{% url product_detail slug=url_value %}

Url template tag and NoReverseMatch

I pass array of objects from view to template, where I want to generate URLs for each object (to different view). So, I have in my URLconf:
url(r'^item/(?P<id>[0-9]+)/(?P<slug>[a-zA-Z0-9]+)$',
'show_item',
name='show_item'),
In template, I iterate on object list and try to generate URL which fits to above URL example, so I pass 2 params to each one:
{% for item in items %}
Item: {{ item.title }}, description: {{ item.description }}
URL: {% url show_item item.id item.slug %}
{% endfor %}
Unlucky, I get django error:
Reverse for 'show_item' with arguments '(1, u'first-item')' and keyword arguments '{}' not found.
What did I do wrong?
In your urls your slug regex needs to contain a hyphen (and might as well add an underscore while we're at it): (?P<slug>[a-zA-Z0-9_\-]+)
Your arguments are named:
{% for item in items %}
Item: {{ item.title }}, description: {{ item.description }}
URL: {% url show_item id=item.id slug=item.slug %}
{% endfor %}
Documentation for named groups in urls
If I'm not wrong show_item should be quoted and parameters named
{% url 'show_item' id=item.id slug=item.slug %}
also check what is generated by url using:
{% url 'show_item' id=item.id slug=item.slug as foo %}
{{ foo }}
"as foo" allows you to see the generated url without raising errors.

Reference a url with an id by name in Django

I want to reference a dynamic url in my templates using its name, but am not sure how to incorporate the object id. In other words, I want to reference "/products/98" in my template without having to hard code it (as my url patterns might change).
In my urls.py, I have:
url(r'^products/(\d+)/$', 'products.views.show_product', name='product'),
How do I name my pattern such that I can call {% url ??? %} in the template to get the correct item with a specified id. e.g.
{% for product in product_list %}
Product #{% product.id %}
{% endfor %}
Use {% url product product.id %}.
urls.py:
url(r'^products/(?P<product_id>\d+)/$', 'products.views.show_product', name='product'),
template:
{% load url from future %}
{% for product in product_list %}
Product #{{ product.id }}
{% endfor %}
Make sure your products.views.show_product view function takes product_id as a parameter.