Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6110203
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T14:29:20+00:00 2026-05-23T14:29:20+00:00

I am pretty new to django, but have many years experience coding in the

  • 0

I am pretty new to django, but have many years experience coding in the java world, so I feel ridiculous asking this question – I am sure the answer is obvious and I am just missing it. I can’t seem to find the right way to query this in google or something… I have searched through the django docs and it either isn’t there or I am just not seeing it. All I want to do is in a template test if the var is not null OR an empty string OR just a bunch of spaces. I have an issue where spaces are getting introduced into my field – another issue I have to, and will, work out… but, I want my logic to work regardless. Right now, because my string contains just spaces simply doing this: {% if lesson.assignment %} always passes even though I don’t want it to. I have looked for a trim type functionality that would work between {% %}, but I can’t seem to find anything. I have tried strip, but it doesn’t work between {% %}. Can someone please point me in the direction of the answer… some docs I might have missed… something?

Thanks a ton in advance!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T14:29:21+00:00Added an answer on May 23, 2026 at 2:29 pm
    {% if lesson.assignment and lesson.assignment.strip %}
    

    The .strip calls str.strip() so you can handle whitespace-only strings as empty, while the preceding check makes sure we weed out None first (which would not have the .strip() method)

    Proof that it works (in ./manage.py shell):

    >>> import django
    >>> from django.template import Template, Context
    >>> t = Template("{% if x and x.strip %}OK{% else %}Empty{% endif %}")
    >>> t.render(Context({"x": "ola"}))
    u'OK'
    >>> t.render(Context({"x": "   "}))
    u'Empty'
    >>> t.render(Context({"x": ""}))
    u'Empty'
    >>> t.render(Context({"x": None}))
    u'Empty'
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am pretty new to django but experienced in Python and java web programming
Pretty new to Java programming, but I'm having trouble getting this to run. I
Im pretty new to Java Web Services, but I cant find a good explanation
I am pretty new to php, but I am learning! I have a simple
I'm pretty new with Django and the whole web-developing concept. I've only taken Java
I am using django and as I am pretty new I have some questions.
We are starting a web project with Django (and are pretty new to this),
I'm pretty new to the idea of recursion and this is actually my first
I'm pretty new to JSF and the many many related technologies out there. I'm
I'm pretty new to regular expressions. I have a requirement to replace spaces in

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.