Using Django to Create Child Sites - django

I am creating a series of small sites, I'm using the django framework. The theory goes a user comes to a master site, signs up, then he gets his own child site.
Example:
navigate to example.com
user creates an account "mysite"
user then gets his own site: mysite.example.com and he can configure this all he wants
My question: * would it be better to have a "gold" version of the site that gets created for each site?
for instance: cp ~/goldsite ~/mysite and change the database pointers appropriately ** the downside is if I ever have to do maintenance on a file, I would have to change all subsites.
...or * have one host and configure the database to support multiple sites. The DB might get messy.
Any feedback would be great.

Use the same code/database for all the users, otherwise it would be a nightmare to maintain the code. Think about database migrations, backups, update the code with git...
...or * have one host and configure the database to support multiple sites. The DB might get messy.
It shouldn't be really complex, associate each website object with an user and use the permissions properly.

Related

Wordpress database integration/sync with Django

My company will be rolling out a new website to accompany our product launch and would like to switch over to Wordpress as our content management system. We will be utilizing a Wordpress theme that will allow users to create their own virtual events without having to log into the Wordpress dashboard (back-end). This event information will be displayed on the website for other users to view and register - this is all built into the theme we have purchased.
These virtual events will be held on our software platform, which is built on Django. We would like to utilize Wordpress to manage the login and event creation process, but would also like to have event information displayed on the Wordpress site AND imported to the Django database as well.
For example: Users will need to submit three items on the front-end Wordpress site to create an event: Title, Host Name, and Start Time. When that information is submitted can it be automatically duplicated to the Django database in addition to it being sent to the WP database?
I have already done some research on this matter, but what I have found thus far might not work for our needs. I found this presentation by Collin Anderson - it is similar to what we want to achieve, but I believe the application is a little different: http://www.confreaks.com/videos/4493-DjangoCon2014-integrating-django-and-wordpress-can-be-simple.
I have a lot of experience with Wordpress, but very limited experience with Django. This question is more for research purposes than a "how-to". We want to know if we can continue to plan on heading toward the Wordpress direction or if we should seek alternative methods for our site. I appreciate you taking moment to answer my question.
I'm working on something similar at the moment and found a good starting point was this:
http://agiliq.com/blog/2010/01/wordpress-and-django-best-buddies/
That way, as dan-klasson suggests, you can use the same database for both the wp side and the django side.
In short, first things first take a back up of the wp database in case anything goes wrong.
Create a new django project and set your settings.py to use the wp database.
In this new django project you can use ./manage.py inspectdb > models.py to autogenerate a models.py file of the wp database. Be careful here as there are differences between wp and django conventions. You will need to manually alter some of the auto generated models.py. Django supplies db_table and db_column arguments to allow you to rename tables and columns for the django part if you'd like to.
You can then create a new django app in your django project and place the models.py you've created in there. This new app will be using the same data as your wordpress site. I'm not sure exactly what you want to do but I would be very, very careful about having wordpress and django access the same data simultaneously. You may want to set the django side as read only.
You can then add other apps to extend the django side of things as you wish.
I should point out that I haven't completed my work on this yet but so far so good. I'll update as I find sticking points etc.

Is there any way to administer multiple Django servers at once?

Short Version
Is there any tool that will let me use a single Django admin page to affect multiple Django installations on different servers?
Detailed Version
I've got a bunch of different servers, each with their own Django installation. This works great ... except when I want to do something via the Django admin to all of the servers, in which case I have to log on to each server separately.
For instance, let's say I have a release coming and a co-worker (who's not a programmer) wants to use the admin to make a "message" record about the release for the server's users to see. They have to log in to each server's admin individually, create the message record, then move on to the next server until they've gotten through all of them.
To get around this whenever I have a multi-server change I've been using Git; ie.:
I make a commit with files for the change
I push that commit
I pull that commit on all of the servers at once (using Fabric).
However, this too is sub-optimal, because we can't take advantage of the admin interface, and doing something as simple as adding a "new release coming" message requires an actual (mini-)release itself.
So, it seems to me the best way to handle this would be if there was some sort of meta-admin tool my co-worker could use to Django admin work on multiple servers at once. Does anything like that exist?

How should I set up and deploy my django project that spans over multiple subdomains and multiple databases?

Let's say I'm building a Django driven site that hosts web shops, let's call it webshop.com. I want it to work like this:
A shop named Foo would like a webshop. They register at webshop.com and the subdomain foo.webshop.com is created, as well as the database foo_DB. The owners of Foo enter their products using an admin interface at foo.webshop.com/admin. Customers can then buy the products from foo.webshop.com.
The the shop Bar has the webshop bar.webshop.com - completely unrelated to the other webshops hosted at webshop.com. And so on. webshop.com should be able to host 100's of stores.
The question is: how should I go about doing this with Django?
At first I was thinking that I should have one settings.py file, one virtual server and one database for each webshop. However I fear that it can be generated too much overhead from running all those instances.
Any pointers or thoughts is greatly appreciated!
Perhaps you could usedjango-sudomains https://github.com/tkaemming/django-subdomains/
It is a middleware which creates a subdomain property for each request.
Since the functionality for each site is the same you could then just query for products based on sudomain requested. You could also assign users a subdomain.
All views would be the same except they would take into account this subdomain. This approach could work easy with 1 shared database where each model has a field that points to the subdomain or a shared database with subdomains being the database naem. https://docs.djangoproject.com/en/dev/topics/db/multi-db/#manually-selecting-a-database-for-a-queryset
I've built half a dozen applications that work like this (though none in python, but it's really irrelevant in this case).
Serve everything from the same virtual host. Get the server name with urlparse Once you have the string you want to match, do a lookup in a master database for the database details which match your customer database (ie the hostname, username, password, etc.). Make sure you sanitize that value before querying on it.
I do think your best bet is separating your customers databases. Not only does this ensure that one database problem doesn't bring down all your customer sites, it also allows you to put them on different servers.

Multiple PostgreSQL schemas and users on Django with subdomains

I have seen various questions on multi-site and multi-host Django, including subdomains and specific schemas per subdomain. What I have not seen is a solution (or tips so I can code one) to this problem.
I am using Django + PostgreSQL on a site, let's say myapp.com
The main site myapp.com is used for registration of companies
A registered company gets its own subdomain, company.myapp.com, and logs in and works from there.
My idea of doing this is by making 2 initial schemas in PostgreSQL.
Schema "auth" for companies and users
Schema "empty_company_template" with the basic tables for a company, all empty but hooked up to the right sequences etc.
When a new company registers, I want this to happen:
Create a new schema for the company, derives from empty_company_template
Create a new DB user for the company, named company (the company name)
Set the search path for this new user to company, auth (no access to empty_company_template, no access to other users schema's)
To me this seems better than the existing solutions that all seem to depend on one single database user for the entire application (with access to all schemes). However, I struggle to get this to work. Is this indeed a viable approach? Can anyone point me in the right direction? It's Django, so perhaps it's been done and I just haven't found it?
I have a working solution that does everything except separate users.
It's a small piece of middleware (just process_request) that determines the subdomain, and executes a SET search_path query on the database. It's good enough for me for now.
Anyone interested in the code, just contact me. I'll publish it somewhere when it's final.
EDIT Dec 22, 2010:
I published the code on my blog at
http://blog.dyve.net/django-subdomains-and-postgresql-schemas
If you want to have separate DB users, you'll probably want separate Django instances, otherwise there will be no security gain. This model will require much more complicated process management. I don't think you'll find a ready-made solution for such an application, so you will most probably have to roll your own. Otherwise, if you're unwilling to invest much time, stick with the one user for the entire app approach.

How to host 50 domains/sites with common Django code base

I have 50 different websites that use the same layout and code base, but mostly non-overlapping data (regional support sites, not link farm). Is there a way to have a single installation of the code and run all 50 at the same time?
When I have a bug to fix (or deploy new feature), I want to deploy ONE time + 1 restart and be done with it.
Also:
Code needs to know what domain the request is coming to so the appropriate data is displayed.
The Sites framework comes to mind.
Apart from that we have Django running for multiple sites by symlinking Django to various docroots. Works like a charm, too.
I can see two quite distinct ways to do this:
Use one database and the sites framework. Every post/picture/whatever model is connected to a Site and you always filter on Site. This requires a separate settings file for every database.
Use one database for each and every site. This allows different users for every site, but requires duplication of everything that is stored in the database. It also requires a separate settings file pointing to the correct database.
Either way, you do not duplicate any code, only data.
--
If you need to do site-specific, or post-specific changes to ie. a template, you should read up on how Django loads templates. It allows you to specify a list, ie ["story_%d.html", "story_site_%d.html", "story.html"] and django will look for the templates in that order.
I just ran into this and ended up using a custom middleware class that:
Fetch the HTTP_HOST
Clean the HTTP_HOST (remove www, ports, etc.)
Look up domain in a Website table that's tied to each account.
Set the account instance on the HTTPRequest object.
The throughout my view code I do lookups based on the account stored in the HTTPRequest objects.
Hope that helps someone in the future.