Djano if statement help

30 views
Skip to first unread message

HarryBoy

unread,
Mar 16, 2012, 10:00:44 AM3/16/12
to Django users
I have the following:

<div class="StackRow_Edit_Right">
<input type="radio" id="StackRow1Input"
name="EnableLogUpload" value="true" {% if EnableLogUpload == '1' %}
checked="checked" {% endif %} />Enabled<br />
<input type="radio" id="StackRow1Input"
name="EnableLogUpload" value="false" {% if EnableLogUpload == '0' %}
checked="checked" {% endif %} />Disabled
</div>

Then the following if statement:
<div id="EnableLogUpload" class="StackRow_Middle">{% if
EnableLogUpload == '1' %} Enabled {% else %} Disabled {% endif %} </
div>

The above code works as expected (If EnableLogUpload equals to true
then 'Enabled' is displayed else 'Disabled' is displayed.

But I want to change the 'value' of the radio buttons to "Enabled" and
"Disabled" instead of "true" and "false" like so:
<div class="StackRow_Edit_Right">
<input type="radio" id="StackRow1Input"
name="EnableLogUpload" value="Enabled" {% if EnableLogUpload == '1'
%} checked="checked" {% endif %} />Enabled<br />
<input type="radio" id="StackRow1Input"
name="EnableLogUpload" value="Disabled" {% if EnableLogUpload == '0'
%} checked="checked" {% endif %} />Disabled
</div>

But I can't get the django if statement to work:
e.g.
<div id="EnableLogUpload" class="StackRow_Middle">{% if
EnableLogUpload == "Enabled" %} Enabled {% else %} Disabled {% endif
%} </div>

This always prints out 'Disabled' even when the EnableLogUpload is
"Enabled".

Am I doing something incorrect??


Joel Goldstick

unread,
Mar 16, 2012, 11:40:19 AM3/16/12
to django...@googlegroups.com
On Fri, Mar 16, 2012 at 6:00 AM, HarryBoy <ryanj...@gmail.com> wrote:
> I have the following:
>
>               <div class="StackRow_Edit_Right">
>                    <input type="radio" id="StackRow1Input"
> name="EnableLogUpload" value="true"  {% if EnableLogUpload == '1' %}
> checked="checked" {% endif %} />Enabled<br />
>                    <input type="radio" id="StackRow1Input"
> name="EnableLogUpload" value="false" {% if EnableLogUpload == '0' %}
> checked="checked" {% endif %}  />Disabled
>                </div>
>
> Then the following if statement:
> <div id="EnableLogUpload" class="StackRow_Middle">{% if
> EnableLogUpload == '1' %} Enabled  {% else %} Disabled {% endif %} </
> div>
>
> The above code works as expected (If EnableLogUpload equals to true
> then 'Enabled' is displayed else 'Disabled' is displayed.
>
> But I want to change the 'value' of the radio buttons to "Enabled" and
> "Disabled" instead of "true" and "false" like so:
>   <div class="StackRow_Edit_Right">
>                    <input type="radio" id="StackRow1Input"
{# print it here to see what it is #}{{EnableLogUpload}}

> name="EnableLogUpload" value="Enabled"  {% if EnableLogUpload == '1'
> %} checked="checked" {% endif %} />Enabled<br />
>                    <input type="radio" id="StackRow1Input"
> name="EnableLogUpload" value="Disabled" {% if EnableLogUpload == '0'
> %} checked="checked" {% endif %}  />Disabled
>                </div>
>
> But I can't get the  django if statement to work:
> e.g.
> <div id="EnableLogUpload" class="StackRow_Middle">{% if
> EnableLogUpload == "Enabled"  %} Enabled  {% else %} Disabled {% endif
> %} </div>
>
> This always prints out 'Disabled' even when the EnableLogUpload is
> "Enabled".
>
> Am I doing something incorrect??
>
Try printing the value before you do the test to see if it is what you
think it is

>
> --
> You received this message because you are subscribed to the Google Groups "Django users" group.
> To post to this group, send email to django...@googlegroups.com.
> To unsubscribe from this group, send email to django-users...@googlegroups.com.
> For more options, visit this group at http://groups.google.com/group/django-users?hl=en.
>

--
Joel Goldstick

HarryBoy

unread,
Mar 16, 2012, 1:01:19 PM3/16/12
to django...@googlegroups.com
Hi, thanks for the reply.

I printed the value and its actually 0 every time.

Why is it 0 when I set it to EnableLogUpload == "Enabled"??

Thanks

Tom Evans

unread,
Mar 16, 2012, 1:09:59 PM3/16/12
to django...@googlegroups.com

'==' tests equality
'=' assigns a rvalue

Cheers

Tom

HarryBoy

unread,
Mar 16, 2012, 1:25:04 PM3/16/12
to django...@googlegroups.com

Sorry the '==' was a typo, its actually:

EnableLogUpload = "Enabled"

HarryBoy

unread,
Mar 16, 2012, 2:26:00 PM3/16/12
to django...@googlegroups.com
Anyone?????????

Joel Goldstick

unread,
Mar 16, 2012, 2:26:57 PM3/16/12
to django...@googlegroups.com
On Fri, Mar 16, 2012 at 9:25 AM, HarryBoy <ryanj...@gmail.com> wrote:
>> Sorry the '==' was a typo, its actually:
>>
>> EnableLogUpload = "Enabled"
>
> --
> You received this message because you are subscribed to the Google Groups
> "Django users" group.
> To view this discussion on the web visit
> https://groups.google.com/d/msg/django-users/-/Tu0wfrtAPJUJ.

>
> To post to this group, send email to django...@googlegroups.com.
> To unsubscribe from this group, send email to
> django-users...@googlegroups.com.
> For more options, visit this group at
> http://groups.google.com/group/django-users?hl=en.

on top you check if EnableLogUpload = '1' Later on you check if it = 'Enabled'


--
Joel Goldstick

HarryBoy

unread,
Mar 16, 2012, 2:47:22 PM3/16/12
to django...@googlegroups.com

If I set it to "Enabled" and then check if it is indeed {% if EnableLogUpload == "Enabled" %} it does not enter the if statement.


What am I doing wrong??

J. Cliff Dyer

unread,
Mar 16, 2012, 4:17:26 PM3/16/12
to django...@googlegroups.com
Well the first suspicious thing was that you were testing to see if it
was equal to the string "0". That tells me you don't understand how
equality works in python. Python is strongly typed. Strings and
integers are not the same thing, and will never be equal to one another.
Without more information, we can't really tell what's going on.

Also, if you are expecting the string "Enabled" or "Disabled" to be
passed in to the template, you are overcomplicating things by saying

{% if EnableLogUpload == "Enabled" %}Enabled{% else %}Disabled{% endif
%}

You could just say

{{ EnableLogUpload }}

But none of that will solve your problem, because EnableLogUpload is
clearly not set to the string "Enabled". I can say for sure that you're
passing it in wrong from the view, but I can't say how without seeing
your view code. Please share that as well.

If I were doing it from scratch, I would just make a the EnableLogUpload
variable a boolean, and worry about how I want to render it in the
template. I would also use standard python naming conventions (because
the most important task of code is to communicate with other
programmers, and only secondarily to make a computer do things). The
simplified version would look like this:

=== views.py ===

def view(request):
context = {'enable_log_upload': True}
return render('template.html', context)

=== end views.py ===

=== template.html ===

{% if enable_log_upload %}enabled{% else %}disabled{% endif %}

=== end template.html ===

Note that I'm not trying to test if enable_log_upload is equal to
anything in particular. I just want to know if it's a true value or
not.

> --
> You received this message because you are subscribed to the Google
> Groups "Django users" group.
> To view this discussion on the web visit

> https://groups.google.com/d/msg/django-users/-/x6xX1B-NbYcJ.


> To post to this group, send email to django...@googlegroups.com.
> To unsubscribe from this group, send email to django-users

> +unsub...@googlegroups.com.

Reply all
Reply to author
Forward
0 new messages