Can namespaces enable multiple domains? - python

My gae app serves multiple domains by if..else.. conditions rather than namespaces.
I see someone else solved it with namespaces
"I have a single app with multiple namespaces defined.
I'd like to set up multiple domains
a.com b.com c.com
and have the app detect the domain and write the domain's data into
its respective namespace."
I don't know how to do it with namespaces and I want a better way to add a domain to the app for settings like content and languages. For example sending an email via a form then I use just a condition instead of namespace.
class FileUploadHandler(blobstore_handlers.BlobstoreUploadHandler):
def post(self):
adminemail = 'admin#domain1.com' if gethost() is 'domain1' else 'admin#domain2.com'
message = mail.EmailMessage(sender=admin_email, subject=self.request.POST.get('subject'))
message.body = ...
message.to='info#domain...
message.send()
self.redirect('/customer_service.htm')
I use same workaround for queries, localization and in some cases even which template to render to while I should be able to make all domains able to be based on same templates and differ only by content so that my app doesn't hard-code the domains and other settings that should be easy to add and change.
Are namespaces a great idea in this case? The way a managed the problem with different domains so far is a variable for the entity which domains it came from
if util.get_host().find('my-dot-com') > 0:
url = 'www.my-dot-com.com'
I have a function that defines what I mean but it may confuse www.domain.com with domain.com or have problems with subdomains
def get_host():
return os.environ.get("HTTP_HOST", os.environ["SERVER_NAME"])
I'd be glad to know any idea if you have, or if I'm mistaken and shouldn't use namespaces in this case.
Thanks

Yes, namespaces are ideally suited to what you're doing. Simply store configuration data like admin emails in a per-domain configuration record, and store all the records for a given domain in a namespace named after that domain.

Related

Mask URL in Django + React webApp

I want to share a page with clients over mail which is something like this:
https://blabla.com/docket/2443
but if I do so they can access all other pages by just changing the docket no. i.e. 2443 in this case.
I tried to use tiny-url but it's of no use. Is there any way to mask the URL to solve the above problem?
There are multiple solutions to that problem.
If that endpoint requires a login and related to a User, you can add permissions to that endpoint which makes it only accessible for the user(s) who are related to it.
If it is a public URL, You can make the id more sophisticated than an integer, you can make the id field a UUIDField, which makes it really hard to guess any other dockets ids.

how to use multiple domains and mask a django project

I have a Django website that's configured on a particular domain example.com. Now I'm planning to use the same website for different countries where I have to use their language. For example China, I'm planning to create a sub domain chinese.example.com which will point to exactly the same example.com project but the language will be switched to chinese. Also I would like to mask this chinese.example.com sub domain with another domain chinaexample.com. Please advise how to accomplish this using Apache & Django.
First step - make sure that all of your domains are added into 'ALLOWED_HOSTS' in django config.
Next - if you will have some content that will be available only for particular domain, it will be handy to use sites framework built into django. But if you want to have one language on multiple domains and want some content to be available for language, not for domain, better solution might be to create own method for assigning content to domain (or language).
Third step - write middleware that will check your domain and activate language assigned to it. Example middleware can look like this:
class DomainLanguageMiddleware:
def process_request(self, request):
try:
host = request.META['HTTP_HOST']
host_port = host.split(':')
if len(host_port) == 2:
host = host_port[0]
if host in settings.LANG_MAP:
request.LANG = settings.LANG_MAP[host]
translation.activate(request.LANG)
request.LANGUAGE_CODE = request.LANG
except KeyError:
pass
This code is using LANG_MAP setting, that should be an dictionary containing domain as key and language assigned to that domain as value.
Using that solution, all domains or subdomains can point to one django instance, with one settings.py file.

Django Subdomain

I'm trying to make a basic store app. I've set up a database so that every product is tied to a particular store: let's call the stores Shoes, Toys, and Books.. I need to set up subdomains for the app (it's in the assignment specs, no choice there) so that I can map to shoes.myapp.com, toys.myapp.com and books.myapp.com. What I think I need to do is somehow set up the subdomain (which I've googled but am confused about: is this the way to go?) and then, I guess, filter my databases from the info in the subdomain so that only products that have the store name "Shoes" for example appear on the page. Am I anywhere approaching the right track or is there a much better way to structure this?
I suggest you to use this application: django-subdomains. http://django-subdomains.readthedocs.org/en/latest/index.html
And then, in your settings.py, you should use:
SUBDOMAIN_URLCONF = {
'toys': 'yourproject.urls.toys',
'shoes': 'yourproject.urls.shoes'
(...)
}
If you need to use the name of the subdomain in a view, it will be attached to the request object:
def your_view(request):
subdomain = request.subdomain
products = Products.objects.filter(store=subdomain) #an example how to use it to specif database queries. I dont know how your models are

Allowing users to use custom domains for Django app on Heroku

I have a Django app hosting on Heroku. In the app, the users create pages at http://domain.com/username
I'd like to give users the option to use their own domain name for their page using a CNAME. Ideally I'd like to avoid an A-Record in case I change hosts in the future and my IP changes.
This is completely new territory for me and dont even know where to start, or what to look for. Does anyone have a suggestion on where to start? I've seen mention of Wildcard DNS, but not sure how that ties into my app.
Any suggestions would be really appreciated.
Prelim Answer:
If you control the nameserver for the domain and have access to the RNDC Key, you can use the post-signup view/signal to squirt out a cname to your DNS server that will resove username.yoursite.com to yoursite.com. Make sure apache is set up to recieve a wildcard virtualhost to the correct app, and then use a custom middleware to read request.META['SERVER_NAME'].lsplit('.')[0] to see what the subdomain is. You can then use this information in your views to differentiate user subdomains.

Django admin site: how to create a single page for global settings?

I would like to create a single page in the admin site of django where I can change some global variables of the website (title of the website, items in the navigation menu, etc). At the moment I have them coded as context processors but I would like to make them editable. Something similar to what happens in WordPress.
Is this possible?
I can store the data in the databse, but can I have a link in the admin site that goes straight to the first document record and doesnt allow the creation of multiple records (they wouldnt make sense)
Instead of creating a model in the database, would it be possible to change some context_processor from the admin site (I think this would be best)
django-preferences does exactly what you are looking for. The implementation is a bit hacky (particularly the setting of __module__ on the model class to trick Django into thinking it was loaded from a different app), but it works.
This sounds like what the sites framework is intended to help with.
http://docs.djangoproject.com/en/stable/ref/contrib/sites/
"It’s a hook for associating objects and functionality to particular Web sites, and it’s a holding place for the domain names and “verbose” names of your Django-powered sites."
The docs make it sound like it's only good for multiple sites, but it's a great place to put stuff in a single-site-per-django model too.
There's an app called django-values that allows you storing of specific settings in the database.

Categories