S
S
Sergey Nizhny Novgorod2016-02-15 14:24:17
Django
Sergey Nizhny Novgorod, 2016-02-15 14:24:17

Why is the error displayed: The requested URL / was not found on this server. (django)?

Guys, hello everyone.
I'm setting up a Django project. I can't resolve the situation. I go to the site, an error appears: The requested URL / was not found on this server. If I put 404.html into the Templates public folder, this page is displayed.
What I did:
1) Created a project based on Django 1.9.2 and python 3.4.3 in PyCharm.
2) I ordered a VPS on Cent OS7 and tied it to the hosting.
3) Created a user with sudo rights: bakotiinii
4) Set the server time.
5) Installed EPEL
sudo yum install epel-release
6) Installed PostgreSQL

sudo yum install python-pip python-devel postgresql-server postgresql-devel postgresql-contrib gcc nginx

7) Set up PostgreSQL
sudo postgresql-setup initdb

sudo systemctl start postgresql

sudo nano /var/lib/pgsql/data/pg_hba.conf

Setting up the database for access under the user
# TYPE DATABASE USER ADDRESS METHOD
# "local" is for Unix domain socket connections only
local   all             all                                     peer
# IPv4 local connections:
#host    all             all             127.0.0.1/32            ident
host    all             all             127.0.0.1/32            md5
# IPv6 local connections:
#host    all             all             ::1/128                 ident
host    all             all             ::1/128                 md5

Making the database autorun
sudo systemctl restart postgresql
sudo systemctl enable postgresql

8) Create a user for the database:
sudo su - postgres

psql

CREATE DATABASE myproject;

CREATE USER myprojectuser WITH PASSWORD 'password';

GRANT ALL PRIVILEGES ON DATABASE myproject TO myprojectuser;

9) Install Python 3.4.3
sudo yum install python34-devel
10) Install a virtual environment
sudo pip install virtualenv
10) Create a virtual environment at the root running Python 3.4.3
mkdir ~/apifolder
cd ~/apifolder

mkvirtualenv -p /usr/local/bin/python3.4 djangoen

11) Activate the virtual environment
source djangoen/bin/activate
12) Install Django, Gunicorn and the psycopg2 manager
pip install django==1.9.2 gunicorn psycopg2
13) Create a Django project in the current folder (the project name is the same as mine in PyCharm):
django-admin.py startproject apifolder .
14) Replace the project folder with my project from PyCharm via FTP
15) I make changes to settings.py - connect the database, connect the statics and set the hosts:
DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.postgresql',
        'NAME': 'myproject',
        'USER': 'myprojectuser',
        'PASSWORD': 'password',
        'HOST': 'localhost',
        'PORT': '',
    }
}


STATIC_URL = '/static/'
MEDIA_URL = '/media/'
STATIC_ROOT = os.path.join(BASE_DIR, "static")
MEDIA_ROOT = os.path.join(BASE_DIR, "media")
STATICFILES_DIRS = (
)

DEBUG = True

ALLOWED_HOSTS = [
    '*',
    ]

16) ImageField is used in project models, so I put Pillow (First dependent packages, then Pillow itself), after which Django stops swearing.
sudo yum install libtiff-devel libjpeg-devel libzip-devel freetype-devel lcms2-devel libwebp-devel tcl-devel tk-devel

pip install Pillow

17) Check the database, do the migration and collect the statics
cd ~/apifolder
./manage.py makemigrations
./manage.py migrate
./manage.py collectstatic

Deactivate the virtual environment
18) Set the Unicorn config
sudo nano /etc/systemd/system/gunicorn.service
[Unit]
Description=gunicorn daemon
After=network.target

[Service]
User=bakotiinii
Group=nginx
WorkingDirectory=/home/bakotiinii/apifolder
ExecStart=/home/bakotiinii/apifolder/djangoen/bin/gunicorn --workers 3 --bind unix:/home/bakotiinii/apifolder/apifolder.sock apifolder.wsgi:application

[Install]
WantedBy=multi-user.target

Let's restart it:
sudo systemctl start gunicorn
sudo systemctl enable gunicorn

19) Setting up nGinx
sudo nano /etc/nginx/nginx.conf

