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

  • Home
  • SEARCH
  • 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 9258939
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T12:32:01+00:00 2026-06-18T12:32:01+00:00

I’m using WTforms with Jinja2 and want to change my templates page title depending

  • 0

I’m using WTforms with Jinja2 and want to change my templates page title depending on whether I am creating a new instance of editing an existing form object.

This is what I wrote in the template:

{% block title %}{% if form.obj %}Edit{% else %}New{% endif %} Post{% endblock %}

What I expect to see:

if the form is filled out I expect to see “Edit Post” in the page title.

if the form is empty I expect to see “New Post” in the page title.

What I get: “New Post” in both instances.

Here is my PostHandler that is passing the form values.

def with_post(fun):
    def decorate(self, post_id=None):
        post = None
        if post_id:
            post = models.BlogPost.get_by_id(int(post_id))
            if not post:
                self.error(404)
                return
        fun(self, post)
    return decorate

class PostHandler(BaseHandler):
    def render_form(self, form):
        self.render_to_response("edit.html", {'form': form})

    @with_post
    def get(self, post):
        self.render_form(MyForm(obj=post))

    @with_post
    def post(self, post):
        form = MyForm(formdata=self.request.POST, obj=post)
        if post and form.validate():
            form.populate_obj(post)
            post.put()
            post.publish()
            self.render_to_response("published.html", {'post': post})
        elif self.request.POST and form.validate():
            post = models.BlogPost()
            post.title = form.title.data
            post.body = form.body.data
            post.tags = form.tags.data
            post.publish()
            self.render_to_response("published.html", {'post': post})
        else:
            self.render_to_response('edit.html', {'form':form})

In short, all I’m trying to do is test whether the form is filled, and change my page title “New Post” or “Edit Post” accordingly.

  • 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-06-18T12:32:02+00:00Added an answer on June 18, 2026 at 12:32 pm

    While Form accepts a obj argument, it doesn’t actually store it, it just uses that obj to fill in any blanks that formdata didn’t provide. So when you ask Jinja2 {% if form.obj %} it’s always going to be False, because there is never a obj property (unless you have a field that happens to be called obj of course).

    If you’re editing a post, you’ll have an id to work with so you know which post to update in the database, so where are you currently storing that? Assuming you store it as a hidden field, you could just do:

    {% if form.id.data == None %}Must be a New form {% endif %}
    

    If you wanted to check if the entire form was empty, you could access the form.data dictionary, and make sure all the entries are None, although you need to be careful, because I know that FileField returns a u'None' instead of a real None, so you’d have to double check what Fields you care about.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am using JSon response to parse title,date content and thumbnail images and place
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I want use html5's new tag to play a wav file (currently only supported
I'm making a simple page using Google Maps API 3. My first. One marker
Let's say I'm outputting a post title and in our database, it's Hello Y’all
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I want to count how many characters a certain string has in PHP, but
I am trying to find ID3V2 tags from MP3 file using jid3lib in Java.
I am using the SimpleRSS gem to parse a WordPress RSS feed. The only

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.