简体   繁体   English

如何在Django中创建网站的多个版本,但在相同的域但不同的URL下运行

[英]How to create multiple versions of a site in django but run under same domain but different url

I have a website i developed with django and i want it to have 2 different front-end designs but same functionality. 我有一个使用django开发的网站,我希望它具有2种不同的前端设计但功能相同。 something very similar to django multi-language functionality. 与django多语言功能非常相似。 So i want a user to be able to access the two designs from different url by specifying the version ie 所以我希望用户能够通过指定版本从不同的URL访问两个设计,即

localhost:8000/v1 本地主机:8000 / v1

localhost:8000/v2 本地主机:8000 / v2

I created a middleware to check the current version and revert to a default if non is found. 我创建了一个中间件来检查当前版本,如果未找到,请还原为默认值。
middleware 中间件

class VersioningMiddleware(MiddlewareMixin):

def process_request(self, request):
    path = request.get_full_path()
    tokens = path.split("/")
    if len(tokens) > 1:
        if tokens[1] in APP_VERSIONS: # APP_VERSION is a list ['v1','v2',...]
            request.app_version = tokens[1]
    new_url = "v1%s" % path
    return HttpResponseRedirect(new_url)

and also i mapped out all app url conf using this 我也使用这个映射了所有应用程序URL conf

urlpatterns = [
path('admin/', admin.site.urls),
url(r'^(?P<version>[a-z][0-9])?/', include('base.all_urls','')),]

So the problem with this approach is that all my views are expected to take an optional parameter version which i dont like. 因此,这种方法的问题在于,我的所有视图都应采用我不喜欢的可选参数版本

So i need a better way to achieve this without having to have different codebase for different version. 因此,我需要一种更好的方法来实现此目标,而不必为不同版本使用不同的代码库。 If i could pass the version to the view without having to specify the optional parameter to all my views, my plan is to use that version to render the appropriate template that each view render. 如果我可以在无需为所有视图指定可选参数的情况下将版本传递给视图,则我的计划是使用该版本来呈现每个视图呈现的适当模板。

thanks in advance 提前致谢

As you said you need different versions for a frontend only and keep the functionality same. 如您所说,仅前端需要不同的版本,并保持相同的功能。

For the frontend, you just need to change js and css 对于前端,您只需要更改jscss

Add "django.core.context_processors.request" to MIDDLEWARE of your settings.py so that Django template would be able to access query parameters in request across all templates in the whole project 在您的settings.py MIDDLEWARE中添加"django.core.context_processors.request" ,以便Django模板能够在整个项目的所有模板中访问请求中的查询参数

settings.py settings.py

MIDDLEWARE = [
    ...
    ...
    "django.core.context_processors.request",
]

Now in your template file, you will be able to retrieve the query parameter and evaluate directly 现在,在模板文件中,您将能够检索查询参数并直接评估

{% if request.GET.version == 'v2' %} 
    loading version two... write tags, load here css,js to be loaded if version is v2
{% elif request.GET.version == 'v1' %}
    loading version two... write tags, load here css,js to be loaded if version is v1
{% else %}
    loading defaults.. write tags, load default css,js to be loaded if no version queried by user
{% endif %}

your URL would be like: 您的网址如下:

for v2 对于v2

localhost:8000/?version=v2

for v1 对于v1

localhost:8000/?version=v1

if nothing specified - default 如果未指定-默认

localhost:8000

Benefits - 好处-

  • No need to write about this frontend versioning in backend views or urls file 无需在后端视图或url文件中编写有关此前端版本控制的信息

  • Only change in HTML template can allow changing the version of the frontend 只有更改HTML模板,才能允许更改前端的版本

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

相关问题 同一站点下的多个Django应用 - Multiple Django apps under the same site 同一站点,不同的Django项目,通过不同的网址提供服务 - Same site, different django projects, served through different url mod_wsgi:如何在单个域下运行不同的应用程序? - mod_wsgi: How to run different apps under single domain? 设置Apache以使用虚拟主机在同一域上运行多个django应用 - Setting Apache to run multiple django apps on the same domain with virtual hosts 如何在Django中维护同一语言的不同国家/地区版本? - How to maintain different country versions of same language in Django? 同时运行同一程序的多个版本 - Run multiple versions of the same program at the same time 如何具有相同对象的多个版本,每个版本具有不同的属性 - How to have multiple versions of the same object, each with different properties Django缓存2个不同版本的url(JSON和HTML) - Django cache 2 different versions of a url (JSON & HTML) Django,有可能运行两个不同的版本? - Django, is possible to run two different versions? 如何使用相同的域在 NGINX 服务器上运行 django 和 wordpress? - How to run django and wordpress on NGINX server using same domain?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM