-1

Before I start please pardon my english, totally newbie in HTML and this is the very first django app I'm creating.

So let's say I want to view static images based on the input in the forms for testing purpose, so if I type in the form goat.jpg it will display goat.jpg

this is my html

<!DOCTYPE html>

{% load static %}

<html>

<head>
    <title>test</title>
</head>

<body>
    <center><img src="{% static "{{staticpath}}" %}" alt="gif" align="middle"/></center>
    {{boldmessage}}

and this is my views

from django.shortcuts import render
from django.http import HttpResponse
from django.template import RequestContext
from django.shortcuts import render_to_response

def generate(request):
    context = RequestContext(request)
    if request.GET:
        path = request.GET.get('path','')
    context_dict = {'staticpath':path}
    return render_to_response("generated/generated.html", context_dict, context)

path is already a string, but if I remove the staticpath double quote django will raise an exception. So how do I exactly put the path's string in the html image source so it will display the static images correctly? thanks!

1 Answer 1

0

Calling {% static 'some/path/' %} will call the Django static file finders. If the image you are looking for is in a directory and you only pass the file name to {% static 'some/path' %} it will not find the file.

Read about the {{ STATIC_URL }} tag as solution that will avoid nesting tags. If your files are all stored in the root folder of your staticfiles directory (set by the STATIC_ROOT setting in settings.py) then this will work- <img src="{{ STATIC_URL }}{{ staticpath }}"/> However, it would probably be best to implement a function called by your view that SEARCHES your STATIC_ROOT folder, and all child folders, to find the full relative ( to STATIC_ROOT ) path for the file and returns it. The approach is convoluted but would work.

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.