Django无法访问默认的管理部分

2024-06-16 12:03:31 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图访问django默认的管理部分,我得到一个

404 error "not found"

我可以访问默认的django启动页面,该页面告诉我我处于调试模式

如果我试图通过nginx访问,我会在浏览器中遇到nginx Base 404错误“找不到”

(secret) kermit@tuna:~/www/src/exchange $ cat /etc/nginx/sites-available/default 
upstream django {
        server unix:///var/run/uwsgi/exchange.sock; # for a file socket
        #server 127.0.0.1:8001; # for a web port socket (we'll use this first)
}


# Default server configuration
#
server {
        access_log /var/log/nginx/access.log;
        error_log /var/log/nginx/error.log;
        listen 80 default_server;
        listen [::]:80 default_server;

        # SSL configuration
        #
        listen 443 ssl default_server;
        listen [::]:443 ssl default_server;
        root /home/kermit/www/src/exchange;
        index index.html index.htm index.php index.nginx-debian.html;

        server_name tuna.tra.com;

        location / {
                try_files $uri $uri/ =404;
                include     /etc/nginx/uwsgi_params;
        }

        # pass PHP scripts to FastCGI server
        #
        location ~ \.php$ {
                include snippets/fastcgi-php.conf;
        }

        # Django media
        location /media  {
                alias /home/kermit/www/src/exchange/media;  # your Django project's media files - amend as required
        }

        location /static {
                alias /home/kermit/www/src/exchange/static; # your Django project's static files - amend as required
        }

        # Finally, send all non-media requests to the Django server.
        #location / {
        #        uwsgi_pass  django;
        #        include     /etc/nginx/uwsgi_params; # the uwsgi_params file you installed
        #}
}

(秘密)kermit@tuna:~/www/src/exchange$cat exchange/url.py

from django.contrib import admin
from django.urls import path

urlpatterns = [
    path('admin/', admin.site.urls),
]

(秘密)kermit@tuna:~/www/src/exchange$cat exchange/settings.py

import os
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
SECRET_KEY = 'snicker'
DEBUG = True
ALLOWED_HOSTS = ['192.168.42.13','localhost']
# Application definition
INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',

    #mystuff
    'userdash',
#    'userdash.apps.UserdashConfig',
]

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 = 'exchange.urls'

TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [],
        '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 = 'exchange.wsgi.application'


# Database
# https://docs.djangoproject.com/en/2.2/ref/settings/#databases

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.sqlite3',
        'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
    }
}


# Password validation
# https://docs.djangoproject.com/en/2.2/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',
    },
]

LANGUAGE_CODE = 'en-us'
TIME_ZONE = 'UTC'
USE_I18N = True
USE_L10N = True
USE_TZ = True
# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/2.2/howto/static-files/
STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR, 'static/')






(secret) kermit@tuna:~/www/src/exchange $ cat userdash/models.py 
#be sure to run makemigrations and migrate after editing this file
from django.db import models

# Create your models here.

class Product(models.Model):
        title           = models.TextField()
        describtion     = models.TextField()
        price           = models.TextField()
        summary         = models.TextField(default='Bite me wanker!')

如何访问默认的django管理部分

更新:

如果我使用

python3 manage.py runserver 8181

然后从命令行使用

links http://127.0.0.1:8181/admin/

我可以连接到管理员文件夹刚刚好。所以我的问题是针对nginx的,而不是Django

更新:

如果使用尾部斜杠浏览,效果相同

http://127.0.0.1/admin/

并且还使用默认的django Web服务器

python3 manage.py runserver 0.0.0.0:8000

更新:

我终于能够从日志中找出这个错误

2020/03/28 19:38:20 [error] 28811#28811: *22 open() "/home/kermit/www/src/exchange/static/admin/css/fonts.css" failed (13: Permission denied), client: 127.0.0.1, server: tuna.tra.com, request: "GET /static/admin/css/fonts.css HTTP/1.1", host: "fatchalkrvfzviql.com", referrer: "http://fatchalkrvfzviql.com/"

我试过了

chown -R www-data:www-data /home/kermit/www/src/exchange/static/

chown -R kermit:kermit /home/kermit/www/src/exchange/static/

确保目录为chmod 700和文件600

没什么


Tags: pathdjangosrccomauthdefaultexchangeserver
3条回答

您的管理员url配置如下path('admin/', admin.site.urls),

您应该使用的URL来访问管理面板是http://127.0.0.1/admin/

您缺少结尾处的尾随正斜杠

你试过这个吗

替换URL

http://127.0.0.1/admin

http://127.0.0.1:8000/admin

是什么让它起作用的

location @proxy {
        # Pass other requests to uWSGI
        uwsgi_pass unix://var/run/uwsgi/exchange.sock;
        include /etc/nginx/uwsgi_params;
}

location / {
        try_files $uri @proxy;
}

我确实试过了

#try_files $uri @proxy =404;

被卡住了,但正在移除

=404 

让它工作,我不知道为什么

相关问题 更多 >