Skip to main content
 首页 » 编程设计

python之Django 寻找不同项目的 URL

2025年05月04日31powertoolsteam

我在 Django 中同时进行两个项目。今天在切换项目后发生了一件非常奇怪的事情。

我有我的第一个项目:urls.py 和 manage.py

from django.conf.urls import patterns, include, url 
from django.contrib import admin 
from django.conf import settings 
 
 
from django.contrib import admin 
admin.autodiscover() 
urlpatterns = patterns('', 
    # Examples: 
    # url(r'^$', 'agenda.views.home', name='home'), 
    # url(r'^blog/', include('blog.urls')), 
 
    url(r'^admin/', include(admin.site.urls)), 
    url(r'^agenda/', include('modulo_agenda.urls')), 
    url(r'^schedule/', include('schedule.urls')), 
 
) 
 
 
""" 
Django settings for agenda project. 
 
For more information on this file, see 
https://docs.djangoproject.com/en/1.7/topics/settings/ 
 
For the full list of settings and their values, see 
https://docs.djangoproject.com/en/1.7/ref/settings/ 
""" 
 
 
# Build paths inside the project like this: os.path.join(BASE_DIR, ...) 
import os 
PROJECT_ROOT = os.path.abspath( 
    os.path.join(os.path.dirname(__file__), ".."), 
) 
BASE_DIR = os.path.dirname(os.path.dirname(__file__)) 
TEMPLATE_PATH = os.path.join(BASE_DIR, 'templates') 
TEMPLATE_DIRS = ( 
    # Put strings here, like "/home/html/django_templates" or "C:/www/django/templates". 
    # Always use forward slashes, even on Windows. 
    # Don't forget to use absolute paths, not relative paths. 
    TEMPLATE_PATH, 
) 
PROJECT_DIR = os.path.abspath(os.path.dirname(__file__)) 
 
# Quick-start development settings - unsuitable for production 
# See https://docs.djangoproject.com/en/1.7/howto/deployment/checklist/ 
 
# SECURITY WARNING: keep the secret key used in production secret! 
SECRET_KEY = '@7j23xm3jv=(#gicejabv2ppa$063st+d@)2x^thld0(#!chwq' 
 
# SECURITY WARNING: don't run with debug turned on in production! 
DEBUG = True 
 
TEMPLATE_DEBUG = True 
 
ALLOWED_HOSTS = [] 
 
 
# Application definition 
 
INSTALLED_APPS = ( 
    'django.contrib.admin', 
    'django.contrib.auth', 
    'django.contrib.contenttypes', 
    'django.contrib.sessions', 
    'django.contrib.messages', 
    'django.contrib.staticfiles', 
    'modulo_agenda', 
    'schedule', 
  'djangobower', 
) 
 
MIDDLEWARE_CLASSES = ( 
    'django.contrib.sessions.middleware.SessionMiddleware', 
    'django.middleware.common.CommonMiddleware', 
    'django.middleware.csrf.CsrfViewMiddleware', 
    'django.contrib.auth.middleware.AuthenticationMiddleware', 
    'django.contrib.auth.middleware.SessionAuthenticationMiddleware', 
    'django.contrib.messages.middleware.MessageMiddleware', 
    'django.middleware.clickjacking.XFrameOptionsMiddleware', 
    'django.middleware.locale.LocaleMiddleware', 
) 
 
ROOT_URLCONF = 'agenda.urls' 
 
WSGI_APPLICATION = 'agenda.wsgi.application' 
 
 
# Database 
# https://docs.djangoproject.com/en/1.7/ref/settings/#databases 
 
DATABASES = { 
    'default': { 
        'ENGINE': 'django.db.backends.sqlite3', 
        'NAME': os.path.join(BASE_DIR, 'db.sqlite3'), 
    } 
} 
 
# Internationalization 
# https://docs.djangoproject.com/en/1.7/topics/i18n/ 
 
LANGUAGE_CODE = 'en-us' 
 
TIME_ZONE = 'UTC' 
 
USE_I18N = True 
 
USE_L10N = True 
 
USE_TZ = True 
 
MEDIA_ROOT = os.path.join(PROJECT_DIR, "site_media") 
ADMIN_MEDIA_PREFIX = '/media/' 
MEDIA_URL = '/site_media/' 
# Static files (CSS, JavaScript, Images) 
# https://docs.djangoproject.com/en/1.7/howto/static-files/ 
 
STATIC_URL = '/static/' 
STATIC_ROOT = os.path.join(PROJECT_ROOT, 'static') 
 
TEMPLATE_CONTEXT_PROCESSORS = ( 
 
 
"django.contrib.auth.context_processors.auth", 
"django.core.context_processors.request", 
 
) 
 
STATICFILES_FINDERS = ( 
        'django.contrib.staticfiles.finders.FileSystemFinder', 
    'django.contrib.staticfiles.finders.AppDirectoriesFinder', 
'djangobower.finders.BowerFinder', 
) 
BOWER_PATH = 'C:/Python34/Lib/site-packages' 
BOWER_COMPONENTS_ROOT = os.path.join(PROJECT_ROOT, "components") 
BOWER_INSTALLED_APPS = ( 
    'jquery', 
    'bootstrap' 
) 
FIRST_DAY_OF_WEEK = 1 # Monday 
ROOT_URLCONF = 'agenda.urls' 

还有我的第二个项目设置和 url:

from django.conf.urls import patterns, include, url 
from django.contrib import admin 
 
urlpatterns = patterns('', 
    # Examples: 
    # url(r'^$', 'proyecto_final_web.views.home', name='home'), 
    # url(r'^blog/', include('blog.urls')), 
 
    url(r'^admin/', include(admin.site.urls)), 
    url(r'^music/', include('music_manager.urls')), 
) 
 
 
 
""" 
Django settings for proyecto_final_web project. 
 
For more information on this file, see 
https://docs.djangoproject.com/en/1.7/topics/settings/ 
 
For the full list of settings and their values, see 
https://docs.djangoproject.com/en/1.7/ref/settings/ 
""" 
 
# Build paths inside the project like this: os.path.join(BASE_DIR, ...) 
import os 
BASE_DIR = os.path.dirname(os.path.dirname(__file__)) 
 
 
# Quick-start development settings - unsuitable for production 
# See https://docs.djangoproject.com/en/1.7/howto/deployment/checklist/ 
 
# SECURITY WARNING: keep the secret key used in production secret! 
SECRET_KEY = '=%65sc*)pue#)wj&pxd#meh3s_v(^s***+ns(p*8_@pjla_+xo' 
 
# SECURITY WARNING: don't run with debug turned on in production! 
DEBUG = True 
 
TEMPLATE_DEBUG = True 
 
ALLOWED_HOSTS = [] 
 
 
# Application definition 
 
INSTALLED_APPS = ( 
    'django.contrib.admin', 
    'django.contrib.auth', 
    'django.contrib.contenttypes', 
    'django.contrib.sessions', 
    'django.contrib.messages', 
    'django.contrib.staticfiles', 
) 
 
MIDDLEWARE_CLASSES = ( 
    'django.contrib.sessions.middleware.SessionMiddleware', 
    'django.middleware.common.CommonMiddleware', 
    'django.middleware.csrf.CsrfViewMiddleware', 
    'django.contrib.auth.middleware.AuthenticationMiddleware', 
    'django.contrib.auth.middleware.SessionAuthenticationMiddleware', 
    'django.contrib.messages.middleware.MessageMiddleware', 
    'django.middleware.clickjacking.XFrameOptionsMiddleware', 
) 
 
ROOT_URLCONF = 'proyecto_final_web.urls' 
 
WSGI_APPLICATION = 'proyecto_final_web.wsgi.application' 
 
 
# Database 
# https://docs.djangoproject.com/en/1.7/ref/settings/#databases 
 
DATABASES = { 
    'default': { 
        'ENGINE': 'django.db.backends.sqlite3', 
        'NAME': os.path.join(BASE_DIR, 'db.sqlite3'), 
    } 
} 
 
# Internationalization 
# https://docs.djangoproject.com/en/1.7/topics/i18n/ 
 
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/1.7/howto/static-files/ 
 
STATIC_URL = '/static/' 
TEMPLATE_PATH=os.path.join(BASE_DIR,  'templates') 
TEMPLATE_DIRS = (TEMPLATE_PATH,) 
STATIC_PATH = os.path.join(BASE_DIR,'static') 
 
STATICFILES_DIRS = ( 
    STATIC_PATH, 
) 

我遇到的问题是,当我输入运行服务器时,我得到了我的议程项目:

System check identified no issues (0 silenced). 
August 30, 2015 - 13:33:04 
Django version 1.7, using settings 'agenda.settings' 
Starting development server at http://127.0.0.1:8000/ 
Quit the server with CTRL-BREAK. 

它说它正在使用 agenda.settings,其中有 ROOT_URLCONF = 'agenda.urls'

但是当尝试转到 URL localhots:8000/schedule 时,我收到此错误:

Using the URLconf defined in proyecto_final_web.urls, Django tried these URL patterns, in this order: 
^admin/ 
^music/ 
The current URL, schedule/, didn't match any of these. 

哪些是我在其他项目中定义的URL。我不明白为什么会发生这种情况,因为在运行服务器上它说它使用的是 agenda.settings 而不是 proyecto_final_web.settings。

谁能告诉我可能会发生什么?

请您参考如下方法:

从上面的代码来看,PROJECT_ROOT 设置似乎有错误。一个项目定义了 PROJECT_ROOT 设置,而其他项目似乎没有此设置。

您需要确保为这两个项目正确定义了 PROJECT_ROOT。