【问题标题】:How to fix Django-admin SAVE button problem?如何修复 Django-admin SAVE 按钮问题?
【发布时间】:2019-08-05 06:46:37
【问题描述】:

当我保存一个新条目时,我收到了这个巨大的错误,最后一行显示You're seeing this error because you have DEBUG = True in your Django settings file. Change that to False, and Django will display a standard page generated by the handler for this status code.

当我这样做时,我收到另一个错误CommandError: You must set settings.ALLOWED_HOSTS if DEBUG is False.

我正在按照教程学习 Django,我想我已经按照教程中的说明完成了

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',
    'products.apps.ProductsConfig'
]

settings.py的sn-p

from django.contrib import admin
from .models import Product

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

admin.py

from django.db import models


class Product(models.Model):
    name = models.CharField(max_length=255)
    price = models.FloatField() 
    stock = models.IntegerField()
    image_url = models.CharField(max_length=2083)

class Offer(models.Model):
    code = models.CharField(max_length=10)
    description = models.CharField(max_length=255)
    discount = models.FloatField()    

models.py

This is where I give my entry

This is when I click the save button

【问题讨论】:

    标签: django-admin


    【解决方案1】:

    您没有说明调试显示的是哪个错误,但是您需要阅读调试并解决该错误。

    同时,由于您已关闭调试,您仍然希望您的应用运行,尽管它仍然会出现上述错误。

    Django 必须知道在 runserver 中运行应用程序的主机,作为重定向保护机制。如果 ALLOWED_HOSTS 为空,则仅假定 localhost,因此只能在同一服务器上的浏览器或代理中工作。

    要启用 runserver,请将您服务器上的 IP 地址设置为 (项目目录)/settings.py ALLOWED_HOSTS = [ '10.1.1.10', ] # 使用主机的 IP 地址,而不是本示例 IP。

    由于您已关闭调试,您需要重新启动运行服务器 (venv)$ python manage.py runserver 0:8080

    您的端口号可能不同。

    现在您可以从另一个系统浏览到您的应用。

    here也讨论了类似的问题

    【讨论】:

      猜你喜欢
      • 2019-04-12
      • 2011-02-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-06-12
      • 2012-09-08
      • 2011-10-18
      • 1970-01-01
      相关资源
      最近更新 更多