0

抱歉,我对 Django 和 Python 很陌生

无论如何,我在 mac 上运行 python 2.7 并尝试在这里创建我的第一个项目。尝试 shell 命令时出现错误python manage.py makemigrations。我之前做了 pip 的升级,pip2 install --upgrade pip否则 Django 无法安装。

所以我不知道这可能是django版本的问题还是......

(mydjangoblog) ninodidoxxx:mydjangoblog antoninodanselmo$ python manage.py makemigrations
Traceback (most recent call last):
  File "manage.py", line 22, in <module>
    execute_from_command_line(sys.argv)
  File "/Users/antoninodanselmo/Desktop/mydjangoblog/lib/python2.7/site-packages/django/core/management/__init__.py", line 364, in execute_from_command_line
    utility.execute()
  File "/Users/antoninodanselmo/Desktop/mydjangoblog/lib/python2.7/site-packages/django/core/management/__init__.py", line 338, in execute
    django.setup()
  File "/Users/antoninodanselmo/Desktop/mydjangoblog/lib/python2.7/site-packages/django/__init__.py", line 27, in setup
    apps.populate(settings.INSTALLED_APPS)
  File "/Users/antoninodanselmo/Desktop/mydjangoblog/lib/python2.7/site-packages/django/apps/registry.py", line 108, in populate
    app_config.import_models()
  File "/Users/antoninodanselmo/Desktop/mydjangoblog/lib/python2.7/site-packages/django/apps/config.py", line 202, in import_models
    self.models_module = import_module(models_module_name)
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/importlib/__init__.py", line 37, in import_module
    __import__(name)
  File "/Users/antoninodanselmo/Desktop/mydjangoblog/mydjangoblog/posts/models.py", line 15
    def__str__(self):
                    ^
SyntaxError: invalid syntax

模型.py

# -*- coding: utf-8 -*-
#from __future__ import unicode_literals

from django.db import models

# Create your models here.

class posts(models.Model):
    titolo = models.CharField(max_length=120)
    contenuto = models.TextField()
    data = models.DataTimeField(auto_now=False, auto_now_add=True)
    slug = models.SlugField()

#python 3
    def__str__(self):
        return self.titolo

#python 2
    def__unicode__(self):
        return self.titolo

设置.py

# Application definition

INSTALLED_APPS = [
    'posts',
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
]

MIDDLEWARE = [
    'django.middleware.security.SecurityMiddleware',
    'django.contrib.sessions.middleware.SessionMiddleware',
    'django.middleware.common.CommonMiddleware',
    'django.middleware.csrf.CsrfViewMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'django.contrib.messages.middleware.MessageMiddleware',
    'django.middleware.clickjacking.XFrameOptionsMiddleware',
]

ROOT_URLCONF = 'mydjangoblog.urls'

TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [],
        'APP_DIRS': True,
        'OPTIONS': {
            'context_processors': [
                'django.template.context_processors.debug',
                'django.template.context_processors.request',
                'django.contrib.auth.context_processors.auth',
                'django.contrib.messages.context_processors.messages',
            ],
        },
    },
]

WSGI_APPLICATION = 'mydjangoblog.wsgi.application'
......

任何帮助,将不胜感激。谢谢!

4

1 回答 1

1

你应该在 and 和 and 之间留def一个__str__空格__unicode__

应该如下图

#python 3
    def __str__(self):
        return self.titolo

#python 2
    def __unicode__(self):
        return self.titolo
于 2018-05-31T17:24:30.360 回答