flask url conflicting with href - flask

In my HTML code i have the following jinja template code:
{% for site in sites %}
{{ site.site_name }}
{% endfor %}
The issue is: let's assume that {{ site.site_link }} = www.domainname.com, so whenever the link is clicked it supposed to direct the user to the domainname page. but, it leads to :
http://127.0.0.1:5000/www.domainname.com
which is a 404..Any idea why is this happening and how to fix it?
I don't know if this is relevant, but, I am using flask Blueprints.

You need to put http:// in front of it, like href="http://........"
{% for site in sites %}
{{ site.site_name }}
{% endfor %}

Related

DjangoCMS render_model with HTML

I'm using Django CMS and the Aldryn News Blog app on a client site to display a blog listing. The default template code uses
{% render_model article "lead_in" %}
to display the "Lead in" excerpt text. However it is displaying p tags for me and I actually need them to render.
https://pasteboard.co/GSq8XObQ.png
https://pasteboard.co/GSq8ONF.png
I have tried both:
{% autoescape off %}
{% render_model article "lead_in" %}
{% endautoescape %}
{% render_model article "lead_in" |safe %}
The first does nothing and the second errors out. How can I get it to render the html tags?
Have you tried this solution?
{{ article.lead_in|safe }}

replace url in Django template

{% if shelf.script or shelf.log_url %}
{% if shelf.log_url %}
Log URL: {{ shelf.log_url }}<br />
{% endif %}
{% if shelf.log_url %}
<b>Another URL:</b> {{ shelf.log_url.replace("/tmp/BOOTLOG/", "http://www.sxp.com") }}<br />
{% endif %}
{% endif %}
I have already got a log URL, I would like to update this URL with a domain address and adding /index.html to at the end.
I would like to replace this URL
shelf.log_url output = /tmp/BOOTLOG/darwin_12345.tgz
to something like this.
www.sxp.com/darwin_12345/index.html
How can I do in Django template ?
What you are trying to do can be achieved by a custom template tag. You can find more about custom template tags at:
https://docs.djangoproject.com/en/1.11/howto/custom-template-tags/
you should try using custom filter for this. and you will get better idea from Django custom filter documentation
for example
{{shelf.log_url|replace:"url you need"}}
add filter in your any file for example filters.py
#register.filter(name='replace')
def replace(url, new_url):
line = url.replace('/tmp/BOOTLOG/', new_url)
return line

Why is this django template tag failing to display?

I have a template tag located in catalog/templatetags/catalog_tags.py, which looks like this:
register = template.Library()
#register.inclusion_tag("tags/navigation.html")
def nav_links():
flatpage_list = FlatPage.objects.all()
return {'flatpage_list': flatpage_list }
I have a catalog.html which has {% load catalog_tags %}, to load that tag, and is followed by an inclusion tag for my navigation, {% include "tags/navigation.html" %}.
navigation.html contains the following:
{% with flatpage_list as pages %}
{% for page in pages %}
{{ page.title }}
{% endfor %}
{% endwith %}
But the list of flat_pages is not appearing in my navigation section. Why is that?
If I understand right, with your current state you have something liek this in catalog.html template:
{% load catalog_tags %}
.....
{% include "tags/navigation.html" %}
What this code does, is just renders the "tags/navigation.html" template, nothing more. So your custom template tag is not hit at all. To fix it, you should replace include with nav_links:
{% load catalog_tags %}
.....
{% nav_links %}
See Django docs for reference.
Not sure if it's just a copy paste error or not but return {'flatpage_list': flatpage_list isn't closed properly return {'flatpage_list': flatpage_list}
Also could this be something more suited for a context processor?
EDIT: After reading the other answer, I realized what you are trying to do, when you were using the {% include ... %} tag it seemed like you just wanted to populate the flatpage_list

Django 1.3 new "localize" tag/filter doesn't work?

I've noticed that Django 1.3 introduced the new "localize" tag/filter.
http://docs.djangoproject.com/en/1.3/topics/i18n/localization/
It says:
To activate or deactivate localization
for a template block, use:
{% localize on %}
{{ value }}
{% endlocalize %}
{% localize off %}
{{ value }}
{% endlocalize %}
However I always got the error message:
Invalid block tag: 'localize'
Looks like the "localize" filter doesn't work as well. Can anybody help me? Or can anybody else confirm if this is a bug? Thanks!
Have you loaded the localization template tags with:
{% load l10n %}
before using "localize" template tag?

Using django-paging extension with Django and Jinja2/Coffin

Recently I switched my templating engine from default to Jinja2/Coffin. Everything works just fine but I'm having troubles trying to use Django/Jinja2 django-paging (http://linux.softpedia.com/get/Internet/HTTP-WWW-/django-paging-58496.shtml) extension in my project.
There is an example how to use this extension with Jinja:
{% with paginate(request, my_queryset) as results %}
{{ results.paging }}
{% for result in results.objects %}
{{ result }}
{% endfor %}
{{ results.paging }}
{% endwith %}
Simply, I don't know where and how to define this new tag paginate to be recognized by Jinja2 engine.
I tried to put is in settings.py as:
JINJA2_EXTENSIONS = (
'paging.helpers.paginate',
)
but the error is raised:
paginate() takes at least 2 arguments (1 given)
Any help is appreciated.
Ok, problem solved. The paging application should be added into INSTALLED_APPS (settings.py)