运行 Django 服务器时出现错误请求或路径错误

时间:2021-04-17 20:50:10

标签: django django-models django-rest-framework django-forms django-templates

我正在创建一个 Django 项目,在该项目下我创建了一个名为 products 的应用程序。我在这个 models.py 应用程序的 products 文件中放入了一些代码,即

from django.db import models

# Create your models here.

class Product(models.Model):
    name = models.CharField(max_length=120)
    image = models.ImageField(upload_to='products', default='bob.png')
    price = models.FloatField(help_text='in US Dollars $')
    created = models.DateTimeField(auto_now_add=True)
    updated = models.DateTimeField(auto_now=True)

    def __str__(self):
        return f"{self.name}"

之后,我在 admin.py 应用程序的 products 文件中注册了该类。代码如下:

from django.contrib import admin
from .models import Product

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

之后我发出了命令:

python manage.py makemigrations python manage.py migrate python manage.py runserver

在运行最后一条命令后,我收到了 error1 中提到的错误。之后,我将 DEBUG 模式更改为 False,但之后我收到了 error2 中提到的错误。我什至将 settings.py 文件中的 ALLOWED_HOSTS 字段值更改为 ['*'] 或 ['127.0.0.1'] 但我仍然得到 Bad Request error

请帮助我解决这个问题。

谢谢

1 个答案:

答案 0 :(得分:0)

您实际上是在浏览器中访问页面时收到错误消息,这意味着 runserver 命令工作正常。您会收到错误消息,因为没有设置索引 (/) 路由。

如果您想访问 Django 的管理界面,请使用 http://127.0.0.1:8000/admin/