簡體   English   中英

django charfield 未顯示在管理面板中

[英]django charfield not showing in admin panel

我在 django 中創建一個 model 並在 admin.py 中注冊它,當我 go 到管理面板時它顯示 model 但是當我想創建 object 時它不顯示字符字段,我可以創建沒有任何細節的對象

這是我下面的代碼

查看.py

from django.shortcuts import render
from django.http import HttpResponse
from .models import Feature

# Create your views here.
def index(request):
    features = Feature.objects.all()
    return render(request, 'index.html', {'features' : features})

model.py

from django.db import models

# Create your models here.
class Feature(models.Model):
    name: models.CharField(max_length=100)
    details: models.CharField(max_length=200)

靜態.py

"""
Django settings for myproject project.

Generated by 'django-admin startproject' using Django 3.2.12.

For more information on this file, see

For the full list of settings and their values, see
 """

from pathlib import Path
import os

# Build paths inside the project like this: BASE_DIR / 'subdir'.
BASE_DIR = Path(__file__).resolve().parent.parent


# Quick-start development settings - unsuitable for production

# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = 'django-insecure-dubzu2qq@tk9lk%d05a*@j1rd1hkr$v72eiga+*u7%v2d)19_5'

# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = True

ALLOWED_HOSTS = []


# Application definition

INSTALLED_APPS = [
    'livesync',
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'myapp'
]

MIDDLEWARE = [
    'livesync.core.middleware.DjangoLiveSyncMiddleware',
    '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 = 'myproject.urls'

TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [BASE_DIR, 'templates'],
        '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 = 'myproject.wsgi.application'


# Database

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.sqlite3',
        'NAME': BASE_DIR / 'db.sqlite3',
    }
}


# Password validation

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',
    },
]


# Internationalization

LANGUAGE_CODE = 'en-us'

TIME_ZONE = 'UTC'

USE_I18N = True

USE_L10N = True

USE_TZ = True


# Static files (CSS, JavaScript, Images)

STATIC_URL = '/static/'
STATICFILES_DIRS = (os.path.join(BASE_DIR, 'static'),)

# Default primary key field type

DEFAULT_AUTO_FIELD = 'django.db.models.BigAutoField'

管理員.py

    from django.contrib import admin
from .models import Feature


# Register your models here.
admin.site.register(Feature)

此屏幕截圖來自我的管理面板 > 添加功能 (myModel),它什么也沒顯示。

管理面板的屏幕截圖> 添加功能

您正在使用@dataclass方式來定義您的 model。我不知道這會起作用……它應該是:

class Feature(models.Model):
    name = models.CharField(max_length=100)
    details = models.CharField(max_length=200)

另外,你有沒有運行遷移? (我懷疑不是,或者我認為您會在這里看到錯誤)。 當您創建或修改 model 定義時,您應該運行python manage.py makemigrations ,然后運行 python manage.py python manage.py migrate - 這就是在數據庫中創建/更改它的原因,如果沒有它,無論如何都不會在管理員中顯示太多...

如果您運行 migrate 它可能會創建一個“空” model ,因為您缺少= - 我真的不知道這段代碼在沒有測試的情況下會做什么。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM