由于尝试更新我的某个Django应用程序的admin.py
,我现在收到的服务器错误(500)导致我的网站崩溃。当我从django.contrib导入admin时,我得到以下回溯:
Traceback (most recent call last):
File "/usr/local/lib/python2.7/dist-packages/django/utils/module_loading.py", line 74, in autodiscover_modules
import_module('%s.%s' % (app_config.name, module_to_search))
File "/usr/lib/python2.7/importlib/__init__.py", line 37, in import_module
__import__(name)
ImportError: No module named admin
使用print_stack()为我提供了更深入的见解:
File "/home/ubuntu/gather/src/foodshop/wsgi.py", line 21, in <module>
application = get_wsgi_application()
File "/usr/local/lib/python2.7/dist-packages/django/core/wsgi.py", line 14, in get_wsgi_application
django.setup()
File "/usr/local/lib/python2.7/dist-packages/django/__init__.py", line 18, in setup
apps.populate(settings.INSTALLED_APPS)
File "/usr/local/lib/python2.7/dist-packages/django/apps/registry.py", line 115, in populate
app_config.ready()
File "/usr/local/lib/python2.7/dist-packages/django/contrib/admin/apps.py", line 22, in ready
self.module.autodiscover()
File "/usr/local/lib/python2.7/dist-packages/django/contrib/admin/__init__.py", line 24, in autodiscover
autodiscover_modules('admin', register_to=site)
File "/usr/local/lib/python2.7/dist-packages/django/utils/module_loading.py", line 74, in autodiscover_modules
import_module('%s.%s' % (app_config.name, module_to_search))
File "/usr/lib/python2.7/importlib/__init__.py", line 37, in import_module
__import__(name)
File "/home/ubuntu/gather/src/recipes/admin.py", line 6, in <module>
class RecipesAdmin(admin.ModelAdmin):
File "/home/ubuntu/gather/src/recipes/admin.py", line 9, in RecipesAdmin
traceback.print_stack()
我在我的项目中的其他几个应用程序中使用此模块,所以我不明白为什么它不能用于这个特定的应用程序。你可以在下面看到我非常简单的Python代码,这是一个管理员。
admin.py
from django.contrib import admin
from .models import Recipes
class RecipesAdmin(admin.ModelAdmin):
list_display = ["__unicode__", "title", "rating", "date_modified"]
prepopulated_fields = {"slug": ("SKU",)}
class Meta:
model = Recipes
admin.site.register(Recipes, RecipesAdmin)
urls.py
from django.conf import settings
from django.conf.urls import include, url
from django.conf.urls.static import static
from django.contrib import admin
from django.conf.urls import patterns, url, include
from djangoratings.views import AddRatingFromModel
urlpatterns = [
url(r'^$', 'customers.views.home', name='home'),
url(r'^home/', 'customers.views.home', name='home'),
url(r'^about/', 'recipes.views.about', name='about'),
url(r'^menu/', 'recipes.views.menu', name='menu'),
url(r'^donate/', 'recipes.views.menu', name='menu'),
# url(r'^contact/', 'recipes.views.contact', name='contact'),
url(r'^admin/', include(admin.site.urls)),
url(r'^about/healthy/', 'recipes.views.healthy', name='healthy'),
url(r'^about/premade/', 'recipes.views.premade', name='premade'),
url(r'^about/exceptional/', 'recipes.views.exceptional', name='exceptional'),
url(r'^accounts/', include('registration.backends.default.urls')),
url(r'^cart/', 'cart.views.get_cart', name='get_cart'),
url(r'^add/', 'recipes.views.menu', name='menu'),
# url(r'^add/(?P<product_id>[-\w]+)/id=(?P<quantity>[-\w]+)/$', 'cart.views.add_to_cart', name='shopping-cart-add'),
url(r'^update/(?P<product_id>[-\w]+)/$', 'cart.views.specific_qty', name='shopping-cart-specify'),
url(r'^remove/(?P<product_id>[-\w]+)/$', 'cart.views.remove_from_cart', name='shopping-cart-remove'),
url(r'^subtract/(?P<product_id>[-\w]+)/$', 'cart.views.subtract_from_cart', name='shopping-cart-subtract'),
url(r'^clear_cart/', 'cart.views.clear_cart', name='shopping-cart-clear'),
url(r'^contact/$','contact.views.email',name='email'),
url(r'^feedback/$','contact.views.feedback',name='feedback'),
url(r'^thanks/$', 'contact.views.thanks',name='thanks'),
url(r'^profile/$', 'cart.views.profile', name='profile'),
url(r'^profile/new/$', 'cart.views.new_profile', name='new_profile'),
url(r'^profile/error/$', 'cart.views.qty_error', name='qty_error'),
url(r'^profile/cancel/$', 'cart.views.cancel_sub', name='cancel_sub'),
url(r'^profile/feedback/$', 'recipes.views.feedback', name='feedback'),
url(r'^rate/(?P<object_id>\d+)/(?P<score>\d+)/', AddRatingFromModel(), {
'app_label': 'recipes',
'model': 'recipes',
'field_name': 'rating',
}),
url(r'^charge/$', 'cart.views.charge', name='charge'),
url(r'^subscription/charge/$', 'cart.views.pay', name='pay'),
url(r'^frequently_asked/$', 'cart.views.frequently_asked', name='frequently_asked'),
url(r'^tender/$', 'cart.views.weekly_orders', name='weekly_orders'),
url(r'^tender_postmates/$', 'cart.views.tender_postmates', name='tender_postmates')
] + static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
if settings.DEBUG:
urlpatterns += static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
我确保我在settings.py中导入django.contrib.admin
,在我的urls.py文件中,我有from django.contrib import admin
和url(r'^admin/', include(admin.site.urls))
。这刚开始时我尝试更改上面的admin.py文件;即使在恢复到原始文件后,也会发生服务器错误。不幸的是,这使我的整个网站崩溃了。
我正在使用Ubuntu机器和Apache2服务器。任何见解都会非常感激!
更新1 :即使在执行git reset --hard to origin / master之后,错误仍然存在。
更新2 :我删除了每个.pyc
文件并重新启动了我的Apache2服务器,但问题仍然存在。
更新3 :在manage.py shell中,当我执行>>> from django.contrib import admin as a
时,>>> a
会解析为<module 'django.contrib.admin' from '/usr/local/lib/python2.7/dist-packages/django/contrib/admin/__init__.py'>
。这明确显示模块存在,但问题仍然存在。