简体   繁体   English

django charfield 未显示在管理面板中

[英]django charfield not showing in admin panel

i create a model in django and register it in admin.py when i go to admin panel it shows the model but when i want to create object it doesn't show the charfields and i can just create the oject without any details我在 django 中创建一个 model 并在 admin.py 中注册它,当我 go 到管理面板时它显示 model 但是当我想创建 object 时它不显示字符字段,我可以创建没有任何细节的对象

this is my codes below这是我下面的代码

view.py查看.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 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)

stting.py静态.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'

admin.py管理员.py

    from django.contrib import admin
from .models import Feature


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

this screenshot is from my admin panel > add Feature (myModel) and it shows nothing.此屏幕截图来自我的管理面板 > 添加功能 (myModel),它什么也没显示。

screenshot of admin panel> add Feature管理面板的屏幕截图> 添加功能

You are using a @dataclass way of defining your model. I'm not aware that will work... It should be:您正在使用@dataclass方式来定义您的 model。我不知道这会起作用……它应该是:

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

Also, have you run migrations?另外,你有没有运行迁移? (I suspect not or I think you'd have seen an error here). (我怀疑不是,或者我认为您会在这里看到错误)。 You should run python manage.py makemigrations followed by python manage.py migrate when you create or amend a model definition - this is what creates/changes it in the DB, and without that there's not a lot to display in admin anyway...当您创建或修改 model 定义时,您应该运行python manage.py makemigrations ,然后运行 python manage.py python manage.py migrate - 这就是在数据库中创建/更改它的原因,如果没有它,无论如何都不会在管理员中显示太多...

It's possible if you ran migrate it created an "empty" model maybe, since you were missing the = - I really don't know exactly what this code would do without testing it.如果您运行 migrate 它可能会创建一个“空” model ,因为您缺少= - 我真的不知道这段代码在没有测试的情况下会做什么。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM