Python Forum

Full Version: Writing my first Django app
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hey Guys,

I am following along Django official documentation for learning Django. I am now in Part 2 (Writing your first Django app)

I have made some changes to polls/models.py like this

from django.db import models

class Question(models.Model):
    # ...
    def __str__(self):
        return self.question_text
    def was_published_recently(self):
        return self.pub_date >= timezone.now() - datetime.timedelta(days=1)


class Choice(models.Model):
    # ...
    def __str__(self):
        return self.choice_text

But when I run this command:

[python]
python manage.py makemigrations
[\python]

I am getting an error in command line like this:

Error:
Traceback (most recent call last): File "manage.py", line 21, in <module> main() File "manage.py", line 17, in main execute_from_command_line(sys.argv) File "C:\dev\polling-app\myenv\lib\site-packages\django\core\management\__init__.py", line 381, in execute_from_command_line utility.execute() File "C:\dev\polling-app\myenv\lib\site-packages\django\core\management\__init__.py", line 357, in execute django.setup() File "C:\dev\polling-app\myenv\lib\site-packages\django\__init__.py", line 24, in setup apps.populate(settings.INSTALLED_APPS) File "C:\dev\polling-app\myenv\lib\site-packages\django\apps\registry.py", line 114, in populate app_config.import_models() File "C:\dev\polling-app\myenv\lib\site-packages\django\apps\config.py", line 211, in import_models self.models_module = import_module(models_module_name) File "C:\dev\polling-app\myenv\lib\importlib\__init__.py", line 127, in import_module return _bootstrap._gcd_import(name[level:], package, level) File "<frozen importlib._bootstrap>", line 1006, in _gcd_import File "<frozen importlib._bootstrap>", line 983, in _find_and_load File "<frozen importlib._bootstrap>", line 967, in _find_and_load_unlocked File "<frozen importlib._bootstrap>", line 677, in _load_unlocked File "<frozen importlib._bootstrap_external>", line 724, in exec_module File "<frozen importlib._bootstrap_external>", line 860, in get_code File "<frozen importlib._bootstrap_external>", line 791, in source_to_code File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed File "C:\dev\polling-app\mysite\polls\models.py", line 17 return self.choice_text ^ SyntaxError: 'return' outside function
Can somebody help me figure this error out?
Make sure you didn't mix TABS and SPACES.
Show your whole module.
Thanks for the reply, fishhook.

Here's my models.py file:

from django.db import models

# Create your models here.
class Question(models.Model):
    question_text = models.CharField(max_length=200)
    pub_date = models.DateTimeField('date published')

    def __str__(self):
        return self.question_text

    def was_published_recently(self):
        return self.pub_date >= timezone.now() - datetime.timedelta(days=1)

class Choice (models.Model):
    question = models.ForeignKey(Question, on_delete=models.CASCADE)
    choice_text = models.CharField(max_length = 200)
    votes = models.IntegerField(default=0)

    class __str__(self):
        return self.choice_text
    
    class __str__(self):
        return self.choice_text
Class definition is not suitable here, you probably wanted to write def instead.
Thank you so much fishhook.
I got it.
Sunil