treecoder
treecoder

Reputation: 45081

Pyramid: How to specify the base URL for the application

Let's say my app is served at the domain www.example.com.

How (where?) should I specify this in the Pyramid configuration file so that functions like request.route_url would automatically pick it and generate the correct URL.

(I think [server:main] is not the place for this)

Upvotes: 2

Views: 1448

Answers (2)

Michael Merickel
Michael Merickel

Reputation: 23331

Yes, a proper reverse proxy will forward along the appropriate headers to your wsgi server. See the pyramid cookbook for an nginx recipe.

Upvotes: 1

treecoder
treecoder

Reputation: 45081

The url generation functions route_url, static_url, resource_url all depend on the WSGI environ dictionary from where they take all the essential parameters required to generate a full URL.

Hence one way to do it is to modify the WSGI environment dictionary at the request creation time, and modify the required parameters. Events are great for this kind of thing:

from pyramid.events import NewRequest
from pyramid.events import subscriber

@subscriber(NewRequest)
def mysubscriber(event):
    event.request.environ['HTTP_HOST'] = 'example.com'

After this, route_url will take example.com as the base URL.

Upvotes: 4

Related Questions