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.
Related
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 }}
I'm having trouble accessing database data in another template
different than django administration. In the administration page I
get results:
But when I try to display them in another template:
(No results available)
Here's how my urls.py admin page and template urls looks like:
urlpatterns = [
path('admin/', admin.site.urls),
path('lista/', views.entitats, name = 'lista_entitats')
]
Here's how my admin.py call to entity list and register looks like:
class EntitatAdmin(admin.ModelAdmin):
list_display = ('codi', 'nom', 'adreça', 'cp', 'correu', 'inspector')
inlines = [DiaLliureDisposicioInline, VisitesDirectorCentreInline, ExitEscolarInline, ObresCentreInline, OfertaEstudisInline, NEE_alumnatInline, FormacioInline, ProjectesInline, ProfessorsInline, DespesesFuncionamentInline]
search_fields=('nom',)
list_per_page=5
admin.site.register(Entitat, EntitatAdmin)
In my views.py this is how I ask for the template (and where I feel I'm not getting db information well):
from entitats.models import Entitat
def entitats(request):
lista_entitats = Entitat.objects.all()
context = {'llista_entitats': lista_entitats}
return render(request, 'entitats.html', context)
And finally my template, where I try to display the list of entities:
{% if context.llista_entitats %}
<ul>
{% for question in objects_list %}
<li>{{ question.question_text }}</li>
{% endfor %}
</ul>
{% else %}
<p>Ninguna entidad disponible.</p>
{% endif %}
Sorry if it's loong, I'll delete if there's something not necessary, and THANKYOU in advance.
You don't need context.variable_name syntax in template to check variable value, just use:
{% if llista_entitats %}
instead of
{% if context.llista_entitats %}
Also since your variable is llista_entitats it should be:
{% for question in llista_entitats %}
<li>{{ question.question_text }}</li>
{% endfor %}
Note I replaced objects_list with llista_entitats and entitat.id with question.id.
I am trying to obtain a url to a view that renders an image so I can use it in an img tag with href.
But my {% url viewname object_id %} is not working. Here are the specifics:
my urls.py:
hydrourlpatterns = patterns('',
url(r'^graphs/$','hydro.views.graphs',name='graphs'),
url(r'^graphs/new/$', 'hydro.views.add_graph', name='add_graph'),
url(r'^graphs/(?P<graph_id>\d+?)/$', 'hydro.views.single_graph', name='graph_detail'),
url(r'^graphs/graphImage/(?P<graph_id>\d+?)/$', 'hydro.views.render_graph', name='graphImage')
)
my template(url: localhost/graphs/(graph_id)/):
{% extends "subpage.django" %}
{% block content %}
{% if graph %}
<h3> {{ graph.name }} </h3>
<h1> {% url 'graphImage' graph_id %} </h1>
{% endif %}
{% endblock %}
The error I keep getting is ViewDoesNotExsist.
Could not import hydro.views.add_site.
View does not exist in module hydro.views.
You use hydro.views.add_site somewhere else in your urls.py. Comment this url line or create add_site view in the hydro.views.
EDIT: You don't pass graph_id variable to the template so change {% url %} call to:
{% url 'graphImage' graph.id %}
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 %}
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.