未加星标

Package of the Week: django-hosts

字体大小 | |
[开发(python) 所属分类 开发(python) | 发布者 店小二05 | 时间 2016 | 作者 红领巾 ] 0人收藏点击收藏

This is a very handy Django package that I’ve used in a couple of projects. Essentially django-hosts let you serve different parts of your application under different subdomains. For example, let’s say we have a Django application deployed on www.example.com . With this app you can serve an e-commerce under shop.example.com and the help center under help.example.com . And in the end, it’s just a single Django website.

It can also be used to host user spaces, with a wildcard, where your users could get their own subdomain like vitor.example.com or erica.example.com . But that require a few tweaks in the DNS configuration.

A small caveat of developing with django-hosts is that you will need to do some configurations in your local machine, which can differ if you are using windows, linux or Mac.

Installation

Install it with pip:

pip install django-hosts

Add the django_hosts to the INSTALLED_APPS :

INSTALLED_APPS = [ 'django.contrib.admin', 'django.contrib.auth', 'django.contrib.contenttypes', 'django.contrib.sessions', 'django.contrib.messages', 'django.contrib.staticfiles', 'django_hosts', 'core', ]

Add the HostsRequestMiddleware in the beginning of the MIDDLEWARE and HostsResponseMiddleware in the end of the MIDDLEWARE :

MIDDLEWARE = [ 'django_hosts.middleware.HostsRequestMiddleware', 'django.middleware.security.SecurityMiddleware', 'django.contrib.sessions.middleware.SessionMiddleware', 'django.middleware.common.CommonMiddleware', 'django.middleware.csrf.CsrfViewMiddleware', 'django.contrib.auth.middleware.AuthenticationMiddleware', 'django.contrib.messages.middleware.MessageMiddleware', 'django.middleware.clickjacking.XFrameOptionsMiddleware', 'django_hosts.middleware.HostsResponseMiddleware', ]

Still in the settings.py , add the following configuration variables:

ROOT_HOSTCONF = 'mysite.hosts' # Change `mysite` to the name of your project DEFAULT_HOST = 'www' # Name of the default host, we will create it in the next steps

Create a file named hosts.py right next to the urls.py :

mysite/ |-- __init__.py |-- hosts.py # <-- The `ROOT_HOSTCONF` refers to this file |-- settings.py |-- urls.py +-- wsgi.py

mysite/hosts.py

from django.conf import settings from django_hosts import patterns, host host_patterns = patterns('', host(r'www', settings.ROOT_URLCONF, name='www'), # <-- The `name` we used to in the `DEFAULT_HOST` setting )

Usage

Let’s create an app named help to illustrate the usage of the django-hosts :

django-admin startapp help

Then inside of the new app, we create a urls.py module:

help/urls.py from django.conf.urls import url, include from . import views urlpatterns = [ url(r'^$', views.home, name='home'), url(r'^articles/$', views.articles, name='articles'), url(r'^articles/(?P<pk>\d+)/$', views.article_details, name='article_details'), ]

Now we update the mysite/hosts , which is our ROOT_HOSTCONF :

mysite/hosts.py

from django.conf import settings from django_hosts import patterns, host host_patterns = patterns('', host(r'www', settings.ROOT_URLCONF, name='www'), host(r'help', 'help.urls', name='help'), )

Testing Locally

In order to test it locally, you will need to setup a local DNS host.

On Linux and Mac, the file is located in the path /etc/hosts . For Windows it should be somewhere in %SystemRoot%\system32\drivers\etc\hosts .

hosts

127.0.0.1 localhost 255.255.255.255 broadcasthost ::1 localhost 127.0.0.1 www.mysite.local 127.0.0.1 help.mysite.local


Package of the Week: django-hosts
Package of the Week: django-hosts
Templates

Now instead of using the {% url 'home' %} notation, you can load the django-hosts template tags:

{% load hosts %} <a href="{% host_url 'home' host 'www' %}">Homepage</a> <a href="{% host_url 'articles' host 'help' %}">Help Articles</a>

Reverse

The django-hosts extends Django’s default reverse function so you can pass an extra argument host :

from django.shortcuts import render from django_hosts.resolvers import reverse def homepage(request): homepage_url = reverse('homepage', host='www') return render(request, 'homepage.html', {'homepage_url': homepage_url})

Further Reading

You can learn more by reading the Official Documentation or browsing the code on GitHub .

本文开发(python)相关术语:python基础教程 python多线程 web开发工程师 软件开发工程师 软件开发流程

主题: DjangoLinuxWindowsGitGitHubAU
分页:12
转载请注明
本文标题:Package of the Week: django-hosts
本站链接:http://www.codesec.net/view/481577.html
分享请点击:


1.凡CodeSecTeam转载的文章,均出自其它媒体或其他官网介绍,目的在于传递更多的信息,并不代表本站赞同其观点和其真实性负责;
2.转载的文章仅代表原创作者观点,与本站无关。其原创性以及文中陈述文字和内容未经本站证实,本站对该文以及其中全部或者部分内容、文字的真实性、完整性、及时性,不作出任何保证或承若;
3.如本站转载稿涉及版权等问题,请作者及时联系本站,我们会及时处理。
登录后可拥有收藏文章、关注作者等权限...
技术大类 技术大类 | 开发(python) | 评论(0) | 阅读(41)