我要更改urls.py:
urlpatterns = [path('rmt / {}'。format(url),view)]
从传递给manage.py的参数
class InitializeService():
def __init__(self):
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'RMT_Django.settings')
try:
from django.core.management import execute_from_command_line
except ImportError as exc:
raise ImportError(
"Couldn't import Django. Are you sure it's installed and "
"available on your PYTHONPATH environment variable? Did you "
"forget to activate a virtual environment?"
) from exc
flag_env = sys.argv[6]
if flag_env == "DEV":
url = "/rmt/push_dev"
elif flag_env == "TEST":
url = "/rmt/push_test"
elif flag_env == "PROD":
url = "/rmt/push"
execute_from_command_line(sys.argv[0:3])
InitializeService()
如何将url传递给urlpattern?
谢谢
答案 0 :(得分:1)
我认为这不是正确的方法。
views.py
中进行# urls.py
urlpatterns = [
path('rmt/<str:service_url>/'.format(url), handeller_view)
]
# views.py
flag_env = sys.argv[6]
service_mapping = {'DEV': 'push_dev', 'TEST': 'push_test', 'PROD': 'push'}
def handeller_view(request, service_url):
if service_url == service_mapping[flag_env]:
# handel your request accordingly
raise Http404()
urls.py
中进行或者,您可以根据args
来匹配URL。
# urls.py
flag_env = sys.argv[6]
if flag_env == "DEV":
url = "/rmt/push_dev"
elif flag_env == "TEST":
url = "/rmt/push_test"
elif flag_env == "PROD":
url = "/rmt/push"
urlpatterns = [
path(url, view),
]
或者您可以根据您的标志创建一个URL模式,如果根据您的标志有多个URL,这将很有用。
# urls.py
flag_env = sys.argv[6]
if flag_env == "DEV":
urlpatterns = [
path("/rmt/push_dev", view),
]
elif flag_env == "TEST":
urlpatterns = [
path("/rmt/push_test", view),
]
elif flag_env == "PROD":
urlpatterns = [
path("/rmt/push", view),
]