简体   繁体   English

django 管理数据未在数据库中注册

[英]django admin data does not register in database

I am building an ecommerce website with Django and having some difficulties to transfer data from the admin page to the database and then html.我正在使用 Django 构建一个电子商务网站,并且在将数据从管理页面传输到数据库然后是 html 时遇到了一些困难。 I am new to Django and I would love your help.我是 Django 的新手,我很想得到你的帮助。 I added 7 "products" (class Product) via Django admin page.我通过 Django 管理页面添加了 7 个“产品”(产品类)。

models.py模型.py

from django.db import models
from django.contrib.auth.models import User


class Customer(models.Model):
    user = models.OneToOneField(User, on_delete=models.CASCADE, null=True, blank=True)
    name = models.CharField(max_length=200, null=True)
    email = models.CharField(max_length=200, null=True)

    def __str__(self):
        return self.name


class Product(models.Model):
    name = models.CharField(max_length=200)
    price = models.FloatField()
    
    def __str__(self):
        return self.name


class Order(models.Model):
    customer = models.ForeignKey(Customer, on_delete=models.SET_NULL, null=True, blank=True)
    date_ordered = models.DateTimeField(auto_now_add=True)
    complete = models.BooleanField(default=False)
    transaction_id = models.CharField(max_length=100, null=True)

    def __str__(self):
        return str(self.id)


class OrderItem(models.Model):
    product = models.ForeignKey(Product, on_delete=models.SET_NULL, null=True)
    order = models.ForeignKey(Order, on_delete=models.SET_NULL, null=True)
    quantity = models.IntegerField(default=0, null=True, blank=True)
    date_added = models.DateTimeField(auto_now_add=True)


class ShippingAddress(models.Model):
    customer = models.ForeignKey(Customer, on_delete=models.SET_NULL, null=True)
    order = models.ForeignKey(Order, on_delete=models.SET_NULL, null=True)
    address = models.CharField(max_length=200, null=False)
    city = models.CharField(max_length=100, null=False)
    country = models.CharField(max_length=100, null=False)
    postcode = models.CharField(max_length=100, null=False)
    date_added = models.DateTimeField(auto_now_add=True)

    def __str__(self):
        return self.address


views.py视图.py

from django.shortcuts import render
from .models import *


def products(request):
    products = Product.objects.all()
    context = {'products':products}
    return render(request, 'store/products.html')


def cart(request):
    context = {}
    return render(request, 'store/cart.html')


def checkout(request):
    context = {}
    return render(request, 'store/checkout.html')


def home(request):
    context = {}
    return render(request, 'store/home.html')


admin.py管理员.py

from django.contrib import admin
from .models import *

admin.site.register(Customer)
admin.site.register(Product)
admin.site.register(Order)
admin.site.register(OrderItem)
admin.site.register(ShippingAddress)

In settings.py, the default database is sqlite3在settings.py中,默认数据库是sqlite3

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

Screen shot of Django admin page with products successfully added成功添加产品的 Django 管理页面的屏幕截图

I have done the migrations and the database db.sqlite3 exits in my project folder.我已经完成了迁移,数据库 db.sqlite3 在我的项目文件夹中退出。

However, when I check the values of the table "store_product", it seems empty...但是,当我检查表“store_product”的值时,它似乎是空的......

prompt迅速的


SQLite version 3.33.0 2020-08-14 13:23:32
Enter ".help" for usage hints.
sqlite> .width
sqlite> .mode markdown
sqlite> select * from store_product
   ...>


In my html page where I want to display the products, nothing displays... The variable products is empty:在我想要显示产品的 html 页面中,没有任何显示...变量 products 为空:

products.html产品.html

        {% if products is defined %}
            value of variable: {{ products }}
        {% else %}
            variable is not defined
        {% endif %}

it displays nothing...它什么都不显示...

Thanks a lot for your help.非常感谢你的帮助。

You never pass the context to your view.您永远不会将上下文传递给您的视图。 It should be like:它应该是这样的:

def products(request):
    products = Product.objects.all()
    context = {'products':products}
    return render(request, 'store/products.html', context)

Also that {% if products is defined %} would not make any sense.此外, {% if products is defined %}没有任何意义。 So simply render the products:所以简单地渲染产品:

{% for product in products %}
    Product: {{ product.name }}
    Price: {{ product.price }}
{% empty %}
    No products to display!
{% endfor %}

Your template code looks wrong - is defined is not python.您的模板代码看起来错误 - is defined不是 python。

The following is sufficient:以下内容就足够了:

{% if products %}
<ul>
    {% for product in products %}
    <li>{{ product.name }}: {{ product.price }}
    {% endfor %}
</ul>
{% else %}
    No products.
{% endif %}

The Django Admin shows exactly whatever is in its connected database, there is no cache. Django Admin 可以准确显示其连接的数据库中的任何内容,没有缓存。

If you cannot see the data in the sqlite client or your other view, this makes me suspect that you are not using the same database or that the sqlite lost the data in the meanwhile (deleting the file will recreate it the next time you start your local server, and the data will be lost).如果您在 sqlite 客户端或您的其他视图中看不到数据,这让我怀疑您没有使用相同的数据库或者 sqlite 同时丢失了数据(删除文件将在您下次启动时重新创建它本地服务器,数据将丢失)。

You might want to start using the DB you are planning to use in production, to rule these issues out, and to be closer to your production setup when developing.您可能希望开始使用您计划在生产中使用的数据库,以排除这些问题,并在开发时更接近您的生产设置。

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

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