How do I manipulate subdomains with Pyramid's url dispatch?
-
I can not seem to find any documentation on working with subdomains with the Pyramid framework. There is one question/answer on SO regarding this (http://stackoverflow.com/questions/5274813/routing-subdomains-in-pyramid) where they say "Theoretically, this is covered by add_route() with a pregenerator argument." I have also found in the docs (http://docs.pylonsproject.org/projects/pyramid/en/latest/api/interfaces.html) where they mention subdomains under the interface IRoutePregenerator __call__(request, elements, kw): " The pregenerator for a route is called bypyramid.request.Request.route_url() in order to adjust the set of arguments passed to it by the user for special purposes, such as Pylons âsubdomainâ support." But these are the only places that even mention subdomains and there is nothing on how to work with them. All I am wanting is to have a wildcard DNS accept any subdomain. If subdomain is user, route to a certain controller, if the subdomain is any other word, route to a controller and provide the subdomain as a variable. This is super easy with Flask: mod = Blueprint('users', __name__, subdomain='user') @mod.route('/') code or @mod.route('/<var2>', subdomain='<var1>') How can I/is it possible to achieve this with Pyramid?
-
Answer:
I needed to do the same and found the solution towards the end of the route_url documentation (http://docs.pylonsproject.org/projects/pyramid/en/latest/api/request.html#pyramid.request.Request.route_url). You need to add keys for _host and _scheme within the kw object that you return from your pagegenerator callable. Mine looks like this: def subdomain_pregenerator(request, elements, kw): # pop the store name from the kw and append the _host including # the store name as the subdomain store_name = kw['traverse'][0] kw['_host'] = '{0}.{1}'.format(store_name, request.host.split(':')[0]) kw['_scheme'] = request.host_url.split(':')[0] kw['traverse'] = kw['traverse'][1:] return elements, kw for a route definition that looks like this config.add_route( 'store', '/*traverse', factory=StoreFactory, pregenerator=subdomain_pregenerator)
Larry Weya at Quora Visit the source
Related Q & A:
- How do I manipulate a large number of moving platforms?Best solution by Electrical Engineering
- How to get the file using it's URL in Javascript?Best solution by befused.com
- How do I change the information that's in autofill?Best solution by Yahoo! Answers
- How do I find a yahoo user's profile if I have their email address?Best solution by Yahoo! Answers
- How can i change yahoo homepage language's to Malaysian?Best solution by Yahoo! Answers
Just Added Q & A:
- How many active mobile subscribers are there in China?Best solution by Quora
- How to find the right vacation?Best solution by bookit.com
- How To Make Your Own Primer?Best solution by thekrazycouponlady.com
- How do you get the domain & range?Best solution by ChaCha
- How do you open pop up blockers?Best solution by Yahoo! Answers
For every problem there is a solution! Proved by Solucija.
-
Got an issue and looking for advice?
-
Ask Solucija to search every corner of the Web for help.
-
Get workable solutions and helpful tips in a moment.
Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.