When i am deploying on heroku i get Page not found (404)

994 Views Asked by At

When I go to the homepage, I get the following error:

Page not found (404)``
Request Method: GET
Request URL:    https://blogapp85.herokuapp.com/
Using the URLconf defined in blog.urls, Django tried these URL patterns, in this order:

admin/
blogapp/
The empty path didn’t match any of these.

You’re seeing this error because you have DEBUG = True in your Django settings file. Change that to False, and Django will display a standard 404 page.

settings.py

from pathlib import Path
import dj_database_url
import os 
import django_heroku

# Build paths inside the project like this: BASE_DIR / 'subdir'.
BASE_DIR = Path(__file__).resolve().parent.parent


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

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

ALLOWED_HOSTS = ['0.0.0.0', 'localhost', '127.0.0.1', 'blogapp85.herokuapp.com']


# Application definition

INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'blogapp',
    'django_social_share',
    'whitenoise.runserver_nostatic'
]

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',
    'whitenoise.middleware.WhiteNoiseMiddleware',
]

ROOT_URLCONF = 'blog.urls'

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

WSGI_APPLICATION = 'blog.wsgi.application'


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

#DATABASES = {
 #   'default': {
 #       'ENGINE': 'django.db.backends.sqlite3',
  #      'NAME': BASE_DIR / 'db.sqlite3',
# }
#}
STATICFILES_STORAGE = 'whitenoise.storage.CompressedManifestStaticFilesStorage'
STATIC_ROOT = os.path.join(BASE_DIR, 'staticfiles')
STATIC_URL = '/static/'

# Extra places for collectstatic to find static files.
STATICFILES_DIRS = (
    os.path.join(BASE_DIR, 'static'),
)
MEDIA_ROOT = os.path.join(BASE_DIR,'media')
MEDIA_URL = '/media/'



DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.postgresql',            
        'POST':'5432',
    }
}
db_from_env = dj_database_url.config(conn_max_age=600)
DATABASES['default'].update(db_from_env)

# Password validation
# https://docs.djangoproject.com/en/4.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/4.0/topics/i18n/

LANGUAGE_CODE = 'en-us'

TIME_ZONE = 'Asia/Kolkata'

USE_I18N = True

USE_TZ = True


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

STATIC_URL = 'static/'


# Default primary key field type
# https://docs.djangoproject.com/en/4.0/ref/settings/#default-auto-field

DEFAULT_AUTO_FIELD = 'django.db.models.BigAutoField'
#smtp configurations
EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'
EMAIL_HOST = 'smtp.gmail.com'
EMAIL_USE_TLS = True
EMAIL_PORT = 587
EMAIL_HOST_USER ='@gmail.com'
EMAIL_HOST_PASSWORD =''

django_heroku.settings(locals())

urls.py of app
urlpatterns = [
    path('admin/', admin.site.urls),
    path('blogapp/',include('blogapp.urls')),
]

    

urls.py of blogapp 
app_name='blogapp'
urlpatterns=[
    path('',views.home,name='home'),
    path('createblog/',views.blogview,name='blogview'),
    path('blog/',views.blogretrieve,name='blog'),
    path('signup/',views.signupview,name='signup'),
    path('login/',views.loginview,name='login'),
    path('logout/',views.logoutview,name='logout'),
    path('author/<int:pk>/',views.authorview,name='author'),
    path('blogdata/<str:pk>/',views.blog,name='blogdata'),
    path('profile/<str:pk>/',views.profile,name='profile'),
    path('change-password/', auth_views.PasswordChangeView.as_view(template_name='blogapp/change-password.html',success_url=reverse_lazy('blogapp:password_change_done')),name='change-password'),
    path('password_change/done/',auth_views.PasswordChangeDoneView.as_view(template_name='blogapp/password_change_done.html'),name='password_change_done'),
   
    path('reset_password/',auth_views.PasswordResetView.as_view(template_name='blogapp/reset_password.html',
            success_url=reverse_lazy('blogapp:password_reset_done'),
            email_template_name='blogapp/reset_password_email.html'
            ),name='reset_password'),  
    path('reset_password_sent/',auth_views.PasswordResetDoneView.as_view(template_name='blogapp/reset_password_sent.html'),name='password_reset_done'),
    path('reset/<uidb64>/<token>/',auth_views.PasswordResetConfirmView.as_view(template_name='blogapp/reset_password_cofirm.html',
            success_url = reverse_lazy("blogapp:password_reset_complete")),name='password_reset_confirm'),
    path('reset_password_complete/',auth_views.PasswordResetCompleteView.as_view(template_name='blogapp/password_reset_complete.html'),name='password_reset_complete'),
    
]
1

There are 1 best solutions below

0
On

You need to include path('',views.home,name='home'), in your project urls.py (the default one) and not in the blog app:

# Import your home view
urlpatterns = [
    path('', home, name='home'),
    path('admin/', admin.site.urls),
    path('blogapp/',include('blogapp.urls')),
]

Remember to set DEBUG=False.