How to show content by domain / subdomain

I am trying to write a small blog where only certain content in the blog should be displayed depending on the domain / subdomain.

let's say the main blog is at www.mainblogsite.com

. here i want to show all blog posts.

But let's say there is also a subdomain of the main blog called www.fr.mainblogsite.com

where only French blog posts should appear.

I am writing a blog in Django.

my first thoughts on database modeling were like this:

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

      

I just get the domain from request.META['HTTP_HOST']

and depending on the domain name, I will filter blog posts 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 don't know how domains and subdomains work, how and where could it be better?

+3


source to share


2 answers


I think you should take a look at the models django.contrib.sites

that are there for the exact problem you are trying to solve - multiple subdomain and content-represented domain.

Quoting the example given here:



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)

      

+2


source


From a DB design perspective, you should move the field lang

to your own model and reference it from BlogEntry.

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

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

      



This way you can change the actual name of the language by updating one entry, not several. However, if you are sure that this is the case you can never stick to your approach.

+1


source







All Articles