vue.js django电子商务网站中的“TypeError at / argument 1 must be str,not PosixPath”

zynd9foi  于 2023-04-07  发布在  Vue.js
关注(0)|答案(3)|浏览(180)

我正在使用Django和Vue.Js构建一个电子商务网站。当我在localhost中托管网站时没有问题。但是当我在Heroku上部署我的网站时,出现了一个错误“TypeError at / argument 1 must be str,not PosixPath”。主要是jinja模板中的错误。

代码:

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

buckler-ecom.herokuapp.com www.example.com

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)
mkh04yzy

mkh04yzy1#

有同样的问题,尝试这个,它的工作:

STATIC_ROOT=BASE_DIR.as_posix()+ "static"

同样地

MEDIA_ROOT=BASE_DIR.as_posix()+ "media"

也必须使用相同的解决方法:

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

kxe2p93d2#

试试这个,我认为这应该为你工作,只是用这个替换你的静态和媒体URL:

STATIC_URL = '/static/'

STATICFILES_DIRS = [

    BASE_DIR / "static",
]

STATIC_ROOT = BASE_DIR.parent / "static_cdn"

MEDIA_URL = '/media/'

MEDIA_ROOT =  BASE_DIR / "media"

并且不需要导入os并尝试使用相对url进行登录,注销如下:

LOGOUT_REDIRECT_URL = '/logout/'

LOGIN_URL = '/login/'

LOGIN_REDIRECT_URL = '/'
pzfprimi

pzfprimi3#

我也遇到过类似的问题。在经历了大量的答案后,下面的答案终于对我起作用了。
1.使用已经导入的pathlib库中的joinpath()函数。
1.使用str()函数将上述结果转换为字符串格式。

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.sqlite3',
        'NAME': str(BASE_DIR.joinpath('db.sqlite3')),
    }
}

让我们知道这是否有效!

相关问题