0

I am recently working on Django following the description from the book Python Crash Course. I followed every step and tried to restart from the beginning a couple of times, still unable to solve the problem(Even reinstalled MacOS to get a cleaner desktop). Here is my code:


class Topic(models.Model):
    '''A topic the user is learning about'''
    text = models.CharField(max_length=200)
    date_added = models.DateTimeField(auto_now_add=True)

    def __str__(self):
        '''Return a string represent model'''
        return self.text

The tracebacks are below:

Traceback (most recent call last):
  File "/Users/bryan/Desktop/python/learning_log/learning_logs/models.py", line 3, in <module>
    class Topic(models.Model):
  File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages/django/db/models/base.py", line 108, in __new__
    app_config = apps.get_containing_app_config(module)
  File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages/django/apps/registry.py", line 253, in get_containing_app_config
    self.check_apps_ready()
  File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages/django/apps/registry.py", line 135, in check_apps_ready
    settings.INSTALLED_APPS
  File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages/django/conf/__init__.py", line 82, in __getattr__
    self._setup(name)
  File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages/django/conf/__init__.py", line 63, in _setup
    raise ImproperlyConfigured(
django.core.exceptions.ImproperlyConfigured: Requested setting INSTALLED_APPS, but settings are not configured. You must either define the environment variable DJANGO_SETTINGS_MODULE or call settings.configure() before accessing settings.
[Finished in 304ms with exit code 1]
[cmd: ['python3', '-u', '/Users/bryan/Desktop/python/learning_log/learning_logs/models.py']]
[dir: /Users/bryan/Desktop/python/learning_log/learning_logs]
[path: /Library/Frameworks/Python.framework/Versions/3.10/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin]

Edit: The code is in the file models.py

peterpan
  • 11
  • 4
  • When does this error occur? When you try to import `Topic`? When you try to apply migrations? Please provide a little more information – Mitchell van Zuylen Oct 27 '21 at 00:13
  • See where it says `django.core.exceptions.ImproperlyConfigured: Requested setting INSTALLED_APPS, but settings are not configured. You must either define the environment variable DJANGO_SETTINGS_MODULE or call settings.configure() before accessing settings.`? Did you try doing those things? What happened when you tried doing those things? – Karl Knechtel Oct 27 '21 at 00:15
  • 1
    See https://stackoverflow.com/questions/26082128/improperlyconfigured-you-must-either-define-the-environment-variable-django-set this post in relation to actual error. This will likely have the answer. – Mitchell van Zuylen Oct 27 '21 at 00:15
  • try this one https://stackoverflow.com/questions/8826534/how-can-i-correctly-set-django-settings-module-for-my-django-project-i-am-using – vladthelad Oct 27 '21 at 00:43
  • Hi @Mitchell van Zuylen, the error occurs when I try to run the code in SublimeText. – peterpan Oct 27 '21 at 06:08
  • Hello @KarlKnechtel, thanks for the suggestion. I have not tried to do those things because honestly, I do know how to... Just started learning programming from almost the beginning. – peterpan Oct 27 '21 at 06:14
  • Thanks a lot for the relating post, @Mitchell van Zuylen. I have checked out but I do not think I understand how to solve it. Specifically how to `export DJANGO_SETTINGS_MODULE=mysite.settings`, may I know a little more information about that? Sorry, I am new to programming. – peterpan Oct 27 '21 at 06:18
  • SublimeText is an editor. When you are running python, it uses a python kernel (In your case Python3. Usually this will be in a terminal. The terminal will also be where the error you posted will be located. The kernel needs to have access to the settings file. Can you add `settings.configure()` at the top of the script you are trying to run? – Mitchell van Zuylen Oct 27 '21 at 06:59
  • "I have not tried to do those things because honestly, I do know how to" So... look them up? You can literally put `django settings.configure` into a search engine; the first result I got was [this previous Stack Overflow question](https://stackoverflow.com/questions/19855160/django-where-do-i-call-settings-configure). For setting environment variables, you have a "how do I use my computer?" question, not a programming question anyway. – Karl Knechtel Oct 27 '21 at 12:14
  • "Specifically how to `export DJANGO_SETTINGS_MODULE=mysite.settings`" Do you know what a command line is? What operating system are you using? – Karl Knechtel Oct 27 '21 at 12:15
  • Hello @MitchellvanZuylen, thanks a lot for the reply! I think I have figured out where the problem is. I tried to run models.py file directly in SublimeText which made the error. It turns out that when working with Django we barely run .py files directly as Mr.Japhyr mentioned. – peterpan Oct 27 '21 at 22:05
  • Hi @KarlKnechtel, sorry if I made any stupid questions. I have been using computers for many years. Just been recently learning to program and found out it is fascinating. But there is a lot I do not quite understand especially when problems occur. I will make a note of your words and try to do a search next time and post more programming-related questions. Anyway, the positive side is this problem has been sorted out with help from the community! I am sorry again if I made you any trouble and thanks for your suggestions! :) – peterpan Oct 27 '21 at 22:19

2 Answers2

0

There is small mistake you are trying to access the text in __str__ method and due to which it failed in order to access the text you need to change the code like self.text

def __str__(self):
    '''Return a string represent model'''
    return self.text
Shreeyansh Jain
  • 1,407
  • 15
  • 25
  • You are right. How negligent I was. But the problem is still there after I changed text to self.text – peterpan Oct 27 '21 at 06:02
0

It seems like the error is happening because you're running the models.py file directly. In working with Django, you rarely run a .py file directly. Usually you run some commands in the terminal, and then you interact with your project in a browser.

  • Are you working in an active virtual environment?
  • Have you run the makemigrations command for your app?
  • Have you run python manage.py migrate?
  • What happens when you run python manage.py runserver?
japhyr
  • 1,710
  • 2
  • 18
  • 24
  • Thank you a lot @japhyr! The problem has been sorted out. As you mentioned, I did run models.py directly in SublimeText which made the error and I have figured out we should use terminals rather than run the files directly in the editor when working with Django. After I modified the models.py and admin.py files, I `makemigration` and `migrate` the manage.py in the terminal. When I checked localhost/8000 everything works great! – peterpan Oct 27 '21 at 22:10