1

So in my template, I have the following code:

<span class="state-txt">{{ state }}</span>

In my views.py, it's handled with the following if/else loop:

if user is not None:
            if user.is_active:
                login(request, user)            
                state = "You're successfully logged in!"
                return render_to_response('uc/portal/index.html', {'state':state, 'username':username}, context_instance=RequestContext(request))
            else:
                state = "Your account is not active, please contact UC admin."
        else:
            state = "Your username and/or password were incorrect."

Essentially, it's working fine at the moment but I want each state to be able to contain different <img> tags, but when I just type state = "<img src="some.jpg"> Your username and/or password were incorrect." The html doesn't render correctly. Is there some way to do what I'm trying to do in Django, or am I barking up the wrong tree?

4
  • 1
    I think you should look into the messages framework instead of using 'state'. That way, you might be able to write styles for ERROR, INFO, etc instead of adding images to your variables. Commented Aug 8, 2013 at 20:57
  • That's actually a really good idea - I love the simplicity of @karthikr's answer below - any reason to suspect his implementation isn't good enough? Commented Aug 8, 2013 at 21:12
  • 1
    Thanks. No, his answer's just as good. it's just another approach. :) Commented Aug 8, 2013 at 21:27
  • 1
    I'm going to be looking into the messages framework, so definitely appreciate your input as well! Cheers! Commented Aug 8, 2013 at 21:31

3 Answers 3

2

I would just pass the image URL in the context from the view, and consume that in the template. Something like this:

if user:
    if user.is_active:
        login(request, user)            
        state = "You're successfully logged in!"
        state_img = success_image_url
        return render_to_response('uc/portal/index.html', 
                 {'state': state, 
                  'state_img': state_img, 
                  'username':username
                 }, context_instance=RequestContext(request))
    else:
        state_img = inactive_image_url
        state = "Your account is not active, please contact UC admin."
else:
    state_img = invalid_credentials_url
    state = "Your username and/or password were incorrect."

and in the template

<span class="state-txt">
    <img src="{{state_img}}" />{{ state }}
</span>
Sign up to request clarification or add additional context in comments.

1 Comment

Oh wow yeah this seems really obvious - especially coming out of php I should have thought of this immediately!! Thank you!
1

For completeness' sake, as karthikr already posted an excellent solution:

The reason the html doesn't render correctly, is because the Django template language automatically assumes that all output by {{ ... }} is not safe, all symbols that have a special meaning in HTML will be escaped (< becomes &lt; etc.).

To render a string as pure HTML code, use the safe filter.

views.py:

state = "<img src="some.jpg" /> Your username and/or password were incorrect."

index.html:

<span class="state-txt">{{ state|safe }}</span>

1 Comment

Gorgeous, thank you so much for explaining this. I'm really looking forward to getting the hang of Django !
0

Don't render the image. try if else

view.py

if user is not None:
        if user.is_active:
            login(request, user)            
            state = True
            return render_to_response('uc/portal/index.html', {'state':state, 'username':username}, context_instance=RequestContext(request))
        else:
            state = False
    else:
        state = False

in template

{%if state %}  
   <img></img>
   you are successfully logged in.
{%endif%}

1 Comment

I am using the state to give specific login error messages, so your solution isn't quite what I am looking for. However thanks for the tip!

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.