In my django application I am showing the uploaded files like this:
{% extends 'base.html' %}
{% block content %}
{% for i in images %}
<embed src="{{ i.document.url }}" width="1600"
height="700"
type="application/pdf"/>
<a href="{% url 'employee:delete_product_file' pk=pk %}" class="btn btn-outline-secondary" role="button" >Delete</a>
{% endfor %}
{% endblock %}
Views.py
def view_product(request,pk):
print("function called with pk ",pk)
images = Uploaded_products.objects.filter(products_connected = pk)
print("images ",images)
return render(request, "packsapp/employee/productspdf.html", {'images': images, 'pk': pk})
It's working fine when I upload a PDF file but when there is any other format like .docx or any excel file it juts simply downloads the file.
How do I show all the file types in the django template?
I am assuming it has something to do with the type="application/pdf
Not exactly what you're asking, but another approach is to publish your files to google drive and then embed them.
Related
I'm using django-inlinecss to style emails.
Here's a snippet of my email template:
{% extends "base/email.html" %}
{% block content %}
{% load inlinecss %}
{% inlinecss "base/css/email.css" %}
<p class="c-c-test">test</p>
{% endinlinecss %}
{% endblock %}
When rendering the template, an error occurs saying:
No such file or directory: '/usr/src/project/static/base/css/email.css'
Although, trying to use the same css file path (i.e. "base/css/email.css") with static template tag works fine.
What am I doing wrong here?
EDIT:
I'm using docker, django-inlinecss seems to load the css file from local storage (i.e. from docker image) where there's no such file. The actual file is located on my machine. Is there a workaround to solve this issue? something like how static template tag work?
I have a template home.html which is the view of an app in Django. Now, I've added some templating in the html file to allow dynamic generation of HTML. For example I use {% load static %} and then href="{% static "path/to/resources" %}". So, when I open the app, after running the server, the path is dynamically created.
Now, the problem is that the static files, that are dynamically loaded, also need to load other static files (and extend a template as well). I thought that DjangoTemplating might be working recursively, and will work on the called file too, but sadly that is not true.
So, what should I do to make sure that all my templating logic is taken into consideration by Django, and is allowed to run?
home.html snippet:
{% load static %}
<area alt="andhra" title="Andhra Pradesh" name="andhra" href="{% static "personal/pages/andhra.html" %}" shape="poly" ... />
andhra.html looks something like:
{% extends "personal/post.html" %}
{% blockcontent %}
<style>
#slider
{
width: 80%;
....
<div class="carousel-inner">
<div class="carousel-item active">
{% load static %}
<img class="d-block w-100" src="{% static "personal/images/andhraImages/1911-1915.jpg" %}" alt="First slide">
</div>
...
{% endblock %}
Which wants to extend the template:post.html which has {% blockcontent %}and {% endblock %} in it's body.
The andhra.html is not being template-processed. That is, when I open the app home.html is loaded correctly, but when I go to andhra.html from home.html, it isn't processed by DjangoTemplating at all.
So, as Daniel Roseman said in the comments, loading the files as a static file won't work. We want django to render them. So, I created a function state in my views.py as :
def state(request,state):
return render(request,'personal/pages/'+state+'.html')
That means it tries to render a file at templates/personal/pages/<state>.html. Now my urls.py looks like:
urlpatterns = [
path('',views.index,name='index'),
path('<state>',views.state,name="state")
]
Note that this belongs to the app polls. Now, since the urls.py of mysite has polls/, thus now the link to these views will be accessible by localhost:8000/polls/<state>.
Now at every place I wanted a static link, I made a dynamic one by:
href="{% url 'state' 'uttarakhand' %}"
where 'state' is the name of the urlpattern and 'uttarakhand' is the input value.
How do I correctly add javascript source assets in Jekyll?
I have tried to add the source link inside _includes\head.html below
which I feel that its not a good convention.
<link rel="stylesheet" href="{{ "/assets/css/main.css" | prepend: site.baseurl }}">
{% include head/meta.html %}
{% include head/links.html %}
{% include head/scripts.html %}
{% include head/styles.html %}
{% include my-head.html %}
<script src="https://cdnjs.cloudflare.com/ajax/libs/particlesjs/2.1.0/particles.min.js"></script>
Here is how the folder tree structure looks like. I am aware that I
have multiple places for my assets `js` and `css` , which I will organize later.
Your question is not very clear, but I would say that you are loading the particles library after you are referencing it. So your browser console should have some errors which you did not mention...
You should include particles js before your scripts.
{% include head/meta.html %}
{% include head/links.html %}
<script src="https://cdnjs.cloudflare.com/ajax/libs/particlesjs/2.1.0/particles.min.js"></script>
{% include head/scripts.html %}
{% include head/styles.html %}
{% include my-head.html %}
or even better way is to just add it to your head/scripts.html so that you have all the script imports in one place, but even there you should watch on the order of importing. If some script has dependancy, it dependency should be imported before it.
I sit possible to render a template by providing a folder path and let jinja2 find all the images in that folder and include them as images? Something like
{% for image in find_all_files_in_path(path) %}
<img src="{{ image }}">
{% endfor %}
It is not possible. Jinja2 only render the context you pass into the templates through variables. So you should send to the template a list with the image urls for jinja to be able to render your images. And then you can loop over this sequence;
{% for image_url in image_urls %}
<img src="{{ image }}">
{% endfor %}
Commonly you use jinja with an web framework (like django, flask, etc) which gives you the possibility to access objects in the templates.
I need to pass the value of a variable and add it to the image source so I can fetch my images in the template as shown.
<div id="Layer7">
{% if imgs %}
{% for i in imgs %}
<img src="{% static "images\uploads\i"%}"">
{% endfor %}
{% else %}
{% endif %}
</div>
Variable imgs contains a list of images names so I need to attach it to the url shown in the img src in order to fetch the image. When the above code is excuted I get a broken image link with the the following url/static/images/uploads/i which is false.
Any help with this issue?
You really don't need to build manually the url for uploaded images, what you need to do is to setuo property MEDIA_ROOT and MEDIA_URL
I assuming you have MEDIA_ROOT pointing to 'static/uploads', and STATIC_URL is '/static/', so then your MEDIA_URL must be '/static/uploads/'
After that with as simple {{ img.url }} you'll gett the full path to the file (without adding nothing before).