I have two django application which are on same server on port 80 and 9002. i.e. urls are www.abc.com and www.abc.com:9002
Both share same database postgresql for authentication. I want to share the share the session data between them so that user logged in to one application can log in automatically in another application.
I read these answers : Multiple Django apps, shared authentication and How to get distinct Django apps on same subdomain to share session cookie?
And did this in my both django application:
Used the same secret key in both.
Added these lines:
SESSION_ENGINE = 'django.contrib.sessions.backends.signed_cookies'
SESSION_COOKIE_NAME = 'abc'
SESSION_COOKIE_DOMAIN = '.abc.com'
But still I am unable to achieve the purpose.
How to share the session cookie between two django apps so that i can have shared authentication?
Other than you have to apply these settings to both applications,
the only thing missing with your approach is the SESSION_COOKIE_DOMAIN.
You set it to '.abc.com', which means it will work if your app has domain name: www.abc.com and somesubdomain.abc.com.
But your second app in this case www.abc.com:9002, by including the port it doesn't share the same TLD with www.abc.com. So, django thinks www.abc.com:9002 and www.abc.com are very different domain and not from the same root .abc.com.
If I'm working on this, there are several possible approach:
Combine both app into one single root django app. Django app were modular anyway, so you could create one single ROOT_URL_CONF and DJANGO_SETTINGS_MODULE to specify how these two apps works in the same domain. You could, for example, append a different prefix url for each app.
You use load balancer, or reverse proxy, such as nginx or haproxy to assign different subdomain for each app, then deploy each app in a different port. Let's say, the end result is you have the first django app deployed on first.abc.com and the second app in second.abc.com (All with port 80 in the frontend), then it will share the same session. Just remember that in the backend you need to assign the actual port that the app uses.
Additional notes from mine. In production settings, you also want to add ALLOWED_HOSTS settings and include .abc.com in the list.
Related
I have been scouring the web to help me find the best way to do this and haven't found a proper answer.
I want to create a single web app with flask that contains multiple dashboard pages. The app needs to run on a different subdomain for every user–the user being a different business eg. client1.myapp.com. The functionality will be largely shared across the different clients and thus subdomains. However, I want to define a config file that will look something like this:
client1 = {"show_graph1":True, "show_graph2":False}
client2 = {"show_graph1":False, "show_graph2":True}
So the app would be hosted on a single aws elastic beanstalk instance and serve all these subdomains. The flow would be:
Client1 goes on unique url client1.myapp.com
Client1 logs in to myapp
Myapp recognises that it is on subdomain for client1, fetches the configuration from the config file and configures the dashboard pages accordingly.
I have looked into flask blueprints and from what I've understood this would be the best way to set this up, but I am not clear on how I would dynamically fetch and implement the configuration nor on how will flask simultaneously serve all subdomains at once.
What would be the best application structure to setup this use case with flask?
Any help would be much appreciated.
If your flask app is listening for all connections, you can point as many domains to it as you like. Then in your dashboard views, or globally if you prefer, you can get your configuration based on what domain the app was requested through.
For example:
#app.before_request
def before_request_func():
domain = request.host
g.client_config = get_client_config(domain)
I am trying to set up a subdomain on a flask server, which has a server hosted on Heroku and a custom domain hosted on GoDaddy. I have verified that my subdomain is working locally. The subdomain is a separate blueprint in my app. My setup in flask is:
blueprint = Blueprint('blueprint', __name__, template_folder="templates", subdomain="blueprint")
#blueprint.route('/')
def index():
return "Hello Mate"
and then
app.config['SERVER_NAME'] = os.environ['MY_SERVER_NAME']
from blueprint.views import blueprint
app.register_blueprint(blueprint)
On my local machine, I set up a custom record in my hosts file (/etc/hosts) to test the subdomain. The file has the entries:
127.0.0.1 virtual.local
127.0.0.1 blueprint.virtual.local
If I navigate to blueprint.virtual.local:5000, I see the intended result (a page that just says Hello Mate. I believe this proves my subdomain settings are set up properly, at least within flask.
I push my code to my heroku app, and this is where I start running into problems. My heroku site has a custom domain associated with it from before. I start by adding an entry for the new subdomain. Running heroku domains in the terminal gives me:
=== myapp Domain Names
blueprint.mysite.com
www.mysite.com
myapp.herokuapp.com
mysite.com
The first issue I am running into is that I can only either view my site on the heroku URL or the custom domain. This is a result of app.config['SERVER_NAME'] (which I set to get my subdomain working) being linked to either the heroku URL or my custom URL. When it is set to the heroku URL, I can only see the site when I visit it at that URL, and when I go to my custom domain, I get a 404 error. This is reversed when I switch the value of the SERVER_NAME.
The second issue is that I cannot get my subdomain to work with GoDaddy on Heroku. In GoDaddy, I create a CNAME record that points my subdomain (blueprint) to my heroku site (myapp.herokuapp.com). Is this correct? I get a 404 error whenever I visit the subdomain on my custom domain (blueprint.mysite.com). I believe this is related to the first issue, but I am not sure. Am I missing any steps?
Any advice on the proper way to set this up, so that I can use Flask subdomains on Heroku, being hosted on a custom domain on GoDaddy? Thanks in advance!
I suspect you're confusing Flask Blueprints and Heroku apps. A flask app (and its containing git repository, in this case) is one and only one Heroku app (a single domain, or subdomian... but crucially, only one of them).
A Flask Blueprint is a way of organizing individual sections of a single Flask app to be more modular.
To create Heroku Apps at awesome.darrellsilver.com and sauce.darrellsilver.com you should set up two independent Flask Apps, in two independent Git repos.
For what it is worth, I had 404 issues when I switched to an SSL endpoint using Flask on Heroku. All I had to do was change the app.config "SERVER_NAME" to the new "www.CUSTOMENDPOINTNAME.com" address from the "CUSTOMENDPOINTNAME.herokuapp.com" which was there previously.
I have a simple portfolio website with some html and css files in the root directory of the site hosted by Dreamhost. I also have a Django app that I'd like to place in a subdomain of this same website. However, Heroku will be serving the django app. I'm confused about how to organize and configure the whole portfolio/django website. How would the system work using two different hosts? Should I integrate the static portfolio site into the django project? Or do I keep them completely seperate and have them live on their own servers? Sorry if my question doesn't make sense. I'm very confused.
As far as the internet's concerned, a subdomain is a completely separate website. You can point a subdomain at whatever address you like; the internet doesn't care that it's a completely separate host. You can host your system however you like: both on Dreamhost, both on Heroku, or one on each. The latter setup is the most complex, so we'll walk through that one here.
Let's say your site is example.com and you want the portfolio site to be portfolio.example.com. If your app's running on Heroku, it'll have a name similar to yourportfolio.herokuapp.com. So we need to do two things: tell Heroku that your app is served from portfolio.example.com, and tell the DNS system to point from your subdomain from Heroku.
Pointing the subdomain to Heroku
Presuming your domain name is hosted on Dreamhost, go to the Domains section of the control panel, then Manage Domains. Under example.com is a link called DNS. You need to add a custom CNAME record; set name to portfolio, type to CNAME, and value to yourportfolio.herokuapp.com.. CNAMEs are a way of setting up aliases on the web; they mean "this site is also known as foo".
Telling Heroku to serve your app
Within your Heroku project, run heroku domains:add portfolio.example.com.
Heroku has documentation about subdomains here, which is a useful overview of the process as well as giving details of more complex setups.
I have two Django projects and applications running on the same Apache installation. Both projects and both applications have the same name, for example myproject.myapplication. They are each in separately named directories so it looks like .../dir1/myproject/myapplication and .../dir2/myproject/myapplication.
Everything about the actual public facing applications works fine. When I log into either of the admin sites it seems ok, but if I switch and do any work on the opposite admin site I get logged out of the first one. In short I can't be logged into both admin sites at once. Any help would be appreciated.
Set the SESSION_COOKIE_DOMAIN option. You need to set the domain for each of your sites so the cookies don't override each other.
You can also use SESSION_COOKIE_NAME to make the cookie names different for each site.
I ran into a similar issue with a live & staging site hosted on the same Apache server (on CentOS). I added unique SESSION_COOKIE_NAME values to each site's settings (in local_settings.py, create one if you don't have one and import it in your settings.py), set the SESSION_COOKIE_DOMAIN for the live site and set SESSION_COOKIE_DOMAIN = None for staging. I also ran "python manage.py cleanup" to (hopefully) clean any conflicted information out of the database.
Well, if they have the same project and application names, then the databases and tables will be the same. Your django_session table which holds the session information is the same for both sites. You have to use different project names that will go in different MySQL (or whatever) databases.
The session information is stored in the database, so if you're sharing the database with both running instances, logging off one location will log you off both. If your circumstance requires you to share the database, the easiest workaround is probably to create a second user account with admin privileges.
Let me guess, is this running on your localhost? and you have each site assigned to a different port? i.e. localhost:8000, localhost:8001 ..?
I've had the same problem! (although I wasn't running Apache per se)
When you login to the admin site, you get a cookie in your browser that's associated with the domain "localhost", the cookie stores a pointer of some sort to a session stored in the database on the server.
When you visit the other site, the server tries to interpret the cookie, but fails. I'm guessing it deletes the cookie because it's "garbage".
What you can do in this case, is change your domain
use localhost:8000 for the first site, and 127.0.0.1:8001 for the second site. this way the second site doesn't attempt to read the cookie that was set by the first site
I also think you can edit your HOSTS file to add more aliases to 127.0.0.1 if you need to. (but I haven't tried this)
I am trying to mock-up an API and am using separate apps within Django to represent different web services. I would like App A to take in a link that corresponds to App B and parse the json response.
Is there a way to dynamically construct the url to App B so that I can test the code in development and not change to much before going into production? The problem is that I can't use localhost as part of a link.
I am currently using urllib, but eventually I would like to do something less hacky and better fitting with the web services REST paradigm.
You could do something like
if settings.DEBUG:
other = "localhost"
else:
other = "somehost"
and use other to build the external URL. Generally you code in DEBUG mode and deploy in non-DEBUG mode. settings.DEBUG is a 'standard' Django thing.
By "separate apps within Django" do you mean separate applications with a common settings? That is to say, two applications within the same Django site (or project)?
If so, the {% url %} tag will generate a proper absolute URL to any of the apps listed in the settings file.
If there are separate Django servers with separate settings, you have the standard internet problem of URI design. Your URI's can be consistent with only the hostname changing.
- http://localhost/some/path - development
- http://123.45.67.78/some/path - someone's laptop who's running a server for testing
- http://qa.mysite.com/some/path - QA
- http://www.mysite.com/some/path - production
You never need to provide the host information, so all of your links are <A HREF="/some/path/">.
This, generally, works out the best. You have can someone's random laptop being a test server; you can get the IP address using ifconfig.