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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T20:49:13+00:00 2026-06-14T20:49:13+00:00

Is there a way to define URLs with optional URL params in Flask? Essentially,

  • 0

Is there a way to define URLs with optional URL params in Flask? Essentially, what I’d like to do is define rules that allow for optionally specified languages:

/
/de -> matches / (but doesn't collide with /profile)
/profile 
/de/profile

I think I’ve figured out a way to do it, but it involves either making a change to how Werkzeug and Flask handles the request (either monkey patching or forking the framework source). This seems like an overly complex way to deal with this problem though.. Is there an easier way to do this that I’m overlooking?

Edit:

Based on Brian’s answer, here’s what I came up with:

app.py:

from loc import l10n

def create_app(config):                                                                                                                 
    app = Flask(__name__)                                                                                                               
    app.config.from_pyfile(config)                                                                                                      

    bp = l10n.Blueprint()                                                                                                               
    bp.add_url_rule('/', 'home', lambda lang_code: lang_code)                                                                           
    bp.add_url_rule('/profile', 'profile', lambda lang_code: 'profile: %s' % 
        lang_code)
    bp.register_app(app)                                                                                                                

    return app  

if __name__ == '__main__':
    create_app('dev.cfg').run()

loc/l10ln.py

class Blueprint(Blueprint_):
    def __init__(self):
        Blueprint_.__init__(self, 'loc', __name__)

    def register_app(self, app):
        app.register_blueprint(self, url_defaults={'lang_code': 'en'})                                                                  
        app.register_blueprint(self, url_prefix='/<lang_code>')

        self.app = app

(I haven’t gotten to pulling lang_code from the variable list yet, but will be doing that shortly)

Now that’s just hot imho.

  • 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-14T20:49:14+00:00Added an answer on June 14, 2026 at 8:49 pm

    Blueprints might work for this, since they can be registered multiple times.

    from flask import Flask, Blueprint
    
    app = Flask(__name__)
    bp = Blueprint('main', __name__)
    
    @bp.route('/')
    def hello(lang):
        return 'Hello ' + lang + '!'
    
    app.register_blueprint(bp, url_defaults={'lang': 'en'})
    app.register_blueprint(bp, url_prefix='/<lang>')
    
    if __name__ == '__main__':
        app.run()
    

    If that works, see Internationalized Blueprint URLs in the Flask documentation for a way to avoid specifying a lang argument in every view function.

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

Sidebar

Related Questions

Is there a way to define custom codes (with messages/descriptions and whatnot) so that
Is there any way to define a variable that can be used in multiple
Is there a way to define regions in CSS file just like regions in
Is there some way to define an abstract type as a parameter in an
Is there a way to define a custom template for the no results view
is there any way to define objects in hibernate.cfg.xml by scope and not one
Is there a way to define an enum in AS3 in a way we
Question Is there a way to define a method only once in C# (in
Is there a concise way to define properties in a ViewModel for data binding
Hi All is there any way to locally define a variable in a function

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.