Flatpages on localhost

I would like to work with flatpages and so I followed the instructions and installed it correctly and I can see the flatpage in admin and also the sites. As default there is an example.com site and if you choose that and you want to view the page it takes you to example site with the url of your page you created

How can I view the page on my local machine and localhost because http://localhost:8000/about/ does not work and I cannot see the about page I created in admin?

We’d probably need to see at least your settings file (any sensitive information redacted) and your urls.py file(s) associated with your app.

Also, assuming you’re using runserver in a shell session to run your application, it might be helpful to see the output from that as well.

Ok here is the settings.py file

"""
Django settings for learndjango project.

Generated by 'django-admin startproject' using Django 3.0.3.

For more information on this file, see
https://docs.djangoproject.com/en/3.0/topics/settings/

For the full list of settings and their values, see
https://docs.djangoproject.com/en/3.0/ref/settings/
"""

import os

# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))


# Quick-start development settings - unsuitable for production
# See https://docs.djangoproject.com/en/3.0/howto/deployment/checklist/

# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = '^82-d++=-5hqd(s5#v+(_bh-^=791lra*eb8!m_lqg9qt^ubdw'

# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = True

ALLOWED_HOSTS = []


# Application definition

INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'django.contrib.sites',
    'django.contrib.flatpages',

    # own apps
    'plugins',
]

MIDDLEWARE = [
    'django.middleware.security.SecurityMiddleware',
    'django.contrib.sessions.middleware.SessionMiddleware',
    'django.middleware.common.CommonMiddleware',
    'django.middleware.csrf.CsrfViewMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'django.contrib.messages.middleware.MessageMiddleware',
    'django.middleware.clickjacking.XFrameOptionsMiddleware',
]

ROOT_URLCONF = 'learndjango.urls'

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

WSGI_APPLICATION = 'learndjango.wsgi.application'

SITE_ID = 1

# Database
# https://docs.djangoproject.com/en/3.0/ref/settings/#databases

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.mysql',
        'NAME': 'trydjango',
        'USER' : 'admin',
        'PASSWORD' : '',
    }
}


# Password validation
# https://docs.djangoproject.com/en/3.0/ref/settings/#auth-password-validators

AUTH_PASSWORD_VALIDATORS = [
    {
        'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator',
    },
    {
        'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator',
    },
    {
        'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator',
    },
    {
        'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator',
    },
]


# Internationalization
# https://docs.djangoproject.com/en/3.0/topics/i18n/

LANGUAGE_CODE = 'en-us'

TIME_ZONE = 'Africa/Johannesburg'

USE_I18N = True

USE_L10N = True

USE_TZ = True


# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/3.0/howto/static-files/

STATIC_URL = '/static/'

Here is the urls.py file

from django.contrib import admin
from django.urls import path, include
from django.contrib.flatpages import views

urlpatterns = [
    path('admin/', admin.site.urls),
    path('/', include('django.contrib.flatpages.urls')),
]
urlpatterns += [
    path('about/', views.flatpage, {'url': '/about/'}, name='about'),
]

Here is the output and it says the template does not exist but I promise you it does

Traceback (most recent call last):
  File "/home/calvin/.local/lib/python3.6/site-packages/django/core/handlers/exception.py", line 34, in inner
    response = get_response(request)
  File "/home/calvin/.local/lib/python3.6/site-packages/django/core/handlers/base.py", line 115, in _get_response
    response = self.process_exception_by_middleware(e, request)
  File "/home/calvin/.local/lib/python3.6/site-packages/django/core/handlers/base.py", line 113, in _get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
  File "/home/calvin/.local/lib/python3.6/site-packages/django/contrib/flatpages/views.py", line 45, in flatpage
    return render_flatpage(request, f)
  File "/home/calvin/.local/lib/python3.6/site-packages/django/utils/decorators.py", line 130, in _wrapped_view
    response = view_func(request, *args, **kwargs)
  File "/home/calvin/.local/lib/python3.6/site-packages/django/contrib/flatpages/views.py", line 61, in render_flatpage
    template = loader.get_template(DEFAULT_TEMPLATE)
  File "/home/calvin/.local/lib/python3.6/site-packages/django/template/loader.py", line 19, in get_template
    raise TemplateDoesNotExist(template_name, chain=chain)
django.template.exceptions.TemplateDoesNotExist: flatpages/default.html
[07/May/2020 19:49:46] "GET /about/ HTTP/1.1" 500 84193
Not Found: /favicon.ico

Selection_007

So what this is telling you is that it can’t find the template. You might have that template file, but something isn’t configured correctly for that template file to be found.

In this case, you don’t have this template file in an app of yours under a templates directory (APP_DIRS True), but you have it under a template directory in the root of your project. This means that in your DIRS entry you need to include this root template directory. Frequently, you’ll see it specified as " ‘DIRS’: [os.path.join(BASE_DIR, ‘templates’), ],

(In a couple projects, I have it as simply ‘DIRS’: [‘templates’,] )

See Templates

I am all new to this so excuse my lack of knowledge. I made the change and I still get the same error but with extra things at the bottom
django.template.loaders.filesystem.Loader : /flatpages/default.html (Source does not exist)

here is the settings


TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': os.path.join(BASE_DIR, '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',
            ],
        },
    },
]

‘DIRS’ is a list. It needs to be:

‘DIRS’: [os.path.join(BASE_DIR, ‘templates’), ],

(Don’t worry - these types of mistakes are very common when getting started - or even when you’ve been doing this for years.)

Here are the settings.py templates section now:

TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [os.path.join(BASE_DIR, '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',
            ],
        },
    },
]

here is my file structure:
Selection_008

Not working and same messages

# TemplateDoesNotExist at /about/

flatpages/default.html

|Request Method:|GET|
| --- | --- |
|Request URL:|http://localhost:8000/about/|
|Django Version:|3.0.3|
|Exception Type:|TemplateDoesNotExist|
|Exception Value:|flatpages/default.html|

here is my settings.py file


TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [os.path.join(BASE_DIR, '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',
            ],
        },
    },
]

Here is my file structure now I copied the templates dir to the root and the project folder
Selection_008

My post was hidden by akismet so I have to wait for it to be shown. I really want to learn this. Thanks for all the help. I presume your name is Ken

You’re certainly welcome.

Ken
(yep, that’s my name)

Happy too make your acquaintance. My name is Calvin and as you know by now I am a newbie, on a mission to learn this. Not sure how long it will take for the post to show. I will check back later.