The web application on the local server cannot be displayed at the URL to which the remote server is transferred.

Asked 1 years ago, Updated 1 years ago, 320 views

Prerequisites/What you want to achieve
When I transfer the web application file created in Django, Python, to the remote server using the command prompt, I get the following error at the destination URL:

Problems/Error Messages you are experiencing

Template error:
Intemplate/URL/polls/index.html, error at line 1
   argument 1 must be str, not PosixPath
   1 : {%iflatest_question_list%} 
   2:<ul>
   3: {%for question intelatest_question_list%}
   4:<li><a href="{%url'polls:detail'question.id%}">{{question.question_text}}</a></li>;
   5: {%endfor%}
   6—</ul>
   7: {%else%}
   8—<p>No polls are available.</p>
   9: {%endif%}

Exception Type: TypeError at /polls/
Exception Value: argument 1 must be str, not PosixPath

Affected Source Code
settings.py

importos

# 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/2.2/howto/deployment/checklist/

# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY=

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

DEBUG = True

ALLOWED_HOSTS= ["*"]


# Application definition

INSTALLED_APPS = [
    'polls.apps.PollsConfig',
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
]

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='mysite.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='mysite.wsgi.application'


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

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.sqlite3',
        'NAME': os.path.join(BASE_DIR/'db.sqlite3'),
    }
}


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

AUTH_PASSWORD_VALIDATORS = [
    {
        'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilityValidator',
    },
    {
        '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/Tokyo'

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'

Tried
settings.py

ALLOWED_HOSTS=["]→ALLOWED_HOSTS=[*"]

Similarly, settings.py DATABASES

'NAME': os.path.join(BASE_DIR/'db.sqlite3'), →'NAME': os.path.joinstr(BASE_DIR/'db.sqlite3'),

When I changed to , the URL could not be displayed, and the migration and runserver received the following error:

TypeError: unsupported operand type(s) for /: 'str' and 'str'

The same error also occurred before changing settings.py DATABASES.

add

settings.py DATABASES

'NAME': str(os.path.join(BASE_DIR/'db.sqlite3')),

After changing to , migrate and runserver locally again, but error

 unsupported operand type(s) for /: 'str' and 'str'

Similarly, settings.py DATABASES

'NAME': os.path.join(BASE_DIR, 'db.sqlite3')),

After changing to , there are no errors in both local migration and runserve.
However, it cannot be viewed remotely.

Also, although the reference site stated that the version of Django was 3 or higher, for errors on the remote, for str recommendation,

'NAME': os.path.join(str(BASE_DIR), 'db.sqlite3')),

I tried to change to , but there were no errors in both the migration and runserve locally.
However, it cannot be viewed remotely.

Supplement (e.g., tool version)
python 3.6.9
django 2.2.7
Windows 10

The local and remote versions of Python, django have been unified.

reference site
https://www.codingforentrepreneurs.com/comments/17266

python html django

2022-09-30 22:04

1 Answers

The first error is

 argument 1 must be str, not PosixPath

As you can see, the argument that should be str is PosixPath.

How about converting the NAME to str as follows and then handing it over?

'NAME': str(os.path.join(BASE_DIR/'db.sqlite3')),

I think this link is also helpful.
https://utubou-tech.com/type-error_posixpath/


2022-09-30 22:04

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.