Unauthorized access problem on django

Hi,I am in trouble with django unauthorized access problem.i have a django project which is ready to production.so i changed Debug=False in settings.py.When i access unauthorized urls or pages on my website,it work perfectly.But the problem is it can’t access static and media files.The background images or other css files are not working perfectly.


Above image is a webpage after adding debug=False.Please help me to solve this problem.

settings.py

STATIC_ROOT = os.path.join(BASE_DIR, ‘static’)
STATIC_URL = ‘/static/’
MEDIA_ROOT = os.path.join(BASE_DIR, ‘media’)
MEDIA_URL = ‘/media/’

Urls.py

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

When you say “can’t access”, are you getting 404 messages on those requests?

  • Have you run collectstatic to move your static files to their proper location?
  • What are you running as your web server?
    • Has it been configured correctly for the static url and directories?

See Deploying static files for more detailed information.

(I really hope you’re not trying to run a production site using runserver…)

<opinion>
I’ve often found that it’s better to set STATIC_ROOT to something completely outside the project - one of the directories that the web server has direct access to. That helps avoid file permission issues.
</opinion>

Ken

Sir,Now i get 404 and 500 messages when i request wrong url.But my webpage are loaded without css and js .i already tried collect static method.But i couldn’t solve this problem.i am using putty and filezilla for production.

.This image shows my django folders.

What does your production environment look like? Are you using Apache and mod_wsgi? Are you using Nginx and uwsgi? Some other combination?

We need a lot more details about your production configuration, not just the file layouts.

ok i am new to django production.So i have no more knowledge about this. i am using a2hosting for domain and server.But i have no cpanal access.So i used putty and filezilla for uploaing and configuring my django project.Also i installed nginx.I hosted my website using these commands

first created a directory using the command-
mkdir test

Change director to test folder-
cd test

Installed and activated virtual environment using the command-
python -m pip install virtualenv
virtualenv venv -p python3
source venv/bin/activate

Installed django using the command-
pip install django

Then i copied my django project from local file to test folder(which is created earlier) using filezilla.
And changed directory using the command-
cd myproject name

Finally run server using these commands-
vi test.sh
added these into vi test.sh python manage.py runserver xx.xxx.xxx.xx:80
give cmnds chmod 777 test.sh
nohup ./test.sh >/dev/null 2>&1

These commands are used to upload and runing my website.
please help me to solve my problems.i am stucked with this more than a month

First, I’m going to suggest you contact a2hosting’s tech support to see how they suggest running Django in their environment. Not all providers are “Django-friendly”, and it’s possible that they are not suitable for running a Django site.

Second, I’m going to reinforce what’s in the documentation for runserver:

DO NOT USE THIS SERVER IN A PRODUCTION SETTING. It has not gone through security audits or performance tests.

Finally, you mentioned nginx but you didn’t mention how you had it configured or how you were running it or how you were planning to connect it to Django.

Please read the docs on Deploying Django and the Deployment Checklist. I suggest walking through the process at least once on a server that you control, not on the providers system, so that you can get a better feel for how all the pieces fit together and to try to understand why all the pieces exist.

You might also just do a web search for django deployment gunicorn nginx and see what you find in the way of tutorials or blogs, I know there are some good ones out there.

Ken

thanks for this valuable informations.Thank you somuch