Django subdomains a la craigslist.com

I am interested in customizing my application so that I can serve specific content.

Something like craigslist, where miami.craigslist.org only shows posts in miami.

Assuming I have a model with a City field, what would be the best way to achieve this?

+4
source share
2 answers

Banjer suggested the structure of the Sites, but I feel that it may not be 100% perfect for your situation. Site frameworks work well for different sites that have the same database. But in your case, this is still the same site, and you basically just want to convey an argument to your views on the city.

Perhaps you could make something even simpler by simply pointing out the *.domain.com on your django site and then using request.get_host() in the view to parse the subdomain:
https://docs.djangoproject.com/en/1.4/ref/request-response/#django.http.HttpRequest.get_host

So, if the domain was "losangeles.domain.com" and you parsed "losangeles", you could use it as your tag for your city in your request. And when you create new content, you can just mark it with a city.

 Post.objects.filter(city_tag="losangeles") 

I suppose it depends on how reliable the "location" structure you need on your site. This may be a City model with various metadata fields, including the tag field. You will then associate it with the content as a foreign key. This seems like the same general idea as the site structure, but in this case you are not really creating different sites.

+5
source

You should check the structure of "sites" . If you've ever used the Django built-in administrator, then you probably saw a section of sites where example.com is the only default site.

An example of the real world, from the link I provided:

Link content to multiple sites

Websites powered by Django LJWorld.com and Lawrence.com are run by the same news organization, the Lawrence Journal-World in Lawrence, Kansas. LJWorld.com focuses on news, while Lawrence.com focuses on local entertainment. But sometimes editors want to publish an article on both sites.

A dead way to solve the problem would be for the site to require producers to publish the same story twice: once for LJWorld.com and again for Lawrence.com. But this is inefficient for site manufacturers and its excess for storing multiple copies of the same story in the database.

The best solution is simple: both sites use the same article database, and the article is associated with one or more sites.

So, instead of the "City" field, you should add a field to your model for the site, for example:

 sites = models.ManyToManyField(Site) 

You basically have one database that all of your cities use. Convenient, but I wonder how it will decrease in the future. I myself am launching a similar project and will use the Django website structure to get the project from the ground. I am interested in hearing about a more scalable solution on behalf of the database though.

+1
source

Source: https://habr.com/ru/post/1411891/


All Articles