我第一次在windows上启动一个新的django项目。我以前在ubuntu和debian上工作过,没有遇到任何问题。但在windows上,我发现安装它是一项艰巨的任务。我终于找到了一个安装教程,它非常有效,直到我开始制作一种显示静态媒体的方法。我根本找不到让它工作的方法,不是用apache的标准django静态媒体处理程序。所以我想我会先把页面的其余部分写出来,或者至少让它发挥作用。但当我为我的urls
制作view.py
时,它在浏览器中一直说"No module named views.py"
。我浏览了一下互联网,找到了很多解决方案并进行了尝试。我在视图所在的目录中放了一个__init__
文件。然后,我试图通过在python shell中运行views.py
来调试它,并得出了以下结果:
Traceback (most recent call last):
File "C:wampwwwmysiteveiws.py", line 5, in <module>
from django.shortcuts import render_to_response
File "C:Python27libsite-packagesdjangoshortcuts__init__.py", line 10, in <module>
from django.db.models.manager import Manager
File "C:Python27libsite-packagesdjangodb__init__.py", line 11, in <module>
if DEFAULT_DB_ALIAS not in settings.DATABASES:
File "C:Python27libsite-packagesdjangoutilsfunctional.py", line 184, in inner
self._setup()
File "C:Python27libsite-packagesdjangoconf__init__.py", line 40, in _setup
raise ImportError("Settings cannot be imported, because environment variable %s is
undefined." % ENVIRONMENT_VARIABLE)
ImportError: Settings cannot be imported, because environment variable
DJANGO_SETTINGS_MODULE is undefined.`
我最终得到了环境变量mysite.settings
,但即使在那时它也不起作用,因为它找不到或不理解它,当我尝试其他事情时,它又回来了。。。我的目录看起来像这个
C:wampwww
-mysite
-static
-top4.png
-__init__.py
-__init__.pyc
-django.wsgi
-settings.py
-settings.pyc
-urls.py
-urls.pyc
-views.py
-wsgi.py
-wsgi.pyc
-templates
-base.html
-start_page
-manage.py
-testmysql.php
-index.php
views.py:
from django.conf import settings
from django.template.loader import get_template
from django.template import Context
from django.http import HttpResponse
from django.shortcuts import render_to_response
from django.http import HttpResponseRedirect
from django.template import RequestContext
def home(request):
return render_to_response('Start_page.html',RequestContext(request))
urls.py
from django.conf.urls import *
from veiws import *
from django.conf import settings
# Uncomment the next two lines to enable the admin:
# from django.contrib import admin
# admin.autodiscover()
urlpatterns = patterns('',
(r'^home/$', home),
)
(r'^site_media/(?P<path>.*)$', 'django.views.static.serve',
{'document_root': settings.STATIC_DOC_ROOT})
设置.py
# Django settings for mysite project.
DEBUG = True
TEMPLATE_DEBUG = DEBUG
ADMINS = (
# ('Your Name', 'your_email@example.com'),
)
MANAGERS = ADMINS
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql', # Add 'postgresql_psycopg2', 'mysql', 'sqlite3' or 'oracle'.
'NAME': 'Ocelot', # Or path to database file if using sqlite3.
'USER': 'root', # Not used with sqlite3.
'PASSWORD': '', # Not used with sqlite3.
'HOST': '', # Set to empty string for localhost. Not used with sqlite3.
'PORT': '', # Set to empty string for default. Not used with sqlite3.
}
}
# Local time zone for this installation. Choices can be found here:
# http://en.wikipedia.org/wiki/List_of_tz_zones_by_name
# although not all choices may be available on all operating systems.
# In a Windows environment this must be set to your system time zone.
TIME_ZONE = 'America/Chicago'
# Language code for this installation. All choices can be found here:
# http://www.i18nguy.com/unicode/language-identifiers.html
LANGUAGE_CODE = 'en-us'
SITE_ID = 1
# If you set this to False, Django will make some optimizations so as not
# to load the internationalization machinery.
USE_I18N = True
# If you set this to False, Django will not format dates, numbers and
# calendars according to the current locale.
USE_L10N = True
# If you set this to False, Django will not use timezone-aware datetimes.
USE_TZ = True
# Absolute filesystem path to the directory that will hold user-uploaded files.
# Example: "/home/media/media.lawrence.com/media/"
MEDIA_ROOT = ''
# URL that handles the media served from MEDIA_ROOT. Make sure to use a
# trailing slash.
# Examples: "http://media.lawrence.com/media/", "http://example.com/media/"
MEDIA_URL = ''
# Absolute path to the directory static files should be collected to.
# Don't put anything in this directory yourself; store your static files
# in apps' "static/" subdirectories and in STATICFILES_DIRS.
# Example: "/home/media/media.lawrence.com/static/"
STATIC_ROOT = 'C:/wamp/www/mysite/static'
STATIC_DOC_ROOT = "C:/wamp/www/mysite/static"
# URL prefix for static files.
# Example: "http://media.lawrence.com/static/"
STATIC_URL = '/static/'
# Additional locations of static files
STATICFILES_DIRS = (
# Put strings here, like "/home/html/static" or "C:/www/django/static".
# Always use forward slashes, even on Windows.
# Don't forget to use absolute paths, not relative paths.
#os.path.join(PROJECT_DIR,'static'),
"C:/wamp/www/mysite/static"
)
# List of finder classes that know how to find static files in
# various locations.
STATICFILES_FINDERS = (
'django.contrib.staticfiles.finders.FileSystemFinder',
'django.contrib.staticfiles.finders.AppDirectoriesFinder',
# 'django.contrib.staticfiles.finders.DefaultStorageFinder',
)
# Make this unique, and don't share it with anybody.
SECRET_KEY = '&16j6qmva-7n00_@i6s17$d^_twu80pzv4l2wbt(^67$4s-c70'
# List of callables that know how to import templates from various sources.
TEMPLATE_LOADERS = (
'django.template.loaders.filesystem.Loader',
'django.template.loaders.app_directories.Loader',
# 'django.template.loaders.eggs.Loader',
)
MIDDLEWARE_CLASSES = (
'django.middleware.common.CommonMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
# Uncomment the next line for simple clickjacking protection:
# 'django.middleware.clickjacking.XFrameOptionsMiddleware',
)
ROOT_URLCONF = 'mysite.urls'
# Python dotted path to the WSGI application used by Django's runserver.
WSGI_APPLICATION = 'mysite.wsgi.application'
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.
"C:/wamp/www/templates"
)
INSTALLED_APPS = (
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.sites',
'django.contrib.messages',
'django.contrib.staticfiles',
# Uncomment the next line to enable the admin:
# 'django.contrib.admin',
# Uncomment the next line to enable admin documentation:
# 'django.contrib.admindocs',
)
# A sample logging configuration. The only tangible logging
# performed by this configuration is to send an email to
# the site admins on every HTTP 500 error when DEBUG=False.
# See http://docs.djangoproject.com/en/dev/topics/logging for
# more details on how to customize your logging configuration.
LOGGING = {
'version': 1,
'disable_existing_loggers': False,
'filters': {
'require_debug_false': {
'()': 'django.utils.log.RequireDebugFalse'
}
},
'handlers': {
'mail_admins': {
'level': 'ERROR',
'filters': ['require_debug_false'],
'class': 'django.utils.log.AdminEmailHandler'
}
},
'loggers': {
'django.request': {
'handlers': ['mail_admins'],
'level': 'ERROR',
'propagate': True,
},
}
}
因此,我无法使用一个简单的页面,也不知道如何使用apache加载静态文件。我们对所有的帮助深表感谢。
编辑:
我试过
set DJANGO_SETTINGS_MODULE = 'mysite.settings'
set DJANGO_SETTINGS_MODULE = 'settings'
wsgi.py
import os
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "mysite.settings")
sys.path.append('C:/wamp/www/mysite')
from django.core.wsgi import get_wsgi_application
application = get_wsgi_application()
管理员.py
import os
import sys
if __name__ == "__main__":
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "mysite.settings")
from django.core.management import execute_from_command_line
execute_from_command_line(sys.argv)
我现在已经尝试设置environmental用户变量,但它似乎删除了自己然后我运行了python.exe manage.py runserver 8000。女巫给了我这个错误:
Traceback (most recent call last):
File "manage.py", line 10, in <module>
execute_from_command_line(sys.argv)
File "c:Python27libsite-packagesdjangocoremanagement__init__.py", line
443, in execute_from_command_line
utility.execute()
File "c:Python27libsite-packagesdjangocoremanagement__init__.py", line
382, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "c:Python27libsite-packagesdjangocoremanagement__init__.py", line
261, in fetch_command
klass = load_command_class(app_name, subcommand)
File "c:Python27libsite-packagesdjangocoremanagement__init__.py", line
69, in load_command_class
module = import_module('%s.management.commands.%s' % (app_name, name))
File "c:Python27libsite-packagesdjangoutilsimportlib.py", line 35, in im
port_module
__import__(name)
File "c:Python27libsite-packagesdjangocoremanagementcommandsrunserver.
py", line 8, in <module>
from django.core.servers.basehttp import AdminMediaHandler, run, WSGIServerE
xception, get_internal_wsgi_application
File "c:Python27libsite-packagesdjangocoreserversbasehttp.py", line 26,
in <module>
from django.views import static
File "c:Python27libsite-packagesdjangoviewsstatic.py", line 95, in <modu
le>
template_translatable = ugettext_noop(u"Index of %(directory)s")
File "c:Python27libsite-packagesdjangoutilstranslation__init__.py", lin
e 75, in gettext_noop
return _trans.gettext_noop(message)
File "c:Python27libsite-packagesdjangoutilstranslation__init__.py", lin
e 48, in __getattr__
if settings.USE_I18N:
File "c:Python27libsite-packagesdjangoutilsfunctional.py", line 184, in
inner
self._setup()
File "c:Python27libsite-packagesdjangoconf__init__.py", line 42, in _set
up
self._wrapped = Settings(settings_module)
File "c:Python27libsite-packagesdjangoconf__init__.py", line 95, in __in
it__
raise ImportError("Could not import settings '%s' (Is it on sys.path?): %s"
% (self.SETTINGS_MODULE, e))
ImportError: Could not import settings 'mysite.settings' (Is it on sys.path?): N
o module named settings
1)将settings.py移动到C:wampwww
。
2) export DJANGO_SETTINGS_MODULE = 'settings'
如果你在Windows上,你可以键入
set DJANGO_SETTINGS_MODULE = 'settings'
但只有当你关上窗户时,它才会起作用。
为了使其永久化,
nbsp nbsp nbsp a) 启动->设置->控制面板->系统或按Win
-Pause
nbsp nbsp nbsp b) 高级系统设置->环境变量->新用户变量:
nbsp nbsp nbsp c) (name)DJANGO_SETTING_MODULE,
nbsp nbsp nbsp nbsp nbsp (值)设置
然后打开新的终端窗口-现有窗口不受影响。
3) 不要通过运行views.py来调试它。它通常不起作用。通过进行调试
python manage.py runserver 8000
当你在C:wampwww
里面的时候。