我试图将我的项目加载到生产服务器上,对于在测试环境中可以正常工作的某些东西,我遇到以下语法错误,这有什么帮助吗?
我看不到为什么出现错误
f'accounts / emails / {template} .html',上下文)^ SyntaxError:语法无效
这是代码
def send_mail(to, template, context):
html_content = render_to_string(f'accounts/emails/{template}.html', context)
text_content = render_to_string(f'accounts/emails/{template}.txt', context)
错误回溯
Unhandled exception in thread started by <function check_errors.<locals>.wrapper at 0x7f00e7044158>
Traceback (most recent call last):
File "/usr/local/lib/python3.5/dist-packages/django/utils/autoreload.py", line 225, in wrapper
fn(*args, **kwargs)
File "/usr/local/lib/python3.5/dist-packages/django/core/management/commands/runserver.py", line 117, in inner_run
self.check(display_num_errors=True)
File "/usr/local/lib/python3.5/dist-packages/django/core/management/base.py", line 379, in check
include_deployment_checks=include_deployment_checks,
File "/usr/local/lib/python3.5/dist-packages/django/core/management/base.py", line 366, in _run_checks
return checks.run_checks(**kwargs)
File "/usr/local/lib/python3.5/dist-packages/django/core/checks/registry.py", line 71, in run_checks
new_errors = check(app_configs=app_configs)
File "/usr/local/lib/python3.5/dist-packages/django/core/checks/urls.py", line 13, in check_url_config
return check_resolver(resolver)
File "/usr/local/lib/python3.5/dist-packages/django/core/checks/urls.py", line 23, in check_resolver
return check_method()
File "/usr/local/lib/python3.5/dist-packages/django/urls/resolvers.py", line 396, in check
for pattern in self.url_patterns:
File "/usr/local/lib/python3.5/dist-packages/django/utils/functional.py", line 37, in __get__
res = instance.__dict__[self.name] = self.func(instance)
File "/usr/local/lib/python3.5/dist-packages/django/urls/resolvers.py", line 533, in url_patterns
patterns = getattr(self.urlconf_module, "urlpatterns", self.urlconf_module)
File "/usr/local/lib/python3.5/dist-packages/django/utils/functional.py", line 37, in __get__
res = instance.__dict__[self.name] = self.func(instance)
File "/usr/local/lib/python3.5/dist-packages/django/urls/resolvers.py", line 526, in urlconf_module
return import_module(self.urlconf_name)
File "/usr/lib/python3.5/importlib/__init__.py", line 126, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "<frozen importlib._bootstrap>", line 986, in _gcd_import
File "<frozen importlib._bootstrap>", line 969, in _find_and_load
File "<frozen importlib._bootstrap>", line 958, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 673, in _load_unlocked
File "<frozen importlib._bootstrap_external>", line 665, in exec_module
File "<frozen importlib._bootstrap>", line 222, in _call_with_frames_removed
File "/home/grouped/teroexamplecom/teroexamplecom/urls.py", line 21, in <module>
url(r'^', include('toys.urls')),
File "/usr/local/lib/python3.5/dist-packages/django/urls/conf.py", line 34, in include
urlconf_module = import_module(urlconf_module)
File "/usr/lib/python3.5/importlib/__init__.py", line 126, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "<frozen importlib._bootstrap>", line 986, in _gcd_import
File "<frozen importlib._bootstrap>", line 969, in _find_and_load
File "<frozen importlib._bootstrap>", line 958, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 673, in _load_unlocked
File "<frozen importlib._bootstrap_external>", line 665, in exec_module
File "<frozen importlib._bootstrap>", line 222, in _call_with_frames_removed
File "/home/grouped/teroexamplecom/toys/urls.py", line 4, in <module>
from toys import views as toysviews
File "/home//grouped/toys/views.py", line 100, in <module>
from .utils import (
File "/home//toys/utils.py", line 9
html_content = render_to_string(f'accounts/emails/{template}.html', context)
^
SyntaxError: invalid syntax
答案 0 :(得分:2)
在{strong> Python 3.6 中引入了f-string
符号。在Python3.6以下将不可用。
将python版本升级到Python3.6或更高版本
使用format()
方法
def send_mail(to, template, context):
html_content = render_to_string('accounts/emails/{template}.html'.format(template=template), context)
text_content = render_to_string('accounts/emails/{template}.txt'.format(template=template), context)