How to make DjangoTemplates work recursively - html

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.

Related

Show excel file in django template

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.

Serving an image on django web app

I'm trying to serve an image on my Django web app, but it seems to be giving me a broken image link. I put the picture I want to serve in the static folder but the hello.html is in templates folder , hoping that would work although it didnt. Here is the hello.html
{% load static %}
<html>
<body>
<form method="post" enctype="multipart/form-data">
{% csrf_token %}
{{ form }}
<input type="submit" value="Upload">
<p>Hello</p>
{% load static %}
<img src="{% static "filecomparison/Example.jpg" %}" alt="My image"/>
</form>
</body>
</html>
This is what I see on the website
I'm new to django and I've been reading guides and watch youtube videos but they dont seem to help me to do what i actually i want, any help is appreciated
When you're using django you have to put images, CSS, JS, etc. in a static files directory. Read up on this: https://docs.djangoproject.com/en/2.0/howto/static-files/
Make sure you load static files in your HTML with the line {% load static %} at the top!

display all images in folder using jinja2

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.

Image not fetched correctly from the specified path - Django Template

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).

Template inheritance for serving static file on GAE not working

I have a small GAE app that's working fine. I use it to serve a few static files as well like (About page, Privacy and contact page). I put the "about.html" in a /static folder along with the /css, /img and /js. In "app.yaml" I declared the handler:
- url: /static
static_dir: static
It all works fine. Now I've abstracted the common contents (navigations, etc) into "_base.html" like this:
<html>
<head><title> Page title here</title>
<!--Some header files here -->
</head>
<div id="content">
{% block bodycontent %}
{% endblock bodycontent %}
</div>
</body>
</html>
The child file looks like this:
{% extends "_base.html" %}
{% block title %} About {% endblock %}
{% block bodycontent %}
<p>Some contents here...</p>
{% endblock bodycontent %}
Now, problem when it displays, the "_base.html" does not render. In fact the whole jinja code just displays. But when I wrote an handler for "about.html" it renders the base html correctly.
Question is, why do I have to create instances b4 I can display static files like About, Privacy pages just because I want to use template inheritance? Am I doing something wrong?
Templates are not static files, by definition. If you want a completely static HTML file, you can of course have one without any handler code. But templates require rendering, which means they need a handler to do it.