Configure one django app on seperate domain and rest on other and need to share data between apps - django

How can i configure a separate domain for one app and another domain rest of the apps. I tried using django sites, django-host. Please share the example

Let there be 2 domains main.example.com and rest.example.com, and the application main_app is to be hosted on main.example.com and the rest of the application on rest.example.com.
# project/hosts.py
from django_hosts import patterns, host
host_patterns = patterns('',
host('main', 'main_app.urls', name='main'),
host('rest', 'project.urls', name='rest'),
)
Considering that the rest of the application URLs is on project.urls.

Related

Set up DRF Browsable API Root with all api urls

In my urls.py I have many rest framework urls:
path(
"api/",
include([
path("users/", api_views.users, name="users"),
path("proposals/", api_views.Proposals.as_view(), name="proposals"),
path("requests/", api_views.Requests.as_view(), name="requests"),
#...
])
)
I can visit the individual endpoints in the browser and access the browsable API, but I want to set up a browsable API Root where I can see all the available endpoints. The DRF tutorial has an example in which they list the urls they want in the root, but I have a lot of urls and I want all of them to show up. Is there a way to include all the urls in my "api/" path?
You should try to use drf_yasg package. Here's a documentation for that.

Django get sub domain from which the request was sent from

i have a standalone front ends serving on different sub domains, and a single backend responding to all requests through API(s)
Say i have following subdomains
first.example.com
second.example.com
third.example.com
and my backend is at
backend.example.com
in my backend views i want to know which subdomain sent me request
i tried
self.request.get_host().split('.')[0]
but this gave me "backend" as result from every sub-domain
what i want to have :
if request was sent from first.example.com > in my
backend.example.com view i want to have "first"
if request was sent from second.example.com > in my
backend.example.com view i want to have "second"
and so on
Technologies information:
Subdomains using React
Backend using Django
Server Nginx
other server Gunicorn
You can get subdomain name from HTTP_REFERER
from urllib.parse import urlparse
urlparse(request.META.get('HTTP_REFERER')).hostname.split('.')[0]

What is the difference between "'pages.apps.PagesConfig'" and "pages"?

It seems there are two ways to register a Django app (say, pages) in INSTALLED_APPS (in file 'settings.py'):
INSTALLED_APPS = [
pages, #option 1
pages.apps.PagesConfig, #option 2
]
Both seem to work with simple apps. But are there any differences between pages.apps.PagesConfig and pages?
Per user #riterix on Reddit who responded to my inquiry:
"The second one gives you the ability to load custom things... Like signals,...
For example you want to load some settings from db after accounts app load when a user logged in through signals.
NB : We used the second method in our project to load currency, metrics,... As a sessions variables based on the user country after he successfully logged in."

django load URLS based on subdomain

I am facing a problem here dealing with subdomains.
Taking wix.com as an example, how do I do this?
www.mysite.com/page1 => load r'^page1/$'from URLS_main.py
user1.mysite.com/page1 => load r'^page1/$' from URLS_users.py
currently I am using a middleware to determine the subdomain part, but I can't figure out the back part of it without doing lots of redirecting in the views or middleware.
Here is what my middleware does if it is relevant:
class subdomainMiddleware:
def process_request(self,request):
domain_parts = request.get_host().split('.')
subdomain = "www"
if(len(domain_parts) > 2):
subdomain = domain_parts[0].lower()
request.subdomain = subdomain

Django redirecting to external url that contains parameters (for development)

When developing with Django without a web server (serving directly from Django) I have a problem with external urls that lack the domain part and have parameters.
Let's say I'm using a javascript library that does an ajax call to "/prefix/foo/bar?q=1" (the url is not something I can change). It is not a problem for the production server but only a problem when not using a web server. I can redirect by adding the following pattern to my urlpatters:
(r'^prefix/(?P<path>.*)$', 'django.views.generic.simple.redirect_to', {'url': 'htttp://example.com/prefix/%(path)s'}),
but of course %(path)s will only contain "foo/bar" not "foo/bar?q=1".
Is there a way to handle this problem with Django?
You'll have to write your own redirect:
def redirect_get(request, url, **kwargs):
if request.META['QUERY_STRING']:
url += '?%s' % request.META['QUERY_STRING']
return redirect_to(request, url, **kwargs)