Direct URL to a file in Django Template - python

I have a CSV file that users downloaded online and saved in a directory on my app. I am trying to create a link to this file so that the user can click the link and the file can be downloaded automatically to their computer. The code below keeps telling file not found. Please does anyone know how to solve this?
{% extends 'base.html' %}
{% block main %}
<p> <a href="/kaggle_dataset/emmy/IRIS.csv" download> Download File </a> </p>
{% endblock main %}

Thanks Joran Beasley for your assitance. I changed the directory the file was being served to static and everythong works find now.

Related

I cannot get images to work on my django project(this is my first one and im having trouble)

I have an img folder in the same directory as my html files. I want to put images onto my site but I am doing something wrong.
this is the code:
{% block a %}
<div class="row">
<div class="col-sm-4 center">
<img src="img/img01.jpeg">
</div>
</div>
{% endblock %}
this is what happens when I run the local server.
this is my file directory
Websites generally need to serve additional files such as images, JavaScript, or CSS. In Django, we refer to these files as “static files”.
Those files need to be handled another way as your templates. The documentation can be found here.
It comes basically down to defining a seperate static directory inside your app. It is really good explained in the documentation.

Django Allauth: How to customize confirmation email subject?

I was able to customize the confirmation email HTML template by adding this file into the templates folder:
templates/account/email/email_confirmation_signup_message.html
Now I'm trying to customize the subject of the email by adding the text I want inside this file:
templates/account/email/email_confirmation_signup_subject.txt
But it doesn't seem to do anything, I still get the default subject all the time.
Does anyone know what I'm doing wrong?
Many thanks!
You have to add this two files...
first:
account/email/email_confirmation_signup_message.html
{% include "account/email/email_confirmation_message.html" %}
second:
account/email/email_confirmation_message.html
The template that you want.
and finally delete the two .txt file:
account/email/email_confirmation_signup_message.txt
account/email/email_confirmation_message.txt
pd: if you copy the folder template/account from your virtual enviroment you have to delete the files over there as well.
Do in this way...
install django-mail-templated
base template file code...
{{ TAG_START_SUBJECT }}
{% autoescape off %}
{% block subject %}
{% endblock %}
{% endautoescape %}
{{ TAG_END_SUBJECT }}
main template file code...
{% block subject %}
Hello User..
{% endblock %}
Change file name to this:-
templates/account/email/email_confirmation_subject.txt
and inside write
{% load i18n %}
{% autoescape off %}
{% blocktrans %}Please Confirm Your E-mail Address or do whatever..{% endblocktrans %}
{% endautoescape %}
Maybe the problem comes with your urls.py file
You need to indicate here the file you use to customize the subject of your email.
PasswordResetView.as_view(template_name='email_confirmation_signup_message.html', subject_template_name='email_confirmation_signup_subject.txt')
For me the default behavior was to have the template in the the project folder and not in the particular apps directory api_project/templates/account/email/email_confirmation_subject.txt!
For anybody that might be here just because [example.com] still appears at the subject even when they customized successfully the templates/account/email/email_confirmation_subject.txt a variable that you need to configure and it's not quit obvious it's ACCOUNT_EMAIL_SUBJECT_PREFIX = '[example.com]'

Extending admin index.html template

I have downloaded the following templates from admin.contrib: base.html, base_site.html and index.html.
I would like to extend index.html, but no matter what I do it does not seem to work.
In settings.py, I have declared the name of my app to override the order in which the admin files are render. So, I can edit the actual files and content does change when I run the server.
But, I create a file in the directory templates/admin/foo.html.
The code with foo.html is:
{% extends "admin/index.html" %}
{% block something %}
<p>Hello</p>
{% endblock %}
And I have change the file templates/admin/index.html to have {% block something %}{% end block %}
When I run the server, the content of foo.html does not display.
Do I need to add foo.html to my urls.py and create a function within views.py to render the content?
Any help would be greatly appreciated.
Foo.html is not recognized by Django admin. If you want to customize admin/index.html, do your changes in admin/index.html.
If you want to use a separate file, include it inside the admin/index.html like this:
{% include "foo.html" %}
Hope it helps!

How to make a pdf link in Django

I'm extremely new to Django and I was hoping someone could help me add a link to my website which allows someone to download a pdf file. The pdf file is located here:
static/files/offline_reg_form.pdf
and I have no idea what the URL should be in the urls.py file or the view in the views.py file. I have looked around but nothing is working as I want it too.
Any help would be great, thanks!
For you specifically:
{% load static %}
my pdf
This will answer your questions.
https://docs.djangoproject.com/en/1.10/howto/static-files/
If it still confuses you,
{% load static %}
<img src="{% static "my_app/example.jpg" %}" alt="My image"/>
is an example of what should be in your template.

Playing audio through audio tag in a Django application

How do i use the audio tag of html 5 to play a audio file that has been uploaded in a my django based application.
models.py - Link
settings.py - Link
In my templates i am using the audio tag in the following way -
{% for file in audio %}
<li>
<h4>{{ file.title }}</h4>
<div id="name">{{ file.name }}</div>
<div id="play"><audio src="{{ file.audio_file }}" controls="controls">
Your browser does not support the audio element.
</audio></div>
</li>
{% endfor %}
But I am not able to play the file.
Based on your settings, you cannot play the file because:
{{ file.audio_file }} returns value saved in the database, which is absolute path to media file, e.g. http://localhost:8000/Users/test/django_app/media/test.wav
To solve this, you need to:
Edit your models.py
class AudioFile(models.Model):
audio_file = models.FileField(upload_to="music")
upload_to -- A path that will be appended to your MEDIA_ROOT setting to determine the value of the url attribute
In your template, use {{ file.audio_file.url }} to correctly reference the URL of the uploaded file
You may need to delete existing files, because wrong path was saved in the database.

Categories

Resources