简体   繁体   English

如何根据域/子域显示内容

[英]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 . 比方说,主要博客是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. 但是,我们可以说,还有一个主要博客的子域名,名为www.fr.mainblogsite.com ,其中只显示法语中的博客条目。

I am writing the blog in Django. 我在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 我只是通过request.META['HTTP_HOST']获取域名,并且根据域名,我将按语言过滤博客条目

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

which gives me only french blog entries for fr.mainblogsite.com 这给了我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? 我不太了解域和子域如何工作,...它如何以及在哪里可以更好?

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. 我想你应该看一下django.contrib.sites模型,这些模型恰好是你试图解决的问题 - 有多个子域和域由内容表示。

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)

From a DB design standpoint you should move the lang field to an own model and reference it from the BlogEntry. 从数据库设计的角度来看,您应该将lang字段移动到自己的模型并从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. 但是,如果你确定这绝不会让你坚持你的方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM