Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
54 views
in Technique[技术] by (71.8m points)

python - Can a django url contain a fixed path at the beginning?

I have a server running a django application. This version is stable. For testing purposes I want to host a testing version of the same application on the server in parallel. Both applications have the similar views.

Normally you could do this with subdomains, so the stable version runs at myserver.de and the testing version at test.myserver.de. This is not possible for me, I have only one available domain (myserver.de).

So I can only use paths. Let's say I host the stable version at myserver.de/[apps urls] and the test version at myserver.de/test/[apps urls]. To achieve this, i could edit all urls in the testing version. But that would defeat the purpose of testing, because to then make a testing version stable, i would have to re-edit all urls.

So I would like to simply edit settings.py and put ALLOWED_HOSTS = ["mysite.de/test"] for the testing version. I tested this locally with ALLOWED_HOSTS = ["127.0.0.1:8000/test", "127.0.0.1/test"], but that unsurprisingly didn't work (I read the docs ^^)

Is there a way to run two applications on a django server where the urls of one app always start with /test/<rest> without editing all urls? Could I do this with apache2 instead? (My guess was no, because django builds all links in the templates, so it must know about being "inside" /test.

question from:https://stackoverflow.com/questions/65831067/can-a-django-url-contain-a-fixed-path-at-the-beginning

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

How about just including the urls like so:

stable_urls = [
    ...
]
testing_urls = [
    ...
]

urlpatterns = [
    path('', include(stable_urls)),
    path('test/', include(testing_urls)),
]

Then later you could simply rename, when you want to change versions.
Edit: If I understand your problem correctly then you can do something like this have an empty app (django app) which is the main app of the django project. Now in this apps urls:

urlpatterns = [
    path('', include('app_version_stable.urls')),
    path('test/', include('app_version_testing.urls')),
]

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...