URL output in template is empty - django

URL pattern declaration from views.py
url(r'^intentions/(\d+)/$', 'intentions.views.show'),
When I am writing address directly, like intentions/1 works ok, but when I trying to display URL show for object like:
{% for i in intentions %}
{% url 'intentions.views.show' i.id as iUrl %}
<li>{{ i }}</li>
{% endfor %}
I am ending with empty href. Could someone give me any advice?

If you are using Django 1.3 or 1.4, make sure you are loading the new url tag. Add the following line to the top of your template:
{% load url from future %}
If you are using Django 1.4 or earlier, and you haven't loaded the new url tag as above, then you need to remove the single quotes from the url pattern:
{% url intentions.views.show i.id as iUrl %}
As an aside, it's recommended to name your url pattern:
url(r'^intentions/(\d+)/$', 'intentions.views.show', name='show_intention'),
Then change your template to:
{% for i in intentions %}
{% url 'show_intention' i.id as iUrl %}
<li>{{ i }}</li>
{% endfor %}

you could try
{% for i in intentions %}
<li><{{ i }}</li>
{% endfor %}
don't use {% url 'intentions.views.show' i.id as iUrl %}, it's maybe confused.
Also,you could try modifiy the url as follow
url(r'^intentions/(\d+)/$', 'intentions.views.show', name="intention_view"),
then,
{% for i in intentions %}
{% url "intention_view" i.id as iUrl %}
<li>{{ i }}</li>
{% endfor %}
If there are also ending empty href,try the first method.

'intentions.views.show'
Also give a name='' to your URL and use it in your template code.

Related

tags are not getting displayed in template, Django

context['d_tags'] = Blog.objects.filter(is_published=True).values('tags__name').order_by('tags__name').distinct()
print(context['d_tags'])
this prints the out put as below
<QuerySet [{'tags__name': 'ev'}, {'tags__name': 'yoga'}]>
how can I show it on templates, tried the following way
{% for tag in d_tags.all %}
<li>{{ tag }}</li>
{% endfor %}
gives an out put in template as
{'tags__name': 'ev'}
{'tags__name': 'yoga'}
but if I do this way
{% for tag in d_tags.all %}
<li>{{ tag.name }}</li>
{% endfor %}
doesn't gives any thing in display, how I can get just the values in template
You said #root's solution doesnt work, but it should... Are you sure you tried {{tag.tags__name}} and not something else?
Another solution would be opening double for loops...
{% for tag in d_tags %}
{% for tag_name in tag %}
<li>{{ tag_name }}</li>
{% endfor %}
{% endfor %}
But hey, thats really the same thing as saying {{tag.tags__name}}

How to pass variable into Django block in url?

Context
I am trying to loop through a list of objects, then call on the name and id attribute and pass it into the Url block as regex to be captured in the urls.py later.
Problem
{%if people_found%}
{%for the_people in people_found%}
{{the_people.firstname}}
{%endfor%}
{%endif%}
Urls.py
app_name = 'Search4Bday'
urlpatterns= [path("search/profile/<str:firstname>/<int:id>", views.profile, name = "profile")]
Question
I'm not sure how I can pass the variables into the urls.py file if I can't call on the variable the_people with double curly braces. I'm also not sure if this is the correct way to pass in the variables into the regular expression capture portion of the urls.py. Any ideas?
Use add tag of Django Template Lang:
{% if people_found %}
{% for the_people in people_found %}
{{the_people.firstname}}
{% endfor %}
{% endif %}
edit: I didn't pay attention to urls.py and this will not work as wanted. Instead of adding slashes between variables put them directly. So this should work.
{% if people_found %}
{% for the_people in people_found %}
{{the_people.firstname}}
{% endfor %}
{% endif %}
I tried another method, I could have just done this:
{% if people_found %}
{% for the_people in people_found %}
{{the_people.firstname}}
{% endfor %}
{% endif %}
https://docs.djangoproject.com/en/4.0/ref/templates/language/

django template url - append variable to string

Im trying to add a variable to the end of a string and am getting issues with reverse match, from what I gather the below should work
{% for type in type_links %}
<li>{{ type.subnet_type }}</li>
{% endfor %}
I should have a url that has "all_1" for example in it.
however I am getting a reverse url error
Reverse for 'site_list' with arguments '('',)' not found. 1 pattern(s) tried: ['sites/site_list/(?P<display_filter>[\\w\\-]+)$']
is this correct way to add a variable to end the of the string?
EDIT:
url pattern, the pattern works as I have tested it manually before trying to create the URLs dyanmically
url(r'^site_list/(?P<display_filter>[\w\-]+)$', views.site_list, name='site_list'),
Thanks
You can do
{% for type in type_links %}
<li>
{% with type.id|stringformat:"s" as str_obj_id %}
{% with 'all_'|add:str_obj_id as extra_param %}
{{ type.subnet_type }}
{% endwith %}
{% endwith %}
</li>
{% endfor %}

Django template: check for empty query set

Is there a way to check for an empty query set in the Django template? In the example below, I only want the NOTES header to be displayed if there are notes.
If I put an {% empty %} inside the "for" then it does display whatever is inside the empty tag, so it knows it's empty.
I'm hoping for something that does not involve running the query twice.
{% if notes - want something here that works %}
NOTES:
{% for note in notes %}
{{note.text}}
{% endfor %}
{% endif %}
Clarification: the above example "if notes" does not work - it still displays the header even with an empty query set.
Here's a simplified version of the view
sql = "select * from app_notes, app_trips where"
notes = trip_notes.objects.raw(sql,(user_id,))
return render_to_response(template, {"notes":notes},context_instance=RequestContext(request))
Edit: the view select selects from multiple tables.
Have a look at the {% empty %} tag.
Example from the documentation
<ul>
{% for athlete in athlete_list %}
<li>{{ athlete.name }}</li>
{% empty %}
<li>Sorry, no athletes in this list.</li>
{% endfor %}
</ul>
Link: https://docs.djangoproject.com/en/1.8/ref/templates/builtins/#for-empty
If you are interested in a table, or some kind of heading if there are results, add the forloop.first:
{% for athlete in athlete_list %}
{% if forloop.first %}
Athlete Name:
{% endif %}
{{ athlete.name }}
{% empty %}
Sorry, no athletes in this list.
{% endfor %}
Try {% if notes.all %}. It works for me.
In your view check whether notes is empty or not. If it is then you pass None instead:
{"notes": None}
In your template you use {% if notes %} as normal.
It's unfortunate that you're stuck using a raw query set - they're missing a lot of useful behavior.
You could convert the raw query set into a list in the view:
notes_as_list = list(notes)
return render_to_response(template, {"notes":notes_as_list},context_instance=RequestContext(request))
Then check it as a boolean in the template:
{% if notes %}
Header
{% for note in notes %}
{{ note.text }}
{% endfor %}
{% endif %}
You could also make it happen without conversions using forloop.first:
{% for note in notes %}
{% if forloop.first %}
Header
{% endif %}
{{ note.text }}
{% endfor %}
What about:
{% if notes != None %}
{% if notes %}
NOTES:
{% for note in notes %}
{{ note.text }}
{% endfor %}
{% endif %}
{% else %}
NO NOTES AT ALL
{% endif %}
Your original solution
{% if notes %}
Header
{% for note in notes %}
{{ note.text }}
{% endfor %}
{% endif %}
Works now with Django 1.7 and thanks to QuerySet caching, it does not cost and extra query.
Often the right way to do this is to use the {% with ... %} tag. This caches the query so it runs only once and also gives you more flexibility with your markup than using {% empty %}.
{% with notes as my_notes %}
{% if my_notes %}
<ul>
{% for note in my_notes %}
<li>{{ note }}</li>
{% endfor %}
</ul>
{% else %}
<p>Sorry, no notes available</p>
{% endif %}
{% endwith %}
With this particular example I'm not sure how useful it is but if you're querying Many-to-Many field, for instance, it's likely what you want to do.
Use {% empty %} in django templates
{% if list_data %}
{% for data in list_data %}
{{ data.field_1 }}
{% endfor %}
{% else %}
<p>No data found!</p>
{% endif %}
We can write above code with {% empty %}.
{% for data in list_data %}
{{ data.field_1 }}
{% empty %}
<p>No data found!</p>
{% endfor %}

Django trans and url tags

I want to translate a paragraph containing a URL in a Django 1.3 application.
<p>
First edit your profile, please.
</p>
Depending on the language, the text surrounded by <a> tags will surely change. How can I allow translators to decide on the link placement? Wrapping the entire thing in a {% trans %} causes an error:
<p>{% trans "First <a href='{% url edit-profile username=user.username %}'>edit your profile</a>, please." %}</p>
The error thrown is TemplateSyntaxError: Searching for value. Unexpected end of string in column 64: trans "First <a href='{% url edit-profile username=user.username.
How should I go about doing this? Do I have to determine the URL in the view, then pass that URL as a string to the template? That seems like a really convoluted solution for what I would think is a very common problem.
Use {% blocktrans %}. The Django translation docs include this example:
{% url path.to.view arg arg2 as the_url %}
{% blocktrans %}
This is a URL: {{ the_url }}
{% endblocktrans %}
This works for me:
{% url "app-name:name-of-view" as the_url %}
{% blocktrans %}
This is a URL: {{ the_url }}
{% endblocktrans %}