I have an application that can upload files in every format I want to be encrypted and saved to database I also created decrypt functionality but I dont know how to show this decrypted file to the user or django templates.
i have tried many ways like TemFile library but still I cant pass my decrypted data to the templates
here is my code for encryption
def encrypt (request):
if request.method == 'POST':
upload_file = request.FILES.get("like")
# key generation
key = Fernet.generate_key()
encode_key = urlsafe_base64_encode(force_bytes(key))
print(encode_key)
document = FileEncrypt.objects.create(document = upload_file,key=encode_key)
# using the generated key
fernet = Fernet(key)
# opening the original file to encrypt
with open(document.document.path, 'rb') as file:
original = file.read()
# encrypting the file
encrypted = fernet.encrypt(original)
with open(document.document.path, 'wb') as encrypted_file:
encrypted_file.write(encrypted)
document.save()
return redirect("decrypt_and_show", id=document.id)
else:
return render(request,"index.html",{
})
after redirecting to decrypt page it's not showing the file
here is my decrypt def
def decrypt(request,id):
document = FileEncrypt.objects.get(pk=id)
decode_key =force_text(urlsafe_base64_decode(document.key))
print(decode_key)
fernet = Fernet(decode_key)
# opening the encrypted file
with open(document.document.path, 'rb') as enc_file:
encrypted = enc_file.read()
# decrypting the file
decrypted = fernet.decrypt(encrypted)
tmpfile = tempfile.NamedTemporaryFile(delete=False,suffix='.JPEG')
tmpfile.seek(0)
tmpfile.write(decrypted)
return render(request,"index2.html",{
"d" : tmpfile.name
})
in my template is
{% load static %}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Document</title>
<script src="{% static 'fileuploads/js/script.js' %}" defer></script>
</head>
<body>
<img src="{{d}}" />
</body>
</html>
in my index template is
{% load static %}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Document</title>
<script src="{% static 'fileuploads/js/script.js' %}" defer></script>
</head>
<body>
<form action"" method="POST" enctype="multipart/form-data">
{% csrf_token %}
<input type="file" name="like" />
{% comment %} {{form}} {% endcomment %}
<button type="submit">Upload</button>
</form>
</body>
</html>
could you please help me with.
Related
The error that occurs during template rendering:
In template /Users/mac/myfirstproject/templates/base_layout.html, error at line 12
Reverse for 'list' not found. 'list' is not a valid view function or pattern name.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>ARTICLES</title>
<link rel="stylesheet" href="{% static 'styles.css' %}">
</head>
<body>
<div class="wrapper">
<h1><img src="{% static 'logo.png' %} "/></h1>
{% block content %}
{% endblock %}
</div>>
</body>
</html>
As indicated in Andrei's comment above, you need to make sure you have 'list' set up as a valid url name in your urls.py file.
something like this:
urls.py
from django.conf.urls import url
from .views import list_view
urlpatterns = (
url(r'^list$', list_view, name='list'),
)
in order to use the {% url 'url_name' %} template tag, you have to have a url named url_name, and if you have your project divided into apps, the format is:
`{% url 'app_name:url_name' %}. If you post your urls.py file, perhaps we can be more helpful.
I want to put an html in a string, NOT a file, like:
t = """
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>foo</title>
</head>
<body>
{{ script|safe }}
</body>
</html>
"""
script = ...
render_template_string(t, script=script)
However when I go to the actual webpage, I don't see an html page with a script, I see garbage:
"\n<!doctype html>\n<html lang=\"en\">\n<head>\n <meta charset=\"utf-8\">\n <title>foo</title>\n</head>\n\n<body>\n \n<script\n src=\"http://localhost:5006/bkapp/autoload.js?bokeh-autoload-element=ff639949-97c5-40b8-9cc2-28b78be802d1&bokeh-app-path=/bkapp&bokeh-absolute-url=http://localhost:5006/bkapp\"\n id=\"ff639949-97c5-40b8-9cc2-28b78be802d1\"\n data-bokeh-model-id=\"\"\n data-bokeh-doc-id=\"\"\n></script>\n</body>\n</html>"
How do I fix this?
EDIT: even a very simple
t = '{{ script|safe }}'
does not work, I get, in the browser:
"\n<script\n src=\"http://localhost:5006/bkapp/autoload.js?bokeh-autoload-element=07a43696-66af-4e54-a4e3-9a09ac242e38&bokeh-app-path=/bkapp&bokeh-absolute-url=http://localhost:5006/bkapp\"\n id=\"07a43696-66af-4e54-a4e3-9a09ac242e38\"\n data-bokeh-model-id=\"\"\n data-bokeh-doc-id=\"\"\n></script>"
EDIT 2: I have also tried
t = '{% autoescape false %}{{ script|safe }}{% endautoescape %}'
but that didn't work either.
I'm using Connexion if that matters.
I am using linux and created a Django project.
I have folder in my home directory /home/simha/siteimages
i am trying to show a list of images from the above directory
In views.py
def gallery(request):
path="/home/simha/siteimages"
img_list =os.listdir(path)
return render(request,'blog/gallery.html', {'images': img_list})
gallery.html is
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
{% for image in images %}
<img src='/static/{{image}}' />
{% endfor %}
</body>
</html>
Its not working.
It says at img_list =os.listdir(path) error No such file or directory
I'm writing my first Django app by following along with this book:
http://chimera.labs.oreilly.com/books/1234000000754/ch05.html#_passing_python_variables_to_be_rendered_in_the_template
In the book there is a test that is verifying that the html is being returned as it is supposed to. Here is the test:
def test_home_page_returns_correct_html(self):
request = HttpRequest()
response = home_page(request)
expected_html = render_to_string('home.html')
print(expected_html)
print(response.content.decode())
self.assertEqual(response.content.decode(), expected_html)
My test is failing on the assertEqual test because I have added a csrf token in my HTML using the Django Template Language. Here is what my HTML page looks like:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>To-Do lists</title>
</head>
<body>
<h1>Your To-Do list</h1>
<form method="POST">
<input name="item_text" id="id_new_item" placeholder="Enter a to-do item"/>
{% csrf_token %}
</form>
<table id="id_list_table">
<tr><td>{{ new_item_list }}</td></tr>
</table>
</body>
</html>
My assert is failing due to the render_to_string method not including the token. Here is what my two print statements included in my test print out:
F<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>To-Do lists</title>
</head>
<body>
<h1>Your To-Do list</h1>
<form method="POST">
<input name="item_text" id="id_new_item" placeholder="Enter a to-do item"/>
</form>
<table id="id_list_table">
<tr><td></td></tr>
</table>
</body>
</html>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>To-Do lists</title>
</head>
<body>
<h1>Your To-Do list</h1>
<form method="POST">
<input name="item_text" id="id_new_item" placeholder="Enter a to-do item"/>
<input type='hidden' name='csrfmiddlewaretoken' value='VAiGvXZLHCjxWEWdjhgQRBwBSnMVoIWR' />
</form>
<table id="id_list_table">
<tr><td></td></tr>
</table>
</body>
</html>
F.
He doesn't have this problem in the book (he's using 1.8), so I was wondering if the method behavior has changed, or how I would write this test to pass.
The request argument was added to render_to_string in Django 1.8. You could try changing the line in your test to:
expected_html = render_to_string('home.html', request=request)
It's only required to make this change in Django 1.9+, the test passes without the request in Django 1.8.
I found this solution which has worked for the latest Django version - 3.0.6
#add a function to the post request test function
def remove_csrf_tag(text):
"""Remove csrf tag from TEXT"""
return re.sub(r'<[^>]*csrfmiddlewaretoken[^>]*>', '', text)
...
# then change assertion
def test_home_page_can_save_a_POST_request(self):
...
self.assertEqual(
remove_csrf_tag(response.content),
remove_csrf_tag(expected_html)
)
<html>
<head>
<title>{{ songname }}</title>
<meta charset="UTF-8">
{% load static %}
</head>
<body>
<center><h1>MUOSIC</h1></center>
<hr>
<audio controls>
<source src="{% static {{ songname }} %}" type="audio/mpeg">
</audio>
<hr>
</body>
Here songname is the name of the song which i want to play.All the static files are in the static directory.From the view function the above template is called using render_to_response function.So please can anyone explain the reason for this problem.
You can't nest template variables like that. Assuming that your songname is the actual name of the file and the file is in the root of your STATIC_ROOT, you simply do:
{% static songname %}