media files not found on pythonanywhere

The site is connected fine but images are not showing. i have done python3.8 manage.py collectstatic. Below are my setting on pythonanywhere.

  #### Code:
  
  What your site is running.
  
  Source code:
  
  [/home/Wilfred7/Django_market_place](https://www.pythonanywhere.com/user/Wilfred7/webapps/#)
  
  [Go to directory](https://www.pythonanywhere.com/user/Wilfred7/files/home/Wilfred7/Django_market_place)
  
  Working directory:
  
  [/home/Wilfred7/Django_market_place](https://www.pythonanywhere.com/user/Wilfred7/webapps/#)
  
  [Go to directory](https://www.pythonanywhere.com/user/Wilfred7/files/home/Wilfred7/Django_market_place)
  
  WSGI configuration file:
  
  [/var/www/wilfred7_pythonanywhere_com_wsgi.py](https://www.pythonanywhere.com/user/Wilfred7/files/var/www/wilfred7_pythonanywhere_com_wsgi.py?edit)
  
  Python version:
  
  3.8

Static files:

Files that aren’t dynamically generated by your code, like CSS, JavaScript or uploaded files, can be served much faster straight off the disk if you specify them here. You need to Reload your web app to activate any changes you make to the mappings below.

this is the directory

Files

.bashrc 2023-05-25 20:33 560 bytes
.gitconfig 2023-05-29 20:14 332 bytes
.my.cnf 2023-05-28 19:08 37 bytes
.profile 2023-05-25 20:33 79 bytes
.pythonstartup.py 2023-05-25 20:33 77 bytes
.sqlite_history 2023-05-29 18:21 598 bytes
.vimrc 2023-05-25 20:33 4.6 KB
README.txt 2023-05-25 20:33 232 bytes
Wilfred7$SQLite 2023-05-28 21:13 0 bytes

/home/Wilfred7/Django_market_place

Directories

Files

Wilfred7$SQLite 2023-05-28 21:51 340.0 KB
db.sqlite3 2023-05-30 20:20 340.0 KB
manage.py 2023-05-25 20:35 665 bytes
requirements.txt 2023-05-25 20:35 111 bytes

the media, logos, thumbnails all are not showing

STATIC_URL = ‘/static/’
STATICFILES = [os.path.join(BASE_DIR, ‘static/’)]
STATIC_ROOT =os.path.join(BASE_DIR, ‘staticfiles’)
MEDIA_URL =‘media/’
STATIC_ROOT = “/home/Wilfred7/Django_market_place/jijiapp/static”

2023-05-30 20:45:54,091: Not Found: /media/media/fan1_G0la2zj.jfif
2023-05-30 20:45:54,115: Not Found: /media/thumbnails/fan2_uBdhCUA.jfif
2023-05-30 20:45:54,134: Not Found: /media/thumbnails/ecco_n7ZKjaI.jpg
2023-05-30 20:45:54,146: Not Found: /media/thumbnails/tv1.jfif
2023-05-30 20:45:54,195: Not Found: /media/thumbnails/bag2_yKX0aGx.jfif
2023-05-30 20:45:54,343: Not Found: /media/thumbnails/bag4.jfif
2023-05-30 20:45:54,346: Not Found: /media/thumbnails/florer_kazITRQ.jpg
2023-05-30 20:45:54,710: Not Found: /media/thumbnails/super.jfif
1 Like

Please i need help here

I think you miss to pass the media_root path in your settings.py file as you did with static file
review your settings.py file.
You need to adjust the path on settings.py file as on the pythonanywhere server

you mean like this

                  MEDIA_ROOT ='/home/Wilfred7/Django_market_place/media'
                  MEDIA_URL = '/media/'

i try this but it refuses

Please take a look at this

I think it’s helpful

I went through it patiently but it doesnt work in my case. just cant figure out the problem for over a week now

Okay let me check on one of my demo app on pythonanywhere , Because I didn’t upload image on it

It’s working properly on my site as I said to you before you have something error in your media path

Let’s review your configuration for this.

  • First of all you must be sure that your local code can generate (media) folder automatically with the uploaded image in order to continue the next steps.

  • make DEBUG="True" in your (.env) file on pythonanywhere server (don’t forget to reload the server from WEB tab to activate your action)

  • Be sure that the next lines are exists on your local settings.py file

MEDIA_ROOT = os.path.join(os.path.dirname(BASE_DIR), "media")
MEDIA_URL = "/media/"

NOT as yours

  • Be sure that the next lines are exists on your local main urls.py file
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
urlpatterns += static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
  • On pythonanywhere server you must write the path as you mention above

But without an ending slash as
/media/|/home/Wilfred7/Django_market_place/media

  • Be sure of your paths carefully on pythonanywhere.com server when you write paths
  • If the media folder created automatically for you on pythonanywhere server then you are in the right direction and you are succeeding to upload your images (To ensure of that)
    https://your_app_name.pythonanywhere.com/media/path/to/image.png
  • Check your image on your website it will appear after refresh the page
  • Don’t forget to reload the sever every time you change anything on pythonanywhere.com server from web tab.

We are finishing now

Good Luck

Thank you for your effort. I have tried as you describe. But up to now the image is not showing. Below is the way i updated my url and setting as you pointed out

    DEBUG = True

  MEDIA_ROOT = os.path.join(os.path.dirname(BASE_DIR), "media")
  MEDIA_URL = "/media/"


  urlpatterns = [
      path('admin/', admin.site.urls),
      path('', include('jijiapp.urls')),
      path('store/', include('jijistore.urls')),
      path('login/', auth_views.LoginView.as_view(template_name='jijiapp/registration/login.html', authentication_form=LoginForm), name='login'),
      # path('accounts/', include('django.contrib.auth.urls')),
      # path('inbox/', include('jijiapp.routing')),
  
      path('accounts/password_reset/', auth_views.PasswordResetView.as_view(
          template_name='jijiapp/registration/password_reset.html'), name='password_reset'),
      path('accounts/password_reset/done/', auth_views.PasswordResetDoneView.as_view(
          template_name='jijiapp/registration/password_reset_done.html'), name='password_reset_done'),
      path('accounts/reset/<uidb64>/<token>/', auth_views.PasswordResetConfirmView.as_view(
          template_name='jijiapp/registration/password_reset_confirm.html'), name='password_reset_confirm'),
      path('accounts/reset/done/', auth_views.PasswordResetCompleteView.as_view(
          template_name='jijiapp/registration/password_reset_complete.html'), name='password_reset_complete'),
  
  ]
  urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
  urlpatterns += static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)

The static files

|URL|Directory|Delete|
|---|---|---|
|/static/|/home/Wilfred7/Django_market_place/jijiapp/static| |
|/media/|/home/Wilfred7/Django_market_place/media| |
|/logos/|/home/Wilfred7/Django_market_place/logos| |
|/thumbnails/|/home/Wilfred7/Django_market_place/thumbnails| |
|Enter URL|Enter path||

i have carefully looked ate the directory to see if i make any mistake but i could not trace any mistake. I have refresh the web. it still did not show up.

Okay

  • Have you checked if your local generates media folder automatically ?

  • Have your static files work as expected on pythonanywhere ? (try to check by)

https://your_app_name.pythonanywhere.com/static/path_to_any_file_in_your_static
  • Have the media folder generated on the pythonanywhere server ? (if yes check by)
    https://your_app_name.pythonanywhere.com/media/path/to/image.png
    (If No) we still have the problem.

  • Can you share your local settings.py (Copy and paste between (```) to investigate it clearly)?

  • Can you share your local urls.py as settings.py

  • Can you share web tab settings on the server (Only take a screen shot) ?

  • Also I need to see the code responsible for upload the image in your local site

  • Share with us the link to your your_app.pythonanywhere.com link

I hope to fix this issue, you maybe miss something

Yes, i dont know if you will need the address of the entire project on on github Wilfred1213/Django_market_place: A website that allows people to sell and buy (github.com)

this is address to the project Jiji Clone (wilfred7.pythonanywhere.com)

this is my setting.py on my local system not on python anywhere as you requested

  import os
  from pathlib import Path
  
  # 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 = []
  
  
  LOGIN_URL = '/login/'
  LOGIN_REDIRECT_URL = '/home/'
  # LOGOUT_REDIRECT_URL = 'index/'
  
  
  
  # Application definition
  
  INSTALLED_APPS = [
      'django.contrib.admin',
      'django.contrib.auth',
      'django.contrib.contenttypes',
      'django.contrib.sessions',
      'django.contrib.messages',
      'django.contrib.staticfiles',
      'jijiapp',
      'jijistore',
      'crispy_forms',
      'multiupload',
      'crispy_bootstrap4',
      # 'channels',
  ]
  
  CRISPY_ALLOWED_TEMPLATE_PACKS = "bootstrap4"
  CRISPY_TEMPLATE_PACK = 'bootstrap4'
  
  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 = 'jijiclone.urls'
  
  TEMPLATES = [
      {
          'BACKEND': 'django.template.backends.django.DjangoTemplates',
          'DIRS': [os.path.join(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 = 'jijiclone.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',
      }
  }
  
  
  # 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 = 'UTC'
  
  USE_I18N = True
  
  USE_TZ = True
  
  
  # Static files (CSS, JavaScript, Images)
  # https://docs.djangoproject.com/en/4.0/howto/static-files/
  
  STATICFILES_FINDERS = [
      'django.contrib.staticfiles.finders.FileSystemFinder',
      'django.contrib.staticfiles.finders.AppDirectoriesFinder',
  ]
  
  
  STATIC_URL = '/static/'
  STATICFILES = [os.path.join(BASE_DIR, 'static/')]
  STATIC_ROOT =os.path.join(BASE_DIR, 'staticfiles')
  
  MEDIA_URL ='/media/'
  
  # Default primary key field type
  # https://docs.djangoproject.com/en/4.0/ref/settings/#default-auto-field
  
  DEFAULT_AUTO_FIELD = 'django.db.models.BigAutoField'
  
  
  
  # email setup for gmail
  EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'
  EMAIL_HOST = 'smtp.gmail.com'
  EMAIL_PORT =465
  EMAIL_USE_SSL=True
  EMAIL_USE_TLS = False
  EMAIL_HOST_USER = 'mathiaswilfred7@gmail.com'
  EMAIL_HOST_PASSWORD= 'kpvwrumwpoucrdft'
  # EMAIL_HOST_PASSWORD=''

this is my local url.py root file as you requested

  from django.contrib import admin
  from django.urls import path, include
  from django.conf import settings
  from django.conf.urls.static import static
  from jijiapp.forms import LoginForm
  from django.contrib.auth import views as auth_views
  
  urlpatterns = [
      path('admin/', admin.site.urls),
      path('', include('jijiapp.urls')),
      path('store/', include('jijistore.urls')),
      path('login/', auth_views.LoginView.as_view(template_name='jijiapp/registration/login.html', authentication_form=LoginForm), name='login'),
      # path('accounts/', include('django.contrib.auth.urls')),
      # path('inbox/', include('jijiapp.routing')),
      
      path('accounts/password_reset/', auth_views.PasswordResetView.as_view(
          template_name='jijiapp/registration/password_reset.html'), name='password_reset'),
      path('accounts/password_reset/done/', auth_views.PasswordResetDoneView.as_view(
          template_name='jijiapp/registration/password_reset_done.html'), name='password_reset_done'),
      path('accounts/reset/<uidb64>/<token>/', auth_views.PasswordResetConfirmView.as_view(
          template_name='jijiapp/registration/password_reset_confirm.html'), name='password_reset_confirm'),
      path('accounts/reset/done/', auth_views.PasswordResetCompleteView.as_view(
          template_name='jijiapp/registration/password_reset_complete.html'), name='password_reset_complete'),
  
  ]
  if settings.DEBUG:
      urlpatterns +=static(settings.MEDIA_URL, document_root =settings.MEDIA_ROOT)


As expected your paths are incorrect.
And your settings.py file miss the next line
MEDIA_ROOT = os.path.join(os.path.dirname(BASE_DIR), "media")

But in your case write this
MEDIA_ROOT = os.path.join(BASE_DIR, "media")

This will work only if the DEBUG is “True”

Also try to use django-environ
Or take a look at this

this is the update of my media root as you suggested. But up to now the image is not displaying

  MEDIA_ROOT = os.path.join(BASE_DIR, "media")
  MEDIA_URL = "/media/"

Are you sure that you push your local changes to the server
Share your server settings.py on the pythonanywhere site

Ok, this is my server setting

  import os
  from pathlib import Path
  
  # 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 = ['wilfred7.pythonanywhere.com']
  
  
  LOGIN_URL = '/login/'
  LOGIN_REDIRECT_URL = '/home/'
  # LOGOUT_REDIRECT_URL = 'index/'
  
  
  
  # Application definition
  
  INSTALLED_APPS = [
      'django.contrib.admin',
      'django.contrib.auth',
      'django.contrib.contenttypes',
      'django.contrib.sessions',
      'django.contrib.messages',
      'django.contrib.staticfiles',
      'jijiapp',
      'jijistore',
      'crispy_forms',
      'multiupload',
      'crispy_bootstrap4',
      # 'channels',
  ]
  
  CRISPY_ALLOWED_TEMPLATE_PACKS = "bootstrap4"
  CRISPY_TEMPLATE_PACK = 'bootstrap4'
  
  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 = 'jijiclone.urls'
  
  TEMPLATES = [
      {
          'BACKEND': 'django.template.backends.django.DjangoTemplates',
          'DIRS': [os.path.join(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 = 'jijiclone.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',
      }
  }
  # DATABASES = {
  #     'default': {
  #         'ENGINE': 'django.db.backends.sqlite3',
  #         'NAME': 'Wilfred7$SQLite',
  #         'HOST': 'Wilfred7.mysql.pythonanywhere-services.com',
  #     }
  # }
  
  
  # 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 = 'UTC'
  
  USE_I18N = True
  
  USE_TZ = True
  
  STATICFILES_FINDERS = [
      'django.contrib.staticfiles.finders.FileSystemFinder',
      'django.contrib.staticfiles.finders.AppDirectoriesFinder',
  ]
  
  
  MEDIA_ROOT = os.path.join(BASE_DIR, "media")
  MEDIA_URL = "/media/"
  
  STATIC_URL = '/static/'
  STATICFILES = [os.path.join(BASE_DIR, 'static/')]
  STATIC_ROOT =os.path.join(BASE_DIR, 'staticfiles')
  
  
  
  DEFAULT_AUTO_FIELD = 'django.db.models.BigAutoField'

Apologies i gat the problem. Ever since i uploaded the project on pythonanywhere, i never try to upload new image. This morning i uploaded new image and it shows up on the server. Thank you so much for supporting me.

1 Like

Glad to here that

Happy coding

1 Like