【问题标题】:Invalid syntax django无效的语法 django
【发布时间】:2021-07-05 11:36:07
【问题描述】:

我正在尝试运行我制作的 django 代码和 imx6 yocto 构建。基本示例进行得很顺利。所以我决定从我正在工作的项目中运行我自己的 django 产品,我得到以下信息:

root@imx6ulevk:/home/mdwb-main# python manage.py runserver 147.106.17.9:8000  
Watching for file changes with StatReloader  
Exception in thread django-main-thread:  
Traceback (most recent call last):  
  File "/usr/lib/python3.5/threading.py", line 914, in _bootstrap_inner  
    self.run()  
  File "/usr/lib/python3.5/threading.py", line 862, in run  
    self._target(*self._args, **self._kwargs)  
  File "/usr/lib/python3.5/site-packages/django/utils/autoreload.py", line 54, in wrapper  
    fn(*args, **kwargs)  
  File "/usr/lib/python3.5/site-packages/django/core/management/commands/runserver.py", line 109, in   inner_run  
    autoreload.raise_last_exception()  
  File "/usr/lib/python3.5/site-packages/django/utils/autoreload.py", line 77, in   raise_last_exception  
    raise _exception[1]  
  File "/usr/lib/python3.5/site-packages/django/core/management/__init__.py", line 337, in execute  
    autoreload.check_errors(django.setup)()  
  File "/usr/lib/python3.5/site-packages/django/utils/autoreload.py", line 54, in wrapper  
    fn(*args, **kwargs)  
  File "/usr/lib/python3.5/site-packages/django/__init__.py", line 24, in setup  
    apps.populate(settings.INSTALLED_APPS)  
  File "/usr/lib/python3.5/site-packages/django/apps/registry.py", line 114, in populate  
    app_config.import_models()  
  File "/usr/lib/python3.5/site-packages/django/apps/config.py", line 211, in import_models  
    self.models_module = import_module(models_module_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 985, in _gcd_import  
  File "<frozen importlib._bootstrap>", line 968, in _find_and_load  
  File "<frozen importlib._bootstrap>", line 957, in _find_and_load_unlocked  
  File "<frozen importlib._bootstrap>", line 673, in _load_unlocked  
  File "<frozen importlib._bootstrap_external>", line 693, in exec_module  
  File "<frozen importlib._bootstrap_external>", line 799, in get_code  
  File "<frozen importlib._bootstrap_external>", line 759, in source_to_code  
  File "<frozen importlib._bootstrap>", line 222, in _call_with_frames_removed  
  File "/home/mdwb-main/api_cloud/models.py", line 15  
    return f'{self.cloud_interval} foi cadastrado com sucesso!'  
                                                              ^  
SyntaxError: invalid syntax  

models.py中的代码如下:

from django.db import models

# Create your models here.
class Initial_config(models.Model):
    #complement = models.CharField(max_length=30,null=True)
    cloud_interval = models.IntegerField()
    device_interval = models.IntegerField()

    class Meta:
        verbose_name = 'config'
        verbose_name_plural = 'configs'

    def __str__(self):
        return f'{self.cloud_interval} foi cadastrado com sucesso!'

我尝试将 ' 更改为 " 删除 {self.cloud_interval},但均未成功。
为什么会发生这种情况?如何解决?

【问题讨论】:

  • f-strings(如f'{self.cloud_interval} ...')在 Python 3.5 中不受支持。这些是在 Python 3.6 中首次引入的。

标签: python django imx6


【解决方案1】:

f-strings 只能从 Python 3.6 开始使用。

如果您想继续使用 Python 3.5,您可以将代码更改为:

def __str__(self):
    return '%s foi cadastrado com sucesso!' % self.cloud_interval

也就是说,Python 3.5 不再受到官方支持;强烈建议升级到 3.6 或更高版本。请参阅https://endoflife.date/python 了解更多信息。

【讨论】:

  • 是的,这很有帮助,我升级了 Python,现在程序运行良好。谢谢很多人
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-06-01
  • 1970-01-01
  • 2017-12-29
  • 2016-01-12
  • 2017-05-07
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多