简体   繁体   中英

“TypeError at / argument 1 must be str, not PosixPath” in django ecommerce website

I was building an eCommerce website using Django and Vue.Js. There was no issue while I was hosting the website in localhost. But when I deployed my website on Heroku an error is showing up "TypeError at / argument 1 must be str, not PosixPath". Mainly an error in the jinja template.

code:

{% for category in menu_categories %}
.. statements ..
{% endfor %}

buckler-ecom.herokuapp.com The link to the deployed site

settings.py


STRIPE_API_KEY_PUBLISHABLE = "secret"
STRIPE_API_KEY_HIDDEN = "secret"

RAZORPAY_API_KEY_PUBLISHABLE = "secret"
RAZORPAY_API_KEY_HIDDEN = "secret"

PAYPAL_API_KEY_PUBLISHABLE = "secret"
PAYPAL_API_KEY_HIDDEN = "secret"

import os

from pathlib import Path

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


SECRET_KEY = 'secret'

DEBUG = True

ALLOWED_HOSTS = ['127.0.0.1', 'buckler-ecom.herokuapp.com']

EMAIL_HOST = 'localhost'
EMAIL_PORT = 1025

LOGIN_URL = 'login'
LOGIN_REDIRECT_URL = 'cart'
LOGOUT_REDIRECT_URL = 'frontpage'

# Cart

SESSION_COOKIE_AGE = 86400
CART_SESSION_ID = 'cart'

# Application definition

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

    'apps.cart',
    'apps.coupon',
    'apps.core',
    'apps.newsletter',
    'apps.order',
    'apps.store',
    'apps.userprofile'
]

MIDDLEWARE = [
    'django.middleware.security.SecurityMiddleware',
    'whitenoise.middleware.WhiteNoiseMiddleware',
    '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 = 'eCom.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',
                'apps.store.context_processors.menu_categories',
                'apps.cart.context_processors.cart'
            ],
        },
    },
]

WSGI_APPLICATION = 'eCom.wsgi.application'


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

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.sqlite3',
        'NAME': BASE_DIR / 'db.sqlite3',
    }
}


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

LANGUAGE_CODE = 'en-us'

TIME_ZONE = 'UTC'

USE_I18N = True

USE_L10N = True

USE_TZ = True


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

STATIC_URL = '/static/'
STATICFILES_DIRS = [
    os.path.join(BASE_DIR, 'static')
]
MEDIA_URL = '/media/'
MEDIA_ROOT = os.path.join(BASE_DIR, 'media/')

views.py

from django.shortcuts import render

from apps.store.models import Product, Category

def frontpage(request):
    products = Product.objects.filter(is_featured=True)
    featured_categories = Category.objects.filter(is_featured=True)
    popular_products = Product.objects.all().order_by('-num_visits')[0:4]
    recently_viewed_products = Product.objects.all().order_by('-last_visit')[0:4]

    context = {
        'products': products,
        'featured_categories': featured_categories,
        'popular_products': popular_products,
        'recently_viewed_products': recently_viewed_products
    }

    return render(request, 'frontpage.html', context)

Had the same issue, tried this and it worked:

STATIC_ROOT=BASE_DIR.as_posix()+ "static"

the same for

MEDIA_ROOT=BASE_DIR.as_posix()+ "media"

also had to use the same workaround to:

DATABASES = {'defalut':{
....
'NAME': BASE_DIR.as_posix() +'db.sqlite3',
}
}

try this i think this should work for you just replace your static and media url by this:

    STATIC_URL = '/static/'

STATICFILES_DIRS = [

    BASE_DIR / "static",
]

STATIC_ROOT = BASE_DIR.parent / "static_cdn"

MEDIA_URL = '/media/'

MEDIA_ROOT =  BASE_DIR / "media"

and no need to import os and try to use relative url for login,logout like this:

LOGOUT_REDIRECT_URL = '/logout/'

LOGIN_URL = '/login/'

LOGIN_REDIRECT_URL = '/'

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM