Search code examples
djangodeploymentwebserveradmindjango-staticfiles

Django deployment missing/admin view css/media/static files


I developed an e-commerce project, and I tried to publish it in my domain. When I publish my website, I see that media/static files and admin view css is missign in my website. (Of course, everything works fine in development mode localhost server.)

I bought the web server and domain from Antagonist.nl by knowing it supports Python; however, they do not give me any technical support to deploy my Django project, and I see that documentation is missing. When I do deploy my project in antagonist.nl webserver provider, I see it works with errors.

Altough I see many similar posts are available in stackoverflow, I couldn't find the answers for my case yet.

Moreover, I see need of manual work from Django documentation in this https://docs.djangoproject.com/en/4.2/howto/static-files/deployment/. However, they are all for apache and nginx web servers.

I would like to get help from the community for my webserver.

Part of my settings.py file:

from pathlib import Path

# Build paths inside the project like this: BASE_DIR / 'subdir'.
BASE_DIR = Path(__file__).resolve().parent.parent

DEBUG = False

INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'store', # Django app
    'cart', # Django app
    'account', # Django app
    'payment', # Django app
    'mathfilters',
    'crispy_forms',
]
    STATIC_URL = '/static/'
    STATICFILES_DIRS = [BASE_DIR / 'static',]
    MEDIA_URL = '/media/'
    MEDIA_ROOT = '/home/deb142470/domains/my-domain.nl/public_html/static/media'
    STATIC_ROOT = '/home/deb142470/domains/my-domain.nl/public_html/static'

urls.py file:

from django.contrib import admin
from django.urls import path, include
from django.conf import settings
from django.conf.urls.static import static

urlpatterns = [

    # Admin URL
    path('admin/', admin.site.urls),

    # Store app
    path('', include('store.urls')),

    # Cart app
    path('cart/', include('cart.urls')),

    # Account app
    path('account/', include('account.urls')),

    # Payment app
    path('payment/', include('payment.urls')),

]

# Add media root to existing django temp
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)

When I type "python manage.py collectstatic" in my SSH connection, I see that directly all static files at my project level static folder are copied to STATIC_ROOT defined in settings.py, which is something expected based on Django documentation. I checked the static files, they are all OK and nicely copied to STATIC_ROOT. Here are the folders created in STATIC_ROOT. (FTP view)

enter image description here

I am also importing media files from my django admin to show some pictures in my website. However, website is unable to read imported media files.

Main page of website

I see in my admin view, website tries to get media from the specific URL but this URL doesn't work. Moreovoer, I do not see uploaded media in any of my website folders.

Below link is an example of it: https://my-domain.nl/media/images/8720908160249-21.jpg

Unfortunately, admin view also has no css available.

enter image description here

If anyone help me to solve my problems, I would be appreciated!


Solution

  • I installed whitenoise package to serve static files for production phase of my django project, and the issue for media files and admin page are solved.