I have a form that uploads an image in the directory upload. When I want to display the image by using asset, it isn't shown.
My code to diplay the image is the following:
<a href="{{ asset('uploads/' ~ doaDetails.dce) }}" class="mdi mdi-file-pdf" title="DCE">
<img src="{{ asset('uploads/Annexes' ~ doaDetails.annexe1 ) }}" >
</a>
Related
The site is able to display the whole content of the blog but not the video contents. When I try to post a new post including video. It's just show html script. Is there anyway to pass the whole html from python?
I'm using post.content to pass the post content to the html page. It works with text, but not image or video.
home.html:
<div class="media-body">
<div class="article-metadata">
<a class="mr-2" href="{{ url_for('users.user_posts', username=post.author.username)}}">{{ post.author.username }}</a>
<small class="text-muted">{{ post.date_posted.strftime('%Y-%m-%d') }}</small>
</div>
<h2><a class="article-title" href="{{ url_for('posts.post',post_id=post.id) }}">{{ post.title }}</a></h2>
<div class="article-content"><{{ post.content }}></div>
</div>
Routes.py
def home():
page = request.args.get('page', 1, type=int)
posts = Post.query.order_by(Post.date_posted.desc()) \
.paginate(page=page, per_page=5)
return render_template('home.html', posts=posts)
sample image
I just found the answer. Just use this:
{{ post.content|safe }}
Work like a charm.
Refer: passing string with html tags to html from python
I want to POST a form from my html to views.py in django, but I am not able do it.
This is my html. This form should post the url of the downloaded image to the views.py function.
{% for x in photo %}
<a class="down" href="{{x.image.url}}" onclick="myfunc()" download="none">get</a>
<form action="/image_info/" method="POST" id='dform'>
{% csrf_token %}
<input name='d_button' type="hidden" value="{{x.image.url}}">
</form>
{% endfor %}
This is my javascript function to submit form whenever the get link is pressed
<script type="text/javascript">
function myfunc()
{
document.getElementById("dform").submit();
console.log('hello');
}
</script>
this is my views.py. This should take the image url from the form and display it in the new page.
def show_image():
image_url = request.POST.get('d_button', None)
return render(request, 'show_image.html', {'image': image_url)
but my problem is that the form is not returning the url of the image that is clicked instead it is returning the url of first link. for example
link1
link2
link3
link4
if I click on link3, it is downloading the image in link3 but POSTING the url of link1.
This is a bit tricky to explain but this is the best I can.
Thanks in advance.
HTML ids are supposed to be unique. You are looping while generating these forms and hence generate a bunch of duplicate ids, so when you write document.getElementById("dform") the first matching element is selected.
One solution would be to use forloop.counter to generate unique ids and use them. We would set these ids as an attribute on the anchor and pass the anchor element to the onclick function:
{% for x in photo %}
<a class="down" href="{{x.image.url}}" onclick="myfunc(this)" data-target="dform-{{ forloop.counter }}" download="none">get</a>
<form action="/image_info/" method="POST" id='dform-{{ forloop.counter }}'>
{% csrf_token %}
<input name='d_button' type="hidden" value="{{x.image.url}}">
</form>
{% endfor %}
Now in your javascript:
<script type="text/javascript">
function myfunc(elem)
{
document.getElementById(elem.getAttribute("data-target")).submit();
console.log('hello');
}
</script>
I made a special slider for the products on the catalog page. However, I cannot bring the images of the product in a loop. How can I get all the photos of the product?
This is the product_card.twig file. I want to create a for loop based on the product.
product_card.twig
{% for image in product.images %}
<div class="carousel-item active">
<img class="d-block" src="{{ images.thumb }}" alt="product-img">
</div>
{% endfor %}
I have problem for displaying img, i saved images with name of book and now to access them need to concatenate .jpg extension. So my problem is how to concatenate variable and string inside twig template ?
<img src="{{ path({{ book.name ~ '.jpg' }}) }}"/>
here is full code:
{% for book in books %}
<a href="{{ path('AppBundle_Book_detailsBook', {'bookId': book.id}) }}"
class="col-2 white kartica">
<img src="{{ path({{ book.name }} ~ '.jpg' ) }}"/>
<h4>
{{ book.name }}
</h4>
</a>
{% else %}
No books.
{% endfor %}
Based on your comments, I think this is what you need:
<img src="{{ asset( '../app/Resources/images/covers/' ~ book.name ~ '.jpg') }}"/>
You might need to adjust the path slightly. Assets start from the web/ directory. I think you should get the idea.
By the way, thank you for making a detailed post!
Why donĀ“t you try these:
<img src="{{ path( book.name ~ '.jpg' ) }}"/>
I have a custom page in /web/blog and I want to create a link to forward to it. How can I do this ? I have tried many things but none of them works, like
<a href="{{ path('homepage') | '/blog' }}" title="" >{{ 'menu.blog'|trans }}</a>
or
<a href="{{ path('homepage') }}/blog" title="" >{{ 'menu.blog'|trans }}</a>
I don't know why but the right answer is :
<a href="{{ path('homepage') }}{{ 'blog' }}">
i use
<a href="{{ path('homepage')}}/{{blog}} title="">
#Adam solution is right