how to view image in Django application - django

I need to view only the image in HTML page.
Like : http://localhost:8000/media/project_name/imagenav.jpg
Example page : https://s3-media3.fl.yelpcdn.com/bphoto/c7ed05m9lC2EmA3Aruue7A/o.jpg
Thanks & Regards,
Mohamed Naveen

It's simple
Your statics must be configured first.
{% load static%}
<br>
<img src = "{% static 'image name in static folder'%} " >
Check my site on heroku
http://pyworldpy.herokuapp.com

Related

Google analytics for Django app only shows root domain instead of page titles

I've got my first Django app in production and would like to setup Google Analytics to see pageviews/timespent/etc by page title for the order flow. I've added the GA javascript to the of my base.html template with the hope that it would track each page with page title.
However, when I look at Google Analytics, I only see page views by my root domain 'mysite.com', and I cannot get get page views by '/app/pagetitle1', '/app/pagetitle2', '/app/pagetitle3', etc. 'app' is the Django app that the root domain gets redirected to 'mysite.com/app'. I'm assuming that Google Analytics would show entire path after root domain, if it were to work.
It seems like there is probably something simple I've overlooked, and would appreciate any advice.
Here's the GA tag I put in base.html right after per GA instructions:
<!-- Global site tag (gtag.js) - Google Analytics -->
<script async src="https://www.googletagmanager.com/gtag/js?id=XXX"></script>
<script>
window.dataLayer = window.dataLayer || [];
function gtag(){dataLayer.push(arguments);}
gtag('js', new Date());
gtag('config', 'XXX');
</script>
Each template extends base.html as follows:
{% extends "base.html" %}
{% block content %}
<section class="container container-interior">
etc - rest of <body>
I made two changes to address lack of page title and double counting.
Remove this gtag call from my base.html template
gtag('config', 'G-ID',{'send_page_view': true});
Added this to each page that I wanted to track using a block tag.
gtag('event', 'page_view', { page_title: '', page_path: ', send_to: '<G-ID'> })

WeasyPrint HTML to PDF not rendered Images in TEST VM Environment - Django

I am using Django + WeasyPrint for HTML to PDF generation.
In HTML template I am having around 2 SVG files. While running in Development (local) server, it rendered SVG Images when converting HTML to PDF. Whereas in TEST Server(Which runs by using NGINX and Docker Container), it fail to render SVG files. In WeasePrint, I have mentioned my TEST URL (http//test.sampleproject.com:8000) in the base_url. I don't know why it is working in my dev server and not working in TEST Server
My Code.
html_string = render_to_string('sample.html', {'data': data})
result = HTML(string=html_string, base_url='http//test.sampleproject.com:8000').write_pdf(presentational_hints=True)
response = HttpResponse(result, content_type='application/pdf')
response['Content-Disposition'] = 'inline; filename={}'.format('sample.pdf')
return response
In my Template file I have loaded around 10 images. Ex: 'sample.html' template file:
{% load staticfiles %}
<img src="{% static 'svg/Logo1.svg' %}" alt="" />
<img src="{% static 'svg/Logo2.svg' %}" alt="" />
Please help me to render the SVG files in the output PDF.
I've done some work in the past with Django + Weasyprint and the only time I had issue rendering images was due to the fact that Weasyprint accesses the image in a different way.
I found that I had to add a static endpoint for it to access the files from, as it is requesting the static images externally to Django. Here's how you can enable your static resources to be accessed externally;
https://docs.djangoproject.com/en/3.0/howto/static-files/#s-serving-static-files-during-development
Also as FlipperPA mentions, ensure you have all the OS level libraries installed on your server;
https://weasyprint.readthedocs.io/en/latest/install.html
Hope this helps :)

django: url routing to hardcoded static and html files

I have a legacy project with a bunch of html files and static files (css, images, js, videos ...). All links in those html files are relative and hardcoded:
"css/main.css" or "img/my_img.jpg" etc.
I need to run that project using django.
I cannot change all those links to kind of
{% load static %}
<link rel="stylesheet" href="{% static 'css/main.css' %}">
but all recipes I found in internet suggest using exactly that method.
How can I:
1) route hardcoded urls like "css/main.css"
2) route hardcoded urls like "another.html" ?
This can be done with much ease like this<!--lets' say image-->
<img src="{% static 'static/images/image1.png' %}">.Please make sure the file directories match with that in the code.
Please visit https://docs.djangoproject.com/en/1.10/howto/static-files for detailed info

Is it possible to display a specific block from a template in django?

Just like in html when you reference a section of a page with for instanceVisit the Useful Tips Section
Would there be a way to do a similar thing in django if for instance I wanted to load my page straight to the tips section? I am extending base.html to my home page that has a tips section. Right now i have a static url home i want to do the exact same but with djangos dynamic url something like {% url 'home'/#tips %}
You can just add the fragment identifier right after the URL returned by the {% url %} template tag:
home

Custom image in Django template via static

I am programming a website powered by Django 1.6.3. Under news you always see a big story and on the side recent articles. Therefore, I used Bootstrap 3 and wrote a static html page serving my requirements.
Now I would like to program the logic behind this. I save all my files under static that consists of the folders css, fonts (from Bootstrap), img and js. The image folder has some subfolder e.g. news. To create a new entry on the news page I would like to open the admin page, add a news_entry and select one image that should be under the titel. How is it possible to include the image in the page? My approach was:
<img src="{% static {{ news.image_name }} %}" class="img-title">
Unfortunately I get an parsing error. Image_name is a property of my news model.
You need to access the url attribute of your image_name.
Try:
<img src="{{ news.image_name.url }}" class="img-title">
Similar question here.