我正在创建一个带有Django后端和React前端的Web。当从前端提交一个对象并试图获取它时,它不工作,除非我重新启动后端,这时它应该立即更新。在管理视图中,它会立即显示出来,但是由于某种原因,它无法获取它,直到我重新启动
这里是设置文件,以防万一
"""
Django settings for backend project.
Generated by 'django-admin startproject' using Django 4.1.1.
For more information on this file, see
https://docs.djangoproject.com/en/4.1/topics/settings/
For the full list of settings and their values, see
https://docs.djangoproject.com/en/4.1/ref/settings/
"""
from pathlib import Path
import os
import xmlrunner
# Build paths inside the project like this: BASE_DIR / 'subdir'.
BASE_DIR = Path(__file__).resolve().parent.parent
MODEL_DIR = os.path.join(
BASE_DIR, 'petRecognition', 'model')
DEMO_PICTURES_DIR = os.path.join(
BASE_DIR, 'petRecognition', 'media')
# Quick-start development settings - unsuitable for production
# See https://docs.djangoproject.com/en/4.1/howto/deployment/checklist/
# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = 'django-insecure-vbkt_liurmfu7$18-v#&n9+!zneamzit*)aeltd+6$*gkpqjgd'
# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = True
ALLOWED_HOSTS = ["*"]
# Application definition
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'api',
'rest_framework',
'corsheaders',
]
MIDDLEWARE = [
'corsheaders.middleware.CorsMiddleware',
'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 = 'backend.urls'
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [],
'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 = 'backend.wsgi.application'
# Database
# https://docs.djangoproject.com/en/4.1/ref/settings/#databases
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': BASE_DIR / 'db.sqlite3',
}
}
# Password validation
# https://docs.djangoproject.com/en/4.1/ref/settings/#auth-password-validators
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
# https://docs.djangoproject.com/en/4.1/topics/i18n/
LANGUAGE_CODE = 'en-us'
TIME_ZONE = 'UTC'
USE_I18N = True
USE_TZ = True
# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/4.1/howto/static-files/
STATIC_URL = 'static/'
# Default primary key field type
# https://docs.djangoproject.com/en/4.1/ref/settings/#default-auto-field
DEFAULT_AUTO_FIELD = 'django.db.models.BigAutoField'
TEST_RUNNER = 'xmlrunner.extra.djangotestrunner.XMLTestRunner'
TEST_OUTPUT_DIR = './backend/test-outputs/'
CORS_ORIGIN_ALLOW_ALL = True
DATA_UPLOAD_MAX_MEMORY_SIZE = 26214400
# Configuración correo SMTP gmail
EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'
EMAIL_HOST = 'smtp.office365.com'
EMAIL_USE_TLS = True
EMAIL_PORT = 587
EMAIL_HOST_USER = 'petfinderpin@outlook.com'
EMAIL_HOST_PASSWORD = 'PIS2000!'
# Actual directory user files go to
MEDIA_ROOT = os.path.join(BASE_DIR, 'mediafiles')
# URL used to access the media
MEDIA_URL = 'media/'
这是我正在检索的视图,它不会在运行时更新
class PublicadosListView(generics.ListAPIView):
queryset = list(Resenya.objects.filter(estado='publicado')) + list(Consulta.objects.filter(estado='publicado'))
permission_classes = []
def get_serializer_class(self):
# Utiliza el serializador adecuado según el tipo de objeto
if isinstance(self.queryset[0], Resenya):
return ResenyaSerializer
elif isinstance(self.queryset[0], Consulta):
return ConsultaSerializer
我试过从管理视图、 Postman 和前端添加该项目,但它只在服务器重新启动时显示。有什么线索吗?
1条答案
按热度按时间hi3rlvi21#
问题出在视图上,因为它被设置为对象的静态列表。使用以下代码修复它: