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

The Archive Base Latest Questions

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

When I am adding a static view like this: cfg = config.Configurator(…) cfg.add_static_view(name=’static’, path=’MyPgk:static’)

  • 0

When I am adding a static view like this:

cfg = config.Configurator(...)
cfg.add_static_view(name='static', path='MyPgk:static')

# And I want to add a view for 'favicon.ico'.
cfg.add_route(name='favicon', pattern='/favicon.ico')
cfg.add_view(route_name='favicon', view='MyPgk.views.mymodule.favicon_view')

I am trying to add that favicon.ico annoying default path of /favicon.ico called by the browser if it’s undefined in the webpage. I would like to use the example at http://docs.pylonsproject.org/projects/pyramid_cookbook/en/latest/files.html and modify it to have:

def favicon_view(request, cache=dict()):
    if (not cache):
        _path_to_MyPkg_static = __WHAT_GOES_HERE__
        _icon = open(os.path.join(_path_to_MyPkg_static, 'favicon.ico')).read()
        cache['response'] = Response(content_type='image/x-icon', body=_icon)
    return cache['response']

Since, I can’t really define the _here proposed in the example, how can I make it dependent to request to get the actual full path at runtime? Or do I really have to deal with:

_here = os.path.dirname(__file__)
_path_to_MyPkg_static = os.path.join(os.path.dirname(_here), 'static')

and having to be careful when I decide to refactor and put the view in another pkg or subpackage, or where-ever?

Something equivalent to request.static_path() but instead of getting the url path, to actually get a directory path:

request.static_file_path('static') -> /path/to/site-packages/MyPkg/static

Thanks,

  • 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-29T23:20:15+00:00Added an answer on May 29, 2026 at 11:20 pm

    You can use the pkg_resources module to make paths that are relative to Python modules (and thus, independent of the module that retrieves them). For example:

    import pkg_resources
    print pkg_resources.resource_filename('os.path', 'static/favicon.ico')
    # 'C:\\Python27\\lib\\static\\favicon.ico'
    

    Just substitute os.path with whatever module that is the parent of your static files.

    EDIT: If you need to remember that 'static' route mapped to 'MyPkg:static', then the easiest way is to save it in some dictionary in the first place:

    STATIC_ROUTES = {'static': 'MyPkg:static'}
    for name, path in STATIC_ROUTES.iteritems():
        cfg.add_static_view(name=name, path=path)
    

    and then simply retrieve the path:

    static_path = STATIC_ROUTES['static']
    package, relative_path = static_path.split(':')
    icon_path = pkg_resources.resource_filename(
        package, os.path.join(relative_path, 'favicon.ico'))
    

    If that’s impossible, though (e.g. you don’t have access to the cfg object), you can retrieve this path, it’s just quite painful. Here’s a sample function that uses undocumented calls (and so may change in future Pyramid versions) and ignores some additional settings (like route_prefix configuration variable):

    def get_static_path(request, name):
        from pyramid.config.views import StaticURLInfo
        registrations = StaticURLInfo()._get_registrations(request.registry)
        if not name.endswith('/'):
            name = name + '/'
        route_name = '__%s' % name
        for _url, spec, reg_route_name in registrations:
            print ':', reg_route_name
            if reg_route_name == route_name:
                return spec
    

    In your case, it should work like this:

    >>> get_static_path(request, 'static')
    MyPkg:static/
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am adding simple data into a listview (name and time) and I want
I am adding input elements in a view, dynamically, using JavaScript. But I am
I am adding user defined text in a UItableView via a modal view controller.
Am working with django 1.3 and doing some template inheritance. My /static/ settings path
I have a view with adding element to collection: It adds me element to
in my app i have a table view and i am adding 4 image
i have a table view. And im adding two buttons to each cell: -
I am adding object exercise to object session (as a relationship). In a view,
in the magento wiki for adding static blocks, there is an option to select
I have a UITableView and I am adding custom view to the cell. It

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.