简体   繁体   English

Docker Compose和Django生成器语法错误

[英]Docker Compose and Django generator syntax error

I'm new to Docker and have been trying to set up Django with Docker. 我是Docker的新手,并一直试图用Docker设置Django。 I've been following the instructions here but am running into the error down below. 我一直在按照这里的说明进行操作但是我在下面遇到了错误。

  File "/usr/local/lib/python3.7/site- 
  packages/django/contrib/admin/widgets.py", line 152
  web_1  |     '%s=%s' % (k, v) for k, v in params.items(),
  web_1  |     ^
  web_1  | SyntaxError: Generator expression must be parenthesized

My Django version is 2.1, Python 3.7. 我的Django版本是2.1,Python 3.7。 As far as I know, this shouldn't be happening, yet it is. 据我所知,这不应该发生,但确实如此。 I have checked the file in question, and it is written correctly. 我检查了有问题的文件,并且写得正确。 It was a fix that was put into place that should have fixed this error in the past with the latest version of Python. 这是一个修复程序,应该在过去使用最新版本的Python修复此错误。

EDIT Adding Docker file here: 编辑在这里添加Docker文件:

 FROM python:3
 ENV PYTHONUNBUFFERED 1
 RUN mkdir /code
 WORKDIR /code
 ADD requirements.txt /code/
 RUN pip install -r requirements.txt
 ADD . /code/

docker-compose.yml file: docker-compose.yml文件:

version: '3'

services:
  db:
    image: postgres
  web:
    build: .
    command: python3 manage.py runserver 0.0.0.0:8000
    volumes:
      - .:/code
    ports:
      - "8000:8000"
    depends_on:
      - db

From widgets.py: 来自widgets.py:

if params:
            related_url += '?' + '&'.join('%s=%s' % (k, v) for k, v in params.items())

This is the entire definition form widgets.py: 这是widgets.py的整个定义形式:

def get_context(self, name, value, attrs):
    context = super().get_context(name, value, attrs)
    rel_to = self.rel.model
    if rel_to in self.admin_site._registry:
        # The related object is registered with the same AdminSite
        related_url = reverse(
            'admin:%s_%s_changelist' % (
                rel_to._meta.app_label,
                rel_to._meta.model_name,
            ),
            current_app=self.admin_site.name,
        )

        params = self.url_parameters()
        if params:
            related_url += '?' + '&'.join('%s=%s' % (k, v) for k, v in params.items())
        context['related_url'] = mark_safe(related_url)
        context['link_title'] = _('Lookup')
        # The JavaScript code looks for this class.
        context['widget']['attrs'].setdefault('class', 'vForeignKeyRawIdAdminField')
    if context['widget']['value']:
        context['link_label'], context['link_url'] = self.label_and_url_for_value(value)
    return context

However, I still get the same error code when trying to compose-up. 但是,在尝试编写时,我仍然会得到相同的错误代码。

I have no idea what to do at this point; 我不知道该做什么; I'm at a complete loss. 我完全失去了。 This is the first time I've run into this problem. 这是我第一次遇到这个问题。

First of all 首先

Your Django version is not compatible with Python 3.7 您的Django版本与Python 3.7不兼容

So make these changes 所以做出这些改变

In your Dockerfile 在你的Dockerfile中

FROM python:3.6 

Restart docker using sudo service docker restart 使用sudo service docker restart

then sudo docker-compose up --build 然后sudo docker-compose up --build

or sudo docker-compose run web python manage.py migrate and sudo docker-compose up --build 或者sudo docker-compose run web python manage.py migratesudo docker-compose up --build

It seems that you are trying to run sample from byob-profiles-rest-api . 您似乎正在尝试从byob-profiles-rest-api运行示例。

  • This example is pretty old and you have to down grade your versions: 这个例子已经过时了,您必须对您的版本进行评分:

Change your Django version to 1.11 to avoid error 将Django版本更改为1.11以避免错误

ModuleNotFoundError: No module named 'django.core.urlresolvers' ModuleNotFoundError:没有名为'django.core.urlresolvers'的模块

Change your Python version to 3.6 将Python版本更改为3.6

SyntaxError: Generator expression must be parenthesized SyntaxError:Generator表达式必须带括号

  • You files should now look like this 你现在的文件应该是这样的

Dockerfile Dockerfile

FROM python:3.6

ENV PYTHONBUFFERED 1
RUN mkdir /code
WORKDIR /code
COPY . /code/
RUN pip install -r requirements.txt

requirements.txt requirements.txt

appdirs==1.4.3
Django==1.11
djangorestframework==3.6.2
packaging==16.8
pyparsing==2.2.0
pytz==2017.2
six==1.10.0
  • Delete the old image of byob-profiles-rest-api 删除byob-profiles-rest-api的旧图像
  • Recreate the image by running 通过运行重新创建图像

docker-compose run web python src/profiles_project/manage.py migrate docker-compose运行web python src / profiles_project / manage.py migrate

You should see that there's no more errors. 你应该看到没有更多的错误。

Running migrations:    
Applying contenttypes.0001_initial... OK   
Applying contenttypes.0002_remove_content_type_name... OK   
Applying auth.0001_initial... OK   
Applying auth.0002_alter_permission_name_max_length... OK   
Applying auth.0003_alter_user_email_max_length... OK   
Applying auth.0004_alter_user_username_opts... OK   
Applying auth.0005_alter_user_last_login_null... OK   
Applying auth.0006_require_contenttypes_0002... OK   
Applying auth.0007_alter_validators_add_error_messages... OK   
Applying auth.0008_alter_user_username_max_length... OK   
Applying profiles_api.0001_initial... OK   
Applying admin.0001_initial... OK   
Applying admin.0002_logentry_remove_auto_add... OK   
Applying authtoken.0001_initial... OK   
Applying authtoken.0002_auto_20160226_1747... OK   
Applying profiles_api.0002_profilefeeditem... OK   
Applying sessions.0001_initial... OK
  • Build and start the server 构建并启动服务器

sudo docker-compose up --build sudo docker-compose up --build

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM