簡體   English   中英

Django model 無法通過管理界面工作

[英]Django model not working through admin interface

我正在使用 django 項目在拍賣網站上工作。 我有一個 model 用於投標。 我已經在 admin.py 中注冊了 model,但是當我嘗試從管理界面出價時,它並沒有在主網站頁面中更新。 但是通過網站,出價正在完美更新,其他模型也運行良好。

模型.py:

from django.contrib.auth.models import AbstractUser
from datetime import datetime
from django.db import models

CHOICES = (
    ('fashion','Fashion'),
    ('tools', 'Tools'),
    ('toys','Toys'),
    ('electronics','Electronics'),
    ('home accesories','Home accessories'),
    ('books','Books'),
)
class User(AbstractUser):
    pass

class Bid(models.Model):
    user = models.CharField(max_length=64)
    title = models.CharField(max_length=64)
    listingid = models.IntegerField()
    bid = models.IntegerField()

class Listing(models.Model):
    owner = models.CharField(max_length=64)
    title = models.CharField(max_length=64)
    description = models.TextField()
    price = models.IntegerField()
    category = models.CharField(max_length=64, choices=CHOICES, default='fashion')
    link = models.CharField(max_length=10000,default=None,blank=True,null=True)
    time = models.CharField(max_length=64, default= datetime.now().strftime(" %d %B %Y %X "))

class Comment(models.Model):
    user = models.CharField(max_length=64)
    time = models.CharField(max_length=64, default= datetime.now().strftime(" %d %B %Y %X "))
    comment = models.TextField()
    listingid = models.IntegerField()

管理員.py:

from django.contrib import admin
from .models import User, Listing, Bid, Comment

# Register your models here.
admin.site.register(Bid)
admin.site.register(User)
admin.site.register(Listing)
admin.site.register(Comment)

這是views.py中用於bidsubmit的function(我在這個文件中導入了Bid model):

def bidsubmit(request,listingid):
    current_bid = Listing.objects.get(id=listingid)
    current_bid = current_bid.price
    if request.method == "POST":
        user_bid = int(request.POST.get('bid'))
        if user_bid > current_bid:
            listing_items = Listing.objects.get(id=listingid)
            listing_items.price = user_bid
            listing_items.save()
            try:
                if Bid.objects.filter(id=listingid):
                    bidrow = Bid.objects.filter(id=listingid)
                    bidrow.delete()
                b = Bid()
                b.user=request.user.username
                b.title = listing_items.title
                b.listingid = listingid
                b.bid = user_bid
                b.save()
            except:
                b = Bid()
                b.user=request.user.username
                b.title = listing_items.title
                b.listingid = listingid
                b.bid = user_bid
                b.save()
            
            response = redirect('auctions:listingpage',id=listingid)
            response.set_cookie('errorgreen','bid successful!!!',max_age=3)
            return response
        else :
            response = redirect('auctions:listingpage',id=listingid)
            response.set_cookie('error','Bid should be greater than current price',max_age=3)
            return response
    else:
        return redirect('auctions:index')

確保您已在項目settings.py中注冊您的應用程序(在INSTALLED_APPS中附加您的應用程序名稱,其中包括您的Bid model),然后運行它的相關管理命令(即manage.py makemigrationsmanage.py migrate )。 我希望這能解決你的問題。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM