【问题标题】:django cannot access default admin sectiondjango 无法访问默认管理部分
【发布时间】:2023-03-04 07:56:06
【问题描述】:

我正在尝试访问 django 默认管理部分,我得到一个

404 error "not found"

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

如果我尝试通过 nginx 访问,我会在浏览器中收到 nginx bases 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/urls.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 管理部分?

更新:

如果我使用 django 服务器运行

python3 manage.py runserver 8181

然后使用

从命令行尝试
links http://127.0.0.1:8181/admin/

我可以很好地连接到管理文件夹。所以我的问题出现在 nginx 特定而不是 Django。

更新:

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

http://127.0.0.1/admin/

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

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

什么都没有

【问题讨论】:

    标签: python django nginx


    【解决方案1】:

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

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

    你错过了末尾的正斜杠。

    【讨论】:

    • 尾部斜线无效。
    【解决方案2】:

    你试过了吗

    替换网址

    http://127.0.0.1/admin
    

    http://127.0.0.1:8000/admin
    

    【讨论】:

    • 尾部斜线无效。
    【解决方案3】:

    是什么让它发挥作用的是

    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 
    

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

    【讨论】:

      猜你喜欢
      • 2016-04-22
      • 2021-10-31
      • 1970-01-01
      • 2011-01-24
      • 2020-05-28
      • 1970-01-01
      • 2014-01-28
      • 2014-01-23
      • 2018-08-07
      相关资源
      最近更新 更多