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 6359511
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T23:31:41+00:00 2026-05-24T23:31:41+00:00

How can i add pass Model-Specific urls to the Template. Let’s say, i want

  • 0

How can i add pass Model-Specific urls to the Template.
Let’s say, i want to build an edit-link.
I would guess, using the uri_for() function would be an easy approach.

But the following gives me “UndefinedError: ‘webapp2’ is undefined”

{% webapp2.uri_for("editGreeting", greeting.key().id()) %}

Or should i prepare these in the MainPage-Request-Handler?
If so, i don’t know how to add them to each greeting.

The following Code-Example is taken from:
http://webapp-improved.appspot.com/tutorials/gettingstarted/templates.html

Controller/Handler

class MainPage(webapp2.RequestHandler):
    def get(self):
        guestbook_name=self.request.get('guestbook_name')
        greetings_query = Greeting.all().ancestor(
            guestbook_key(guestbook_name)).order('-date')
        greetings = greetings_query.fetch(10)

        if users.get_current_user():
            url = users.create_logout_url(self.request.uri)
            url_linktext = 'Logout'
        else:
            url = users.create_login_url(self.request.uri)
            url_linktext = 'Login'

        template_values = {
            'greetings': greetings,
            'url': url,
            'url_linktext': url_linktext,
        }

        path = os.path.join(os.path.dirname(__file__), 'index.html')
        self.response.out.write(template.render(path, template_values))

Template/View:

<html>
  <body>
    {% for greeting in greetings %}
      {% if greeting.author %}
        <b>{{ greeting.author.nickname }}</b> wrote:
      {% else %}
        An anonymous person wrote:
      {% endif %}
      <blockquote>{{ greeting.content|escape }}</blockquote>
    {% endfor %}

    <form action="/sign" method="post">
      <div><textarea name="content" rows="3" cols="60"></textarea></div>
      <div><input type="submit" value="Sign Guestbook"></div>
    </form>

    <a href="{{ url }}">{{ url_linktext }}</a>
  </body>
</html

The class BaseHandler is the class all handlers inherit from.
I tried the following as @moraes suggested.
I still get:

value = self.func(obj)
File "C:\Users\timme04\python\hellowebapp\handlers\basehandler.py", line 23, in jinja2
return jinja2.get_jinja2(factory=self.jinja2_factory)
File "C:\Users\timme04\python\hellowebapp\webapp2_extras\jinja2.py", line 212, in get_jinja2
jinja2 = app.registry[key] = factory(app)
TypeError: jinja2_factory() takes exactly 1 argument (2 given)

🙁

import webapp2

from webapp2_extras import jinja2

class BaseHandler(webapp2.RequestHandler):

    def jinja2_factory(app):
        j = jinja2.Jinja2(app)
        j.environment.filters.update({
            # Set filters.
            # ...
        })
        j.environment.globals.update({
            # Set global variables.
            'uri_for': webapp2.uri_for,
            # ...
        })
        return j

    @webapp2.cached_property
    def jinja2(self):
        # Returns a Jinja2 renderer cached in the app registry.
        return jinja2.get_jinja2(factory=self.jinja2_factory)

    def render_response(self, _template, **context):
        # Renders a template and writes the result to the response.
        rv = self.jinja2.render_template(_template, **context)
        self.response.write(rv)
  • 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-24T23:31:42+00:00Added an answer on May 24, 2026 at 11:31 pm

    You must set uri_for as a global variable. One way to do it is to set an initializer for global variables and filters:

    import webapp2
    from webapp2_extras import jinja2
    
    def jinja2_factory(app):
        j = jinja2.Jinja2(app)
        j.environment.filters.update({
            # Set filters.
            # ...
        })
        j.environment.globals.update({
            # Set global variables.
            'uri_for': webapp2.uri_for,
            # ...
        })
        return j
    
    class BaseHandler(webapp2.RequestHandler):
    
        @webapp2.cached_property
        def jinja2(self):
            # Returns a Jinja2 renderer cached in the app registry.
            return jinja2.get_jinja2(factory=jinja2_factory)
    
        def render_response(self, _template, **context):
            # Renders a template and writes the result to the response.
            rv = self.jinja2.render_template(_template, **context)
            self.response.write(rv)
    

    Edit: changed example to use a RequestHandler.

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

Sidebar

Related Questions

Users on my site can add nodes of a custom type (let's call it
I want to add Pagination to one of my views using MvcPager3. This would
How can I add a new object item to a Main Views model that
My model consists of a single main object that the user can add various
Why can't I pass in html attributes to EditorFor() ? eg; <%= Html.EditorFor(model =>
I can add and remove the last line in my dynamic form and calculate
I can add a normal rightBarButton to my navigation without a problem but now
I can add a Conditional statement to a breakpoint, for instance arg0.startsWith(something) but i'd
We can add an image to an photo album using UIImage *img = [UIImage
Can someone please direct me on how I can add something similar to inputAccessoryView

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.