阿里云主机 django_如何使用Django主机创建无限域

阿里云主机 django

Subdomains are used for a variety of reasons including but not limited to eCommerce stores, location or language specific sites, and version testing.

使用子域的原因多种多样,包括但不限于电子商务商店,特定于位置或语言的站点以及版本测试。

For example, if you own the domain website.com the blog can be on the subdomain blog.website.com and the eCommerce store can be at store.website.com.

例如,如果您拥有域website.com,则博客可以位于子域blog.website.com上,而电子商务商店可以位于store.website.com。

However it is important to keep in mind search engines such as Google see subdomains as separate sites independent of the main domain.

但是,请务必牢记Google等搜索引擎将子域视为独立于主域的单独站点。

If you are trying to grow your site via browser search rankings, we recommend using subdirectories. Subdirectories, such as website.com/blog, are arguably better for SEO given that all pages created under the subdirectory are considered part of the main site.

如果您尝试通过浏览器搜索排名来扩大网站,我们建议使用子目录。 可以将子目录(例如website.com/blog)用于SEO,因为将在子目录下创建的所有页面均视为主站点的一部分。

But if you are interested in sending users directly to your eCommerce site via ads or recreating your site for a Spanish-speaking audience, then a subdomain may be the better option.

但是,如果您有兴趣通过广告直接将用户吸引到您的电子商务网站,或者为讲西班牙语的受众群体重新创建您的网站,那么子域可能是更好的选择。

It all depends on the intention and purpose of the site.

这完全取决于站点的意图和目的。

In this article, we are going to go over the configurations required to get a subdomain up and running on your Django project. With the Django project mysite, we will create two apps, one called main and the other blog. The blog app will connect to blog.website.com while the main app will appear when the website.com is requested.

在本文中,我们将介绍在Django项目上启动并运行子域所需的配置。 通过Django项目mysite ,我们将创建两个应用程序,一个称为main ,另一个称为blog博客应用将连接到blog.website.com,而应用将在请求website.com时出现。

创建一个Django项目和主应用 (Create a Django project and the main app)

macOS Terminal

macOS终端

(env)User-Macbook:env user$ pip install django==2.1.15(env)User-Macbook:env user$ django-admin startproject mysite(env)User-Macbook:env user$ cd mysite(env)User-Macbook:mysite user$ python3 manage.py startapp main

Windows Command Prompt

Windows命令提示符

(env)C:\Users\Owner\desktop\env> pip install django==2.1.15(env)C:\Users\Owner\desktop\env> django-admin startproject mysite(env)C:\Users\Owner\desktop\env> cd mysite(env)C:\Users\Owner\desktop\env\mysite> py manage.py startapp main

Once your virtual environment is created and activated, install django, create the project mysite, then create the app main. For simplicity the Django project will always be referred to as mysite. If you already have an app, then skip down to the creating a blog app of the article.

创建并激活虚拟环境后 ,安装django ,创建项目mysite ,然后创建应用程序main 。 为简单起见,Django项目将始终称为mysite 。 如果您已经有一个应用程序,那么请跳至创建该文章的博客应用程序。

If you are new to Django, refer to this beginner’s guide. Otherwise, configure your app’s settings and add HTML templates, URLs, and views needed.

如果您是Django的新手 ,请参阅此初学者指南 。 否则,请配置应用程序的设置并添加所需HTML模板,URL和视图。

Then, when you are ready, we can move on to the blog app.

然后,当您准备就绪时,我们可以继续进行博客应用程序。

创建一个名为Blog的新应用 (Create a new app called blog)

macOS Terminal

macOS终端

(env)User-Macbook:mysite user$ python3 manage.py startapp blog

Windows Command Prompt

Windows命令提示符

(env)C:\Users\Owner\desktop\env\mysite> py manage.py startapp blog

With the main app configured, return back to your CLI and create a new app called blog. This is the app that will connect to the subdomain blog.website.com.

配置应用程序后,返回到CLI并创建一个名为blog的新应用程序。 这是将连接到子域blog.website.com的应用程序。

创建博客URL (Create the blog URLs)

from django.urls import path
from . import viewsapp_name = "blog"
urlpatterns = [
path("", views.homepage, name="homepage"),
]

Similar to the configurations made to main, you will need to add a URLs file to the app blog.

main的配置类似,您需要向应用程序博客添加URL文件。

From there, you will need to add the app to the list of installed apps in the settings, create the templates, and the views that connect to your URL paths. If you need to learn more about setting up your templates, views and URLs see Django configurations.

从那里,您需要将应用程序添加到设置中已安装的应用程序列表中,创建模板以及连接到URL路径的视图。 如果您需要了解有关设置模板,视图和URL的更多信息,请参阅Django配置

安装django-hosts软件包 (Install the django-hosts package)

macOS Terminal

macOS终端

(env)User-Macbook:mysite user$ pip install django-hosts

Windows Command Prompt

Windows命令提示符

(env)C:\Users\Owner\desktop\env\mysite> pip install django-hosts

Once your two apps are configured, you can use django-hosts. Created by jazzband, django-hosts is an app designed to route requests to different URL schemes defined in the hosts.py file.

一旦配置了两个应用程序,就可以使用django-hosts了 。 django-hosts由jazzband创建,是一个旨在将请求路由到hosts.py文件中定义的不同URL方案的应用程序。

在设置中将应用添加到已安装的应用中 (Add the app to the installed apps in the settings)

env > mysite > mysite > settings.py

env> mysite> mysite> settings.py

INSTALLED_APPS = [
'main.apps.MainConfig',
'blog.apps.BlogConfig',
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'django_hosts',
]

Go to the settings.py file and add django_hosts to the list of install apps. As you can see, the apps main and blog are also listed among the installed apps.

转到settings.py文件,并将django_hosts添加到安装应用程序列表。 如您所见,已安装的应用程序中也列出了应用程序主程序博客

env > mysite > mysite > settings.py

env> mysite> mysite> settings.py

MIDDLEWARE = [
'django_hosts.middleware.HostsRequestMiddleware',
...
'django_hosts.middleware.HostsResponseMiddleware',
]

Then find the middleware listed below the installed apps and add the HostsRequestMiddleware to the top of the list and the HostsResponseMiddleware to the end of the list.

然后找到安装的应用程序下面列出的中间件和添加HostsRequestMiddleware到列表的顶部和HostsResponseMiddleware到列表的末尾。

创建主机文件 (Create the hosts file)

env > mysite > mysite > (New File) hosts.py

env> mysite> mysite>(新文件)hosts.py

from django_hosts import patterns, hosthost_patterns = patterns(
'',
host(r'', 'mysite.urls', name=' '),
host(r'blog', 'blog.urls', name='blog'),
)

Now create the Python file hosts.py in the same folder as settings.py. Within this file, import patterns and host form django_hosts then specify the host_patterns. The host patterns are the subdomains. The empty host pattern are all the domains in the mysite.urls while the blog pattern is the subdomain blog.

现在在与settings.py相同的文件夹中创建Python文件hosts.py 。 在此文件中,导入模式和主机形式django_hosts然后指定host_patterns 。 主机模式是子域。 空主机模式是mysite.urls中的所有域,而博客模式是子域blog

在设置中将应用添加到已安装的应用中 (Add the app to the installed apps in the settings)

env > mysite > mysite > settings.py

env> mysite> mysite> settings.py

ROOT_HOSTCONF = 'mysite.hosts'DEFAULT_HOST = ' '

At this point, we need to add the rest of the configurations. Add the ROOT_HOSTCONF, setting it equal to the hosts.py path. Then add the DEFAULT_HOST=' '. The default host is the name of the host pattern that is called if no other pattern matches a request.

此时,我们需要添加其余配置。 添加ROOT_HOSTCONF ,将其设置为与hosts.py路径相等。 然后添加DEFAULT_HOST=' ' 。 默认主机是主机模式的名称,如果没有其他模式与请求匹配,则会调用该主机模式。

env > mysite > mysite > settings.py

env> mysite> mysite> settings.py

ALLOWED_HOSTS = [
'127.0.0.1',
...
'blog.localhost',
]

The last thing we need to do is add the host blog.localhost to the list of allowed hosts in the settings. If you forget to add this host, you will not have access to the blog in the local browser even after adding a local DNS host for the subdomain.

我们需要做的最后一件事是将主机blog.localhost添加到设置中允许的主机列表中。 如果您忘记添加此主机,即使在为子域添加本地DNS主机之后,也将无法在本地浏览器中访问博客。

访问本地主机上的子域 (Access the subdomain on your localhost)

If you run the server and visit 127.0.0.1:800, the main app is displaying.

如果您运行服务器并访问127.0.0.1:800,则显示应用程序。

But to get to the blog app, we need to visit blog.local:8000. If you try to access this page, it does not load. That’s because thee is no local DNS host that matches. We need to add it to the list of hosts.

但是要进入博客应用程序,我们需要访问blog.local:8000。 如果您尝试访问此页面,则不会加载。 这是因为您不是与之匹配的本地DNS主机。 我们需要将其添加到主机列表。

For Windows:

对于Windows:

Click on the Windows icon in the bottom left corner of your screen. Then search for the app Notepad.

单击屏幕左下角的Windows图标。 然后搜索应用程序记事本。

Before opening the app click the down arrow under “Open”, and select “Run as administrator”.

打开应用程序之前,单击“打开”下的向下箭头,然后选择“以管理员身份运行”。

Once Notepad is open, click File > Open and navigate to This PC/Local Disk/Windows/System32/drivers and select the folder “etc”.

打开记事本后,单击“ 文件”>“打开” ,然后导航到“ 此PC /本地磁盘/ Windows / System32 /驱动程序”,然后选择文件夹“ etc”。

If no files are showing in the “etc” folder, you need to change “Text Documents” to “All Files” in the drop down menu.

如果“ etc”文件夹中没有文件显示,则需要在下拉菜单中将“文本文档”更改为“所有文件”。

Then you can select and open the file called “hosts”.

然后,您可以选择并打开名为“主机”的文件。

This PC > Local Disk > Windows > System32 > drivers > etc > hosts

此PC>本地磁盘> Windows> System32>驱动程序>等>主机

# localhost name resolution is handled within DNS itself.
# 127.0.0.1 localhost
# ::1 localhost
# 127.0.0.1 blog.localhost

Find where the localhost 127.0.0.1 is listed, then add a new local host called blog.localhost. Be sure to save the file.

找到列出localhost 127.0.0.1位置,然后添加一个名为blog.localhost的新本地主机。 确保保存文件。

For macOS:

对于macOS:

For mac users, the process is similar.

对于Mac用户,此过程类似。

System Preferences > Network (Choose your network connection) > Advanced > DNS

系统偏好设置>网络(选择网络连接)>高级> DNS

127.0.0.1 localhost
255.255.255.255 broadcasthost
::1 localhost127.0.0.1 blog.localhost

With the DNS records updated, you can then access the subdomain for your browser. If you are running into issues, review the steps above or switch to a different browser.

随着DNS记录的更新,您可以随后访问浏览器的子域。 如果您遇到问题,请查看上述步骤或切换到其他浏览器。

Originally published at https://www.ordinarycoders.com.

最初发布在 https://www.ordinarycoders.com上

翻译自: https://levelup.gitconnected.com/how-to-add-subdomains-using-django-hosts-dbe3cd4cedf3

阿里云主机 django

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值