Having troubles with django project on deployment on vercel

Are you using nginx or apache in your project ?

I am using none of them

Instead of this^^ try it with this


STATIC_URL = '/static/'

STATICFILES_DIRS = (
    os.path.join(BASE_DIR, 'static'),
)

STATIC_ROOT = os.path.join(BASE_DIR, 'staticfiles')

but the folder name is static, so in the STATIC_ROOT won’t I’ve to keep it “static”?

When you run collectstatic command then all the static files are copied to STATIC_ROOT folder i.e staticfiles if you keep the same name then it might be possible that when you run collectstatic command then static copy folder will be created with all the required static files and it will use static folder which is available.
Also when you run collectstatic command it will copy all the Apps static files i.e even your Django Admin static files.

okay I get that, but why does it matter if the folder name is static or staticfiles? because both the folders are in different directories

You can name anything to the folder just have to specify STATIC_URL and STATIC_ROOT correctly in your settings.py
See I’ve done same thing as you

STATIC_URL = '/static/'
STATICFILES_DIRS = (
    os.path.join(BASE_DIR, 'static'),
)
STATIC_ROOT = os.path.join(BASE_DIR, 'static')

and when I run collectstatic command see the errors

but my STATICFILES_DIR has the static folder inside the folder where settings.py file is

Just try this it will work

do I have to comment STATICFILES_DIR line in deployment?

I’ve not commented this in my project deployment, it does not bug me while deploying the project.

its still not working

hey man, I figured out the issue, the problem was in my build_files.sh and the vercel.json file. I corrected it and it is now serving the static files. But now another issue is there. I am using gmail’s SMTP for sending emails. in the development its sending mails but in the production its not sending mails

for anyone coming across the same issue I hope this helps!

in the settings.py make this-

STATICFILES_DIRS = (
    os.path.join(BASE_DIR, 'expenseswebsite', 'static'),
)

STATIC_ROOT = os.path.join(BASE_DIR, 'staticfiles')

the STATICFILES_DIR will be different, I have kept it inside the folder where settings.py file is.

in the root URLS.py add this-

urlpatterns += static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)

in the build_files.sh make sure its like this-

pip install -r requirements.txt
python manage.py collectstatic --noinput 

in the vercel.json-

{
    "version": 2,
    "builds": [
      {
        "src": "yourprojectname/wsgi.py",
        "use": "@vercel/python",
        "config": { "maxLambdaSize": "15mb", "runtime": "python3.9" }
      },
      {
        "src": "build_files.sh",
        "use": "@vercel/static"
      }
    ],
    "routes": [
      {
        "src": "/staticfiles/(.*)",
        "dest": "/staticfiles/$1"
      },
      {
        "src": "/(.*)",
        "dest": "yourprojectname/wsgi.py"
      }
    ]
  }

make sure to keep the staticfiles name as it is because the name of the directory which is saving your staticfiles in the build on vercel

Thats it, I hope this helps anyone struggling.

1 Like

Hey Bro WHy you add website name in settings.py and i am facing the same issue plz tell me even everything is done same according to you

hey man im getting the same error you experienced when deploying django on vercel but even though I have removed psycopg2 it is still appearing in vercel if there is any thing you did other than remove it please notify me thank you

For some reason i cannot also use pymongo in vercel. when i comment out the import my app runs correctly. maybe try commenting out psycopg2 library. I am still tring to make mongodb integrate with my project using pymongo. If you want i can share you my project.

Hey man!
i am also getting the same error in my project
i already did what you have written here but none of my static files are loading in the deployed site
Can you please help out
i have tried a lot of platform but i didnot got any idea about my problem

See the issue could be the static files folder you defined in the settings.py and build.sh file can you share these three files?
settings.py
vercel.json
build.sh

maybe this problem is still hot but I managed to solve the problem. Most of the tutorials I followed didn’t work, and I’m sure the tutorial I followed was the same as yours which caused this problem. okay, for the solution, I deleted build_files.sh and for the vercel.json file I filled it simply like this

{
  "builds": [{
      "src": "yourproject/wsgi.py",
      "use": "@vercel/python",
      "config": { "maxLambdaSize": "15mb", "runtime": "python3.9" }
  }],
  "routes": [
      {
          "src": "/(.*)",
          "dest": "yourproject/wsgi.py"
      }
  ]
}

and for wsgi.py like this

application = get_wsgi_application()
app = application

. after that I pushed it to github and it automatically deployed. after deploy is complete. Static files were also detected. note, I also use the whitenoise library. let me know if this solution helps you guys