server {
    listen 80;
    server_name example.ru www.example.ru;

    location = /favicon.ico { access_log off; log_not_found off; }
    location /static/ {
        root /home/bakotiinii/apifolder;
    }

    location / {
        proxy_set_header Host $http_host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto $scheme;
        proxy_pass http://unix:/home/bakotiinii/apifolder/apifolder.sock;
    }
}

20) Final settings and launch:
sudo usermod -a -G bakotiinii nginx

chmod 710 /home/bakotiinii

sudo nginx -t

sudo systemctl start nginx

sudo systemctl enable nginx

______
As a result, the situation turns out that:
On a LAN via runserver - it works.
On the combat server
1) The admin panel is working.
2) The database is running.
3) The static is given away.
4) And the pages give an error that there is nothing at such URLs.
5) The nginx logs do not give any errors.
My project has several applications, so each application has its own url file and template folder (I attached the url through the include, and I attached the templates through PyCharm - right mouse button and MarkDirectory as template). Is there some kind of problem with this?
download?id=DLRkcI1vADF3xCIrfbeSMoc0rIR4
For example, the ULRs themselves:
urlpatterns = [
    url(r'^$', views.index, name='index'),
    url(r'^step(?P<step_id>[0-9]+)$', views.step, name='step'),
    url(r'^addcomment(?P<step_id>[0-9]+)$', views.addcomment, name='addcomment'),
    url(r'^question(?P<question_id>[0-9]+)$', views.question, name='question'),
    url(r'^newpage(?P<step_id>[0-9]+)/$', views.new_page,name='newpage'),
    url(r'^lastpage(?P<step_id>[0-9]+)/$', views.last_page,name='lastpage'),
    url(r'^answerset$', views.answerset, name='answerset'),
    url(r'^isperm$', views.isperm, name='isperm'),
]

Update
Added
TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [
            os.path.join(BASE_DIR, 'templates'),
            os.path.join(BASE_DIR, 'bakot/templates/'),
            os.path.join(BASE_DIR, 'loginsys/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',
                'django.template.context_processors.media'
            ],
        },
    },
]

Ps TEMPLATE_DIRS has been removed since version 1.8.
Does not help, mail is not configured yet.
Update - solution
url.py of one of the application was copied via FTP for some reason without rights. Therefore, copying was canceled, and a standard file with commentary examples was created. I started walking around the project and noticed a mismatch in size.
Ps You need to use git!

Answer the question

In order to leave comments, you need to log in

4 answer(s)
D
Dmitry Voronkov, 2016-02-16
@Terras

TEMPLATE_DIRS = ('FULL_PATH_PROJECT/templates/',)
Try in the settings list the paths to your templates.
More:

LOGGING = {
    'version': 1,
    'disable_existing_loggers': True,
    'formatters': {
        'verbose': {
            'format': '%(levelname)s %(asctime)s %(module)s '
                      '%(process)d %(thread)d %(message)s'
        },
        'simple': {
            'format': '%(levelname)s %(message)s'
        },
    },
    'handlers': {
        'null': {
            'level': 'DEBUG',
            'class': 'django.utils.log.NullHandler',
        },
        'console': {
            'level': 'DEBUG',
            'class': 'logging.StreamHandler',
            'formatter': 'simple'
        },
        'mail_admins': {
            'level': 'ERROR',
            'class': 'django.utils.log.AdminEmailHandler',
            'include_html': True,
        }
    },
    'loggers': {
        'django': {
            'handlers': ['console'],
            'propagate': True,
            'level': 'INFO',
        },
        'django.db': {
            'level': 'DEBUG',
            'handlers': ['console'],
            'propagate': True,
        },
        'django.request': {
            'handlers': ['mail_admins', 'console'],
            'level': 'ERROR',
            'propagate': False,
        },
    }
}

If mail is configured on the server, then letters with error logs will fly to you. Watch them.

D
Dimonchik, 2016-02-15
@dimonchik2013

it's time to start learning the basics of marketing:
if you want an answer, simplify the question

S
Sergey Novikov, 2016-02-15
@BOOMER_74

Did ulrs.py even rule?

O
Oscar Django, 2016-02-15
@winordie

Run via runserver

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question