I have a django template where i send an image actually its for an email.It only works when i keep the whole url like abc.com/images/a.png .
i have sent he image like this:
{
'picture':picture.url
}
This is the code for template that i am using for email template:
<img src="{{picture}}" alt="image" width="200" height="200" >
Am i missing something? as it only works when i keep the whole url?
To build an absolute URL you can use request.build_absolute_uri(picture.url) in your view.
Related
I have loaded the pictures to database(sqlite) and now, how to get that photo in html, can you guys tell me the steps to get there, thanks in advance
I want to grab the picture from database and show it in the html.
To show uploaded images in template:
In html file:
<img src="/media/{{image}}" /> #image is a model field name.
This is the way you can view uploaded image in template.
Your images are stored in your static files folder and not directly in the database. I assume you are using an ImageField to store the image in your model. Something like this:
image = models.ImageField(upload_to="static/images/", null=True, blank=True)
If that's the case, in order to display it in one of your templates what you have to do is refer to the image url:
<img src="{{ mymodel.url }}" />
I've been trying to generate URL's on a detailpage for my app. I want the url of the QR-Code to be a link to the detail page of the item. What i have right now is this:
<img src="{% qr_url_from_text "localhost:8000/items/{{item.id}}" %}" alt="QR-Code for the item">
the Problem with this is, that i want to give item.id as part of the url. But it's not seen as variable, so when i scan the qr-code, it opens the page http://localhost:8000/items/{{item.id}}, but i want it to have the actual id (e.g. 4) in the url, not {{item.id}} as string.
Is there any way to put variables in those URL names?
Thank you already
Do not make url using variable in template.
Do it in view, as context variable, and than use that variable in place of "localhost:8000/items/{{item.id}}" in template .
So, in your view, you will have something like:
def yourview(request, pk):
qrcode_url = "localhost:8000/items/" + str(pk)
context = {
qrcode_url: 'qrcode_url',
}
return render(request, 'yourtemplate.html', context)
and than in template:
<img src='{% qr_url_from_text qrcode_url size="t" version=10 image_format="png" error_correction="L" %}' alt="QR code">
I have a view that renders a template with an image:
<html>
...
<img src="/static/img/mypic.jpg" />
...
</html>
I want to write a unit test that asserts this img src is valid and does not return a 404 broken link.
def test_myimage(self):
response = self.client.get('/static/img/mypic.jpg')
However, although I know this link is in fact valid, the test returns 404.
Why does this occur and how can I test valid img src links?
The easiest way is to use Django's built-in static file tools documentation here. Then you can use something like <img src="{% static 'mypic.jpg' %}" /> and Django will take care of making sure the file exists. If you use this, then you don't need to write unit tests for static files at all.
I am trying to interactively display plots from matplotlib in django. From this answer, I see that I can send the plot from a view with this code:
response = HttpResponse(mimetype="image/png")
# create your image as usual, e.g. pylab.plot(...)
pylab.savefig(response, format="png")
return response
So the view sends the plot as an Httpresponse, but how do I reference that in the html code of the template? I'm guessing it will be something like this but am having a tough time finding examples of html code:
<img src={ some reference to the plot here }>
Again, I think I can generate the plot with a view but am not sure how to reference that output in the html template.
A view is served by a URL. This view exists purely to serve the content of an image, therefore you should simply use its URL as the src in your img tag. For instance:
urlpatterns = [
path('path/to/my/image', views.my_image, 'my_image')
]
...
def my_image(request, ...):
response = HttpResponse(mimetype="image/png")
# create your image as usual, e.g. pylab.plot(...)
pylab.savefig(response, format="png")
return response
...
<img src="{% url "my_image" %}">
Let's suppose you have a django project, the response of one of its views is an image, if you want to insert that image inside an HTML document you do:
<img src="/path_to_my_view/">
Then, when the user open the Webpage that return that HTML document he will see the image. Nothing new at this point.
Now, what happens if the response of that "view" is not an image but a simple text, there is no HTML tag to embed a simple text, so, how would you do it?
The view code that return a simple text is:
from django.http import HttpResponse
def view(request):
return HttpResponse('Simple line of text returned by my view')
So, what I would like to do in my html is something like this:
<p>The exit of the view was: <?HTML_TAG? src="/path_to_my_view/"></p>
Or something like this (using Django template language):
<p>The exit of the view was: {{ MyProjectApp.view }}</p>
But I have not found any one of those alternatives yet.
The current solution to the problem is by using the object HTML tag:
<object width="400" height="400" data="/path_to_my_view/"></object>
However I'm not happy with this solution, it seems like using a cannon to kill a bug, I think that I'm missing something.
Thank you for your support!
I think you are looking for an <iframe>, which is the way of embedding one HTML document inside the other:
<iframe src='/path_to_my_view/'>
However since these are both views on the same server, I don't know why you want to embed it via HTML. Why not simply call the view from your other view and pass the result as a context variable? Or, even better, factor out the code whose result you want into a common method, called by both views.
You have 2 choices here: either you use a TemplateView that you map directly to your url and in that template you add your html code.
The other solution is to interpret a string as a template and render it.
I found a solution to my question:
View.py
def myview(request):
content ='Hello I am an innocent string'
response = HttpResponse(content, content_type='text/plain')
return response
template.html
<object type="text/plain" data="/myapp/"></object>
Hope it helps someone else!
Bye!