创建 2 个共享 90% 数据和代码的 django 站点
我有两个密切相关的网站,一个主网站和一个移动网站,作为 django 应用程序托管。他们将具有许多相同的功能并且需要访问相同的数据。主要区别在于模板不同,网站的结构方式也不同。
我有两个独立的虚拟主机,每个虚拟主机一个(尽管我不必这样做)。我的第一个想法是 Django 站点框架有助于解决这个问题,但文档似乎没有描述我的用例。
有人可以给我一个提示,让我知道我是否走在正确的轨道上吗? urls.py 需要不同,因为例如应用程序之间的主页完全不同。主要目标是共享两个不同应用程序中的数据以及不需要重复的管理代码。
从主站点:
- 用户提交存储在模型中的项目
从移动站点:
- 用户查看项目列表并查看刚刚在主站点上输入的项目
- 用户对最近添加的项目给出 5 星评级
从主站点site:
- 用户查看高评价项目的列表,并且最近添加的项目(现在具有高评价)显示在列表中。
I have two closely related sites, a main site and a mobile site, hosted as a django app. They'll have a lot of the same functionality and need to access the same data. The main difference is the templates will be different and the way the site is structured will be different.
I have two separate virtual hosts, one for each (though I don't have to do it that way). My first thought was that the Django sites framework helps to solve this, but the docs don't seem to describe my use case.
Can someone give me a hint to know if I'm on the right track? The urls.py will need to be different since, for example, the homepage is completely different between the apps. The main goal is that for the data in the two different apps to be shared and the code to manage that does not need to be duplicated.
From the main site:
- User submits an item that is stored in the model
From the mobile site:
- User views a list of items and see the one just entered on the main site
- User gives a 5 star rating on the recently added item
From the main site:
- User views a list of highly rated items and the recently added item (which now has a high rating) shows up on the list.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
查看类似问题的答案。基本上,您可以使用相同的视图,并根据用户代理返回不同的模板。
另外,如果您构建应用程序逻辑,以便将其分解为 django 术语中的不同“应用程序”,那么如果您需要具有相似组件的不同流程,则可以重复使用它们。
希望这能让你开始跑步。
更新:
假设您有一个主网站 http://www.mainsite.com/ 其中有自己的 urls.py models.py 和views.py 使您的主站点功能可用。然后你有 http://www.m.mainsite.com/ 它有自己的一套网址和视图。然后,您可以导入主站点的模型并在移动站点视图中使用它们。
Have a look at this answer to a similar question. Basically you can just use the same views and just return different templates based on the user-agent.
Also, if you structure your application logic so that it is broken up into different "apps" in django terms, then you can re-use them if you need different flows with similar components.
Hopefully this gets you off and running.
UPDATE:
So lets say you have your main site http://www.mainsite.com/ which has it's own urls.py models.py and views.py that makes your functionality for the main site. Then you have http://www.m.mainsite.com/ which has it's own set of urls, and views. Then you can just import the main site's models and use them in the mobile sites views.
好的,这两个答案都很好,并且对我选择的最终解决方案做出了贡献。
在settings.py 文件中有一个名为ROOT_URLCONF 的选项。我创建了两个名为 settings_desktop.py 和 settings_mobile.py 的 settings.py 文件,并且在每个文件中都使用了以下代码:(
或者在桌面的情况下为 myapp.urls_desktop)
这实际上提供了很多很酷的功能,例如能够为每个站点使用不同的模板目录,尽管我实际上不会这样做。
然后我创建了两个版本的 wsgi 文件,其中唯一的区别是这一行:
或者
在每个虚拟主机中,唯一的区别是 WSGIScriptAlias 行,它指向每个主机的不同 wsgi 文件。
这使我能够有效地使用一个可以轻松容纳两个站点的 django 应用程序。
感谢您帮助制定一个好的解决方案。
OK, both answers are great and contributed to what I chose for my final solution.
In the settings.py file there is an option called ROOT_URLCONF. I created two settings.py files, called settings_desktop.py and settings_mobile.py and in each of these used the following code:
(or in the case of the desktop, myapp.urls_desktop)
This actually gives a lot of cool features such as being able to use different template directories for each site, though really I'm not going to do that.
Then I created two versions of the wsgi file where the only difference was this line:
or
In each of the virtual hosts the only difference is the WSGIScriptAlias line that points to the different wsgi file for each host.
This allowed me to effectively use one django app that could easily accommodate both sites.
Thanks for helping work out a good solution to this.
我曾经做过类似的事情。我解决多个 urls.py 问题的方法是这样的:
创建两个 urlconf,每个站点一个;
创建一个新的中间件:
还要检查为什么必须执行 patch_vary_headers在文档上。
I did something very similar once. My way of solving this problem of multiple urls.py was something like this:
Create two urlconf, one for each site;
Create a new Middleware:
Check also why you have to do the patch_vary_headers on the docs.