简体   繁体   English

Django model 无法通过管理界面工作

[英]Django model not working through admin interface

I am working on a auction site using django project.我正在使用 django 项目在拍卖网站上工作。 I have a model for placing bid.我有一个 model 用于投标。 I have registered the model in admin.py, but when I try to place bid from admin interface it is not updating in the main website page.我已经在 admin.py 中注册了 model,但是当我尝试从管理界面出价时,它并没有在主网站页面中更新。 But through website the bid is updating perfectly and other models are working perfectly.但是通过网站,出价正在完美更新,其他模型也运行良好。

models.py:模型.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()

admin.py:管理员.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)

This is the function for bidsubmit in views.py (I have imported Bid model in this file):这是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')

Make sure that you have registered your app in your project settings.py (append your app name which includes your Bid model in INSTALLED_APPS ) and then run it's related management commands (ie manage.py makemigrations and manage.py migrate ).确保您已在项目settings.py中注册您的应用程序(在INSTALLED_APPS中附加您的应用程序名称,其中包括您的Bid model),然后运行它的相关管理命令(即manage.py makemigrationsmanage.py migrate )。 I hope this will solve your problem.我希望这能解决你的问题。

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

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