views:

141

answers:

4

Hello,

I would like to generate an HttpResponse that contains a certain string. For each of the characters in the string I have a background color I want to use.

For simplification, let's assume I can only have shades of green in the background, and that the "background colors" data represents "level of brightness" in the green domain.

For example, a response may be 'abcd', and my "background colors" data may be:

[0.0, 1.0, 0.5, 1.0]

This would mean the first character 'a' needs to have a background of dark green (e.g. 004000), the second character 'b' needs to have a background of bright green (e.g. 00ff00), the third character 'c' needs to have a "middle" brightness (e.g. 00A000), and so on.

I don't want to use a template but rather just return "plain text" response. Is that possible?

If not - what would be the simplest template I could use for that?

Thanks

+2  A: 

It could be something like this:

aString = 'abcd'
newString =''
colors= [0.0, 1.0, 0.5, 1.0]
for i in aString:
    newString = newString + '<span style="background-color: rgb(0,%s,0)">%s</span>'%(colors.pop(0)*255,i)



response = HttpResponse(newString)

untested

vikingosegundo
This worked for me, except a slight bugfix: the value used for color value (in the example colors.pop(0)) should be multiplied by 255 in the string argument.
Roee Adler
and i and colors.pop(0) where mixed up. But now i corrected it
vikingosegundo
+2  A: 

you can use something like this to generate html in the django view itself and return it as text/html

data = "abcd"
greenShades = [0.0, 1.0, 0.5, 1.0]

out = "<html>"
for d, clrG in zip(data,greenShades):
    out +=""" <div style="background-color:RGB(0,%s,0);color:white;">%s</div> """%(int(clrG*255), d)
out += "</html>"
Anurag Uniyal
+1  A: 

Your best bet here would be to use the span element, as well as a stylesheet. If you don't want to use a template, then you'd have to render this inline. An example:

string_data = 'asdf'
color_data = [0.0, 1.0, 0.5, 1.0]
response = []
for char, color in zip(string_data, color_data):
    response.append('<span style="background-color:rgb(0,%s,0);">%s</span>' % (color, char)
response = HttpResponse(''.join(response))

I'd imagine that this could also be done in a template if you wanted.

bluejeansummer
A: 

Hi Sorry that not i was looking for, have you got codes for 2 coloured page the template 1 colour and the background another colour please can you help ty...Susziewong