Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Writing my first Django app
#1
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?
Reply
#2
Make sure you didn't mix TABS and SPACES.
Show your whole module.
Reply
#3
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
    
Reply
#4
    class __str__(self):
        return self.choice_text
Class definition is not suitable here, you probably wanted to write def instead.
Reply
#5
Thank you so much fishhook.
I got it.
Sunil
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Django: How to automatically substitute a variable in the admin page at Django 1.11? m0ntecr1st0 3 3,303 Jun-30-2019, 12:21 AM
Last Post: scidam

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020