doniyor
doniyor

Reputation: 37904

how to show content depending on domain/subdomain

I am trying to write a little blog where only some specific content of blog should show up depending on domain/subdomain.

lets say, the main blog is at www.mainblogsite.com. here I want to show all blog entries.

But lets say, there is also a subdomain of main blog, called www.fr.mainblogsite.com where only blog entries in french should show up.

I am writing the blog in Django.

my first thoughts on database modelling were like this:

class BlogEntry(models.Model):
  text = models.TextField()
  lang = models.CharField(max_length="2")

I just get the domain with request.META['HTTP_HOST'] and depending on domain name, i will filter blog entries by language like

#for fr.mainblogsite.com
BlogEntry.objects.filter(lang='fr')

which gives me only french blog entries for fr.mainblogsite.com

my question is: does this database architecture make sense? I dont know much about how domains and subdomains work,.. how and where could it be better?

Upvotes: 3

Views: 89

Answers (2)

Anshul Goyal
Anshul Goyal

Reputation: 77007

I think you should have a look at the django.contrib.sites models, which are there for precisely the problem you are trying to solve - have multiple subdomain and domain represented by the content.

Quoting the example mentioned there:

from django.db import models
from django.contrib.sites.models import Site

class BlogEntry(models.Model):
    headline = models.CharField(max_length=200)
    text = models.TextField()
    # ...
    sites = models.ManyToManyField(Site)

Upvotes: 2

Martin Thurau
Martin Thurau

Reputation: 7654

From a DB design standpoint you should move the lang field to an own model and reference it from the BlogEntry.

class Language(models.Model):
    lang = models.CharField(max_length="2")

class BlogEntry(models.Model):
    text = models.TextField()
    lang = manufacturer = models.ForeignKey('Language')

That way you can change the actual name of the language by updating a single record and not multiple. However, if you are sure that this will never you can also stick with your approach.

Upvotes: 1

Related Questions