1

When I run my project this error comes.

Exception in thread django-main-thread:
Traceback (most recent call last):
  File "C:\Users\Mowgli\anaconda3\envs\surajDjangoEnv\lib\threading.py", line 916, in _bootstrap_inner
    self.run()
  File "C:\Users\Mowgli\anaconda3\envs\surajDjangoEnv\lib\threading.py", line 864, in run
    self._target(*self._args, **self._kwargs)
  File "C:\Users\Mowgli\anaconda3\envs\surajDjangoEnv\lib\site-packages\django\utils\autoreload.py", line 53, in wrapper
    fn(*args, **kwargs)
  File "C:\Users\Mowgli\anaconda3\envs\surajDjangoEnv\lib\site-packages\django\core\management\commands\runserver.py", line 118, in inner_run
    self.check(display_num_errors=True)
  File "C:\Users\Mowgli\anaconda3\envs\surajDjangoEnv\lib\site-packages\django\core\management\base.py", line 396, in check
    databases=databases,
  File "C:\Users\Mowgli\anaconda3\envs\surajDjangoEnv\lib\site-packages\django\core\checks\registry.py", line 70, in run_checks
    new_errors = check(app_configs=app_configs, databases=databases)
  File "C:\Users\Mowgli\anaconda3\envs\surajDjangoEnv\lib\site-packages\django\contrib\staticfiles\checks.py", line 9, in check_finders
    finder_errors = finder.check()
  File "C:\Users\Mowgli\anaconda3\envs\surajDjangoEnv\lib\site-packages\django\contrib\staticfiles\finders.py", line 81, in check
    if prefix.endswith('/'):
AttributeError: 'WindowsPath' object has no attribute 'endswith'
大陸北方網友
  • 3,696
  • 3
  • 12
  • 37
suraj karosia
  • 11
  • 1
  • 2
  • I don't declare WindowsPath anywhere and don't import them why has this error come? – suraj karosia Nov 04 '20 at 06:46
  • Show us your settings (excluding private information)! Which Django version are you using? – Klaus D. Nov 04 '20 at 06:52
  • 1
    Hi Suraj, please provide the relevant code. It should however be enough, if you change your code to something like if str(prefix).endswith('/') to solve the specific issue here. But since you don't explain, what you are trying to do, it is hard to guess, if there might be a better solution. – n00by0815 Nov 04 '20 at 06:55
  • 1
    @n00by0815 The code is in the Django library, changing it there would create update troubles. I suspect faulty data or a bug in Django. – Klaus D. Nov 04 '20 at 07:02
  • @KlausD. You are right and I am sorry I didn't properly read the error message. Still his OS (Although from the paths it looks like windows) as well as his Python/Anaconda and Django version are missing. And I don't seem to see any path in his traceback, that he presumeably set somewhere in his code. Maybe he just set it to Path(something) instead of just providing a path in string format. Hard to tell – n00by0815 Nov 04 '20 at 12:42
  • @KlausD I use Django version 3.1.1 – suraj karosia Nov 08 '20 at 06:42
  • And the settings? – Klaus D. Nov 08 '20 at 06:45
  • # Build paths inside the project like this: BASE_DIR / 'subdir'. BASE_DIR = Path(__file__).resolve().parent.parent TEMPLATE_DIR= BASE_DIR, 'template' STATIC_DIR = BASE_DIR, 'static' MEDIA_DIR= BASE_DIR, 'media' – suraj karosia Nov 10 '20 at 05:21
  • # Application definition INSTALLED_APPS = [ 'django.contrib.admin', 'django.contrib.auth', 'django.contrib.contenttypes', 'django.contrib.sessions', 'django.contrib.messages', 'django.contrib.staticfiles', 'debug_toolbar', 'basic_app' ] – suraj karosia Nov 10 '20 at 05:23
  • TEMPLATES = [ { 'BACKEND': 'django.template.backends.django.DjangoTemplates', 'DIRS': [TEMPLATE_DIR], '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', ], }, }, ] – suraj karosia Nov 10 '20 at 05:24
  • # Static files (CSS, JavaScript, Images) # https://docs.djangoproject.com/en/3.1/howto/static-files/ STATIC_URL = '/static/' STATICFILES_DIRS= [STATIC_DIR,] #Media MEDIA_ROOT= MEDIA_DIR MEDIA_URL = '/media/' LOGIN_URL = '/basic_app/ user_login' – suraj karosia Nov 10 '20 at 05:30
  • @KlausD. is this helpful? – suraj karosia Nov 10 '20 at 05:36
  • Please add all relevant information to the question itself. It is unreadable in the comments. – Klaus D. Nov 10 '20 at 05:53

3 Answers3

1

You can get parts of WindowsPath object with property parts. Select the last part and use the endswith attribute.

import pathlib
import os

path_to_here = pathlib.Path(os.getcwd())
last_part = path_to_here.parts[-1]
print(last_part.endswith('ending'))
Jman
  • 171
  • 4
0

""" Django settings for the learning_user project.

Generated by 'Django-admin start project' using Django 3.1.1.

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

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

from pathlib import Path

Build paths inside the project like this: BASE_DIR / 'subdir'.

suraj karosia
  • 11
  • 1
  • 2
0

This error happens due to the configuration of static files

Try this:

STATIC_URL = '/static/'

STATICFILES_DIRS = [
    BASE_DIR / "static",
]

STATIC_ROOT = BASE_DIR / "staticfiles"

MEDIA_URL = "/media/"

MEDIA_ROOT = BASE_DIR / "media"

You don't have to use the os module, it not necessary. BASE_DIR already defined in your settings.py file.

STATICFILES_DIRS is used in deployment. If you want, you can delete it for now.

click for detailed explanation

Alpcap
  • 46
  • 7