microsoftgraph / msgraph-sample-pythondjangoapp

This sample demonstrates how to use the Microsoft Graph .NET SDK to access data in Office 365 from Python web apps.
MIT License
131 stars 63 forks source link

settings.py needs to be updated for templates and static folders #51

Closed natisdale closed 3 years ago

natisdale commented 3 years ago

Where did you get the code?

Describe the bug

A clear and concise description of what the bug is.

Tutorial creates the folders tutorial/templates/tutorial and tutorial/static/tutorial. Instructions for updating settings.py to see content placed in these folders is not included in tutorial steps. This could prevent someone who is new to Django from being able to complete the tutorial.

To Reproduce

Steps to reproduce the behavior:

Follow the first section of the tutorial, 'Create a Python Django Web App'

Expected behavior

Expected home page to load.

Screenshots

django.template.exceptions.TemplateDoesNotExist

Desktop

Dependency versions

Additional context

The following settings will resolve the TemplateDoesNotExist issue:

    TEMPLATES = [
        {
            'BACKEND': 'django.template.backends.django.DjangoTemplates',
            'DIRS': [(BASE_DIR / 'tutorial/templates/'),],
            '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',
                ],
            },
        },
    ]

    STATICFILES_DIRS = [
        (BASE_DIR / "tutorial/static/"),
    ]
jasonjoh commented 3 years ago

Interesting. This may be a change to Django. I did not have to modify settings.py this way. I took a look at their documentation and don't see anything about a change here though.

From https://docs.djangoproject.com/en/3.1/intro/tutorial03/#write-views-that-actually-do-something, it looks like you shouldn't have to do this:

Your project’s TEMPLATES setting describes how Django will load and render templates. The default settings file configures a DjangoTemplates backend whose APP_DIRS option is set to True. By convention DjangoTemplates looks for a “templates” subdirectory in each of the INSTALLED_APPS.

Within the templates directory you have just created, create another directory called polls, and within that create a file called index.html. In other words, your template should be at polls/templates/polls/index.html. Because of how the app_directories template loader works as described above, you can refer to this template within Django as polls/index.html.

With 'APP_DIRS': True, the app should work, since the directory structure is following the app_name/templates/app_name convention. What version of Django are you using?

natisdale commented 3 years ago

Django==3.1.4

Looking at the diff I see that 3.1.4 uses pathlib whereas 3.0.4 used os, to build BASE_DIR.

BASE_DIR = Path(__file__).resolve().parent.parent   

instead of

BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))

Swapping out settings.py with the one in this repo works fine.

jasonjoh commented 3 years ago

Interesting. I have 3.1.4 on my machine, but when it generates a project, settings.py still says it was generated by 3.0.4, and it uses the os.path.dirname approach. It looks like I've got an issue with multiple Python versions on this machine. I'll clean that up and get this straightened out in the tutorial. Thanks for reporting.

jasonjoh commented 3 years ago

I cleaned up my Python installs and started this over with Python 3.9.2 and Django 3.1.7. I did see the following line in settings.py:

BASE_DIR = Path(__file__).resolve().parent.parent

However, I still did not need to modify settings.py as you did. Did you add tutorial in the INSTALLED_APPS section as described in step 6? If I skip that step I also get a TemplateDoesNotExist error.

ghost commented 3 years ago

This issue has been automatically marked as stale because it has been marked as requiring author feedback but has not had any activity for 4 days. It will be closed if no further activity occurs within 3 days of this comment.