gpt4 book ai didi

python - 模型未显示在 Django Admin 中

转载 作者:行者123 更新时间:2023-11-28 19:19:45 25 4
gpt4 key购买 nike

我看过很多关于此的帖子并尝试了大部分答案,但没有成功。

我有一个名为 photos 的应用,其 models.py 看起来像这样

from django.db import models

class Photo(models.Model):
name = models.CharField(max_length=200)
location = models.CharField(max_length=200)
description = models.CharField(max_length=200)
def __unicode__(self):
return self.name

应用已安装:

INSTALLED_APPS = (
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'photos',
)

Admin.py 是这样的:

from photos.models import Photo
from django.contrib import admin

class PhotoAdmin(admin.ModelAdmin):
pass

admin.site.register(Photo, PhotoAdmin)

网址.py

from django.conf.urls import patterns, include, url
from django.contrib import admin

admin.autodiscover()

urlpatterns = patterns('',
# Examples:
# url(r'^$', 'garthreckers.views.home', name='home'),
# url(r'^blog/', include('blog.urls')),

url(r'^admin/', include(admin.site.urls)),
)

我已经在 settings.py 和 wsgi.py 上运行了 touch。我已经运行服务器、syncdb、迁移。

如果有帮助,这里是我的目录结构的分割:

garthreckers
|-garthreckers
|-__init__.py
|-__init__.pyc
|-settings.py
|-settings.pyc
|-urls.py
|-wsgi.py
|-wsgi.pyc
|-photos
|-migrations
|-__init__.py
|-__init__.pyc
|-admin.py
|-admin.pyc
|-models.py
|-models.pyc
|-tests.py
|-views.py
|-manage.py

编辑:虚拟主机配置文件

WSGIPythonPath /home/ubuntu/xxxxxxx.com/garthreckers

<VirtualHost *:80>

ServerName www.xxxxxxx.com
ServerAlias xxxxxxx.com

WSGIScriptAlias / /home/ubuntu/xxxxxxx.com/garthreckers/garthreckers/wsgi.py

<Directory /home/ubuntu/xxxxxxx.com/garthreckers/garthreckers>
<Files wsgi.py>
Order deny,allow
Require all granted
</Files>
</Directory>
<Directory /home/ubuntu/xxxxxxx.com/garthreckers/static>
Require all granted
</Directory>
<Directory /home/ubuntu/xxxxxxx.com/garthreckers/media>
Require all granted
</Directory>
</VirtualHost>

最佳答案

您的代码没问题。我设置了一个新的 virtualenv,一个新的 Django 安装,逐字复制了你的代码,Admin 就如预期的那样。我猜不出具体出了什么问题,但是,嘿!好消息你的代码没问题。重新开始很快,所以也许值得一试。删除所有 .pyc 文件,暂时不用担心 Apache。一旦您知道项目设置正在运行,那么您就可以担心 Apache 托管的下一步了。这是我运行的命令:

django-admin startproject garthreckers
django-admin startapp photos
# copied your code into appropriate files
./manage.py makemigrations
./manage.py migrate
./manage.py createsuperuser
./manage.py runserver

小贴士:从 Django 1.7 开始,您不需要 admin.autodiscover()( documentation )。

关于python - 模型未显示在 Django Admin 中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28269519/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com