django tutorial

Hi all, I have 2 questions:

  1. is there a gitlab repository with the codes for the official django tutorial?
  2. linked to question 1: I have arrived to the 5th section of the tutorial (about testing) and I think I have used exactly the same codes shown, with exact same folder structure.
    However, when running python manage.py test polls I get the following:
ERROR: mysite.polls (unittest.loader._FailedTest)
----------------------------------------------------------------------
ImportError: Failed to import test module: mysite.polls
Traceback (most recent call last):
  File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/unittest/loader.py", line 470, in _find_test_path
    package = self._get_module_from_name(name)
  File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/unittest/loader.py", line 377, in _get_module_from_name
    __import__(name)
ModuleNotFoundError: No module named 'mysite.polls'

anybody had a similar issue?
Thanks a lot

Generally speaking, these types of errors are frequently caused by something missed or mistyped.

Which step are you at in the tutorial?

Please post a complete description of the directory structure of your project, along with your INSTALLED_APPS setting and the apps.py file in your polls app.
(Copy/paste the contents - don’t post images or retype it.)

Hi, thanks for the reply. I am in step 5 (testing your application). My project directory structure is the following:

DjangoProject/
    main.py
    mysite/
        db.sqlite3
        __init__.py
        manage.py
        mysite/
            asgi.py
            __init__.py
            settings.py
            urls.py
            wsgi.py
        polls/
            models.py
            __init__.py
            apps.py
            admin.py
            tests.py
            urls.py
            views.py
            migrations/
                __init__.py
                0001_initial.py
            templates/
                polls/
                    index.html
                    results.html
                    detail.html

My INSTALLED_APPS are the following:

INSTALLED_APPS = [
    'polls.apps.PollsConfig',
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
]

The content of the apps.py file is the following:

from django.apps import AppConfig
class PollsConfig(AppConfig):
    default_auto_field = 'django.db.models.BigAutoField'
    name = 'polls'

I also add that all the rest of the app works correctly, the only issue I am having is with this testing section.

This all looks good. Please post the contents of your tests.py file.

here it is

import datetime

from django.test import TestCase
from django.utils import timezone

from .models import Question


class QuestionModelTests(TestCase):

    def test_was_published_recently_with_future_question(self):
        """
        was_published_recently() returns False for questions whose pub_date
        is in the future.
        """
        time = timezone.now() + datetime.timedelta(days=30)
        future_question = Question(pub_date=time)
        self.assertIs(future_question.was_published_recently(), False)

thanks for the help

I’m a little curious about your directory structure - what is this “main.py” file? And why do you have an __init__.py file in your project’s base directory? (Is it an empty file?)

In the tutorial, your “outer” mysite directory would be the project root, and it would be the current directory when you run manage.py. Is that the directory you are running this from?

The main.py file has this inside, Django or Pycharm created it by itself.


# This is a sample Python script.

# Press ⇧F10 to execute it or replace it with your code.
# Press Double ⇧ to search everywhere for classes, files, tool windows, actions, and settings.


def print_hi(name):
    # Use a breakpoint in the code line below to debug your script.
    print(f'Hi, {name}')  # Press ⌘F8 to toggle the breakpoint.


# Press the green button in the gutter to run the script.
if __name__ == '__main__':
    print_hi('PyCharm')

# See PyCharm help at https://www.jetbrains.com/help/pycharm/

The __init__.py file is created automatically by pycharm, with nothing inside.

I think the tutorial intentionally thought there was an outer folder as it says:
" From the command line, cd into a directory where you’d like to store your code, then run the following command"… This will create a mysite directory in your current directory. So the “Current directory” is the DjangoProject, then I cd to mysite, also following the tutorial, where it says:
“Let’s verify your Django project works. Change into the outer mysite directory, if you haven’t already, and run the following commands:”

Yes, but that outer directory (DjangoProject) is not part of your project. It would only be a container for all the projects that you might create using Django. The top-most (outermost) directory of your project is the first mysite directory. There should not be an __init__.py file in that directory.

So try removing that file, make sure that the outer mysite directory is your current directory, and try running the manage.py command again.

great it seems it works now, wouldn’t have thought about it
thanks