在django网络应用程序中,你如何给用户自己的子域? [英] In a django web application, how do you give users their own subdomain?

查看:127
本文介绍了在django网络应用程序中,你如何给用户自己的子域?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Django和Pinax开始一个新的Web应用程序。我想能够给我的用户独特的域名,如Wordpress和其他网站: username.wordpress.com 。我不知道如何使用Django来处理这个问题,因为url解析逻辑(在urls.py中)以域名之后的URL开头。



更具体地说,会有多组用户,每组有一个唯一的名字。不知道有什么不同,但是我以为我应该提到这个。



有没有办法我可以操纵http请求,以便URL看起来像Django,就像网址类似于 www.domain.com/groupname ,但仍在浏览器地址栏中显示为 groupname.domain.com

解决方案

您可以使用一些自定义中间件来拦截请求并从中获取子域。以下代码将检索子域并通过将命名为url



将其放在应用程序的middleware.py文件中。



确保您在settings.py文件中设置了中间件。



确保您在urls.py


$ b $中命名了您的视图从django.http导入httpResponseRedirect
从django中获取

  b 

middleware.py .core.urlresolvers import reverse
import re

subdomain_pattern = re.compile('(?P< subdomain>。*?)\ .. *?')

class SubdomainMiddleware(object):
def process_request(self,request):
match = subdomain_pattern.match(request.get_host())
subdomain = match.group('subdomain' )
redirect_url = reverse('groups_detail',args = [subdomain])
return HttpResponseRedirect(redir ect_url)

urls.py

  from django.conf.urls.defaults import * 

urlpatterns = patterns('',
url(r'^ groups /(?P<name>.+)/$','groups.views.detail',{},name ='group_detail'),

注意:此代码未经测试。



重定向可以更改URL的出现。如果你想避免这种情况,只需调用相关视图,捕获其结果,并将其返回到 HttpResponse()


I'm starting a new web app project using Django and Pinax. I want to be able to give my users unique domain names like Wordpress and other sites do : username.wordpress.com. I'm not sure how to approach this with Django, since the url parsing logic (in urls.py) starts with the url AFTER the domain name.

More specifically, there will be multiple groups of users, each group having a unique name. Not sure that makes a difference, but I thought I should mention that.

Is there some way I can manipulate the http request so that the URL looks to Django as if the url were something like www.domain.com/groupname, but still showed in the browser address bar as groupname.domain.com?

解决方案

You can use some custom middleware to intercept the request and get the subdomain from it. The following code will retrieve the subdomain and redirect to a view by reversing the named url.

Put it in a middleware.py file in your app.

Make sure you set up the middleware in your settings.py file.

Make sure you've named your view in urls.py

middleware.py

from django.http import HttpResponseRedirect
from django.core.urlresolvers import reverse
import re

subdomain_pattern = re.compile('(?P<subdomain>.*?)\..*?')

class SubdomainMiddleware(object):
    def process_request(self, request):
        match = subdomain_pattern.match(request.get_host())
        subdomain = match.group('subdomain')
        redirect_url = reverse('groups_detail', args=[subdomain])
        return HttpResponseRedirect(redirect_url)

urls.py

from django.conf.urls.defaults import *

urlpatterns = patterns('',
    url(r'^groups/(?P<name>.+)/$', 'groups.views.detail', {}, name='group_detail'),
)

Note: this code is untested.

Redirecting can alter the URL's appearance. If you want to avoid this, simply call the associated view, capture its result, and return it in an HttpResponse().

这篇关于在django网络应用程序中,你如何给用户自己的子域?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