这是我的views.py文件:
from django.contrib.auth import authenticate, login, logout
from django.contrib.auth.decorators import login_required
from django.db import IntegrityError
from django.http import HttpResponseRedirect
from django.shortcuts import render
from django.urls import reverse
from .models import *
def index(request):
return render(request, "auctions/index.html", {
"listings": Listing.objects.all()
})
def login_view(request):
if request.method == "POST":
# Attempt to sign user in
username = request.POST["username"]
password = request.POST["password"]
user = authenticate(request, username=username, password=password)
# Check if authentication successful
if user is not None:
login(request, user)
return HttpResponseRedirect(reverse("index"))
else:
return render(request, "auctions/login.html", {
"message": "Invalid username and/or password."
})
else:
return render(request, "auctions/login.html")
def logout_view(request):
logout(request)
return HttpResponseRedirect(reverse("index"))
def register(request):
if request.method == "POST":
username = request.POST["username"]
email = request.POST["email"]
# Ensure password matches confirmation
password = request.POST["password"]
confirmation = request.POST["confirmation"]
if password != confirmation:
return render(request, "auctions/register.html", {
"message": "Passwords must match."
})
# Attempt to create new user
try:
user = User.objects.create_user(username, email, password)
user.save()
except IntegrityError:
return render(request, "auctions/register.html", {
"message": "Username already taken."
})
login(request, user)
return HttpResponseRedirect(reverse("index"))
else:
return index(request)
@login_required(login_url='login')
def create(request):
if request.method == "POST":
title = request.POST["title"]
description = request.POST["description"]
price = request.POST["price"]
picture = request.POST["picture"]
user = User.objects.get(username=request.user)
try:
listing = Listing(item=title, description=description, price=price, image=picture, owner=user)
listing.save()
except IntegrityError:
return render(request, "auctions/register.html", {
"message": "Username already taken."
})
return index(request)
else:
return render(request, "auctions/new_listing.html")
@login_required(login_url='login')
def listing(request, listing):
if request.method == "POST":
bid = request.POST.get("bid")
try:
new_bid = listing.get_bids(time=time, user=request.user, price=bid)
new_bid.save()
except:
return HttpResponseRedirect("ERROR")
listing_info = Listing.objects.get(item=listing)
return render(request, "auctions/listing.html", {
"listing": listing_info
})
else:
listing_info = Listing.objects.get(item=listing)
return render(request, "auctions/listing.html", {
"listing": listing_info
})
@login_required(login_url='login')
def watchlist(request):
user = User.objects.get(username=request.user)
if request.method == "POST":
product = request.POST.get("product")
listing_info = Listing.objects.get(item=product)
watch = Watchlist.objects.filter(product=listing_info, user=user)
if not watch:
try:
new_product = Watchlist(user=user, product=listing_info)
new_product.save()
except:
return render(request, "auctions/listing.html", {
"message": "ERROR."
})
return render(request, "auctions/listing.html", {
"listing": listing_info,
"message": "Added to watchlist",
"collor": "green"
})
else:
return render(request, "auctions/listing.html", {
"listing": listing_info,
"message": "Already in your watchlist",
"collor": "red"
})
else:
products = Watchlist.objects.filter(user=user)
listing_dict = {}
for product in products:
listing_dict[product] = Listing.objects.get(item=product.product)
return render(request, "auctions/watchlist.html", {
"products": products,
"info": listing_dict
})不是最好的,我本可以用更好的方式完成大部分工作,但这就是我所做的。这是我的Models.py代码:
from django.contrib.auth.models import AbstractUser
from django.db import models
class User(AbstractUser):
pass
class Listing(models.Model):
item = models.CharField(max_length=64)
description = models.CharField(max_length=255, blank=True)
price = models.DecimalField(max_digits=10, decimal_places=2)
owner = models.ForeignKey(User, on_delete=models.CASCADE, related_name="owners")
image = models.CharField(max_length=200)
def __str__(self):
return self.item
class Bid(models.Model):
time = models.DateTimeField(auto_now_add=True, blank=True)
user = models.ForeignKey(User, on_delete=models.CASCADE, related_name="user_bids")
price = models.DecimalField(max_digits=10, decimal_places=2)
product = models.ForeignKey(Listing, on_delete=models.CASCADE, related_name="product_bids")
def __str__(self):
return f"{self.user} put a bid in for {self.price}"
class Watchlist(models.Model):
user = models.ForeignKey(User, on_delete=models.CASCADE, related_name="watchlist")
product = models.ForeignKey(Listing, on_delete=models.CASCADE, related_name="products")
def __str__(self):
return f"{self.product} by {self.user}"发布于 2021-04-29 08:24:12
我不知道你发这个问题的时候是不是把代码弄错了,但是你的代码缩进得很厉害。您为User和Listing模型添加了两个制表符空间。我只是删除了一个制表符空格,并将其恢复为正常的缩进。
from django.contrib.auth.models import AbstractUser
from django.db import models
class User(AbstractUser):
pass
class Listing(models.Model):
item = models.CharField(max_length=64)
description = models.CharField(max_length=255, blank=True)
price = models.DecimalField(max_digits=10, decimal_places=2)
owner = models.ForeignKey(User, on_delete=models.CASCADE, related_name="owners")
image = models.CharField(max_length=200)
def __str__(self):
return self.item
class Bid(models.Model):
time = models.DateTimeField(auto_now_add=True, blank=True)
user = models.ForeignKey(User, on_delete=models.CASCADE, related_name="user_bids")
price = models.DecimalField(max_digits=10, decimal_places=2)
product = models.ForeignKey(Listing, on_delete=models.CASCADE, related_name="product_bids")
def __str__(self):
return f"{self.user} put a bid in for {self.price}"
...发布于 2021-04-29 10:01:59
我按原样复制了您的整个代码,到目前为止没有任何错误。
你试过跑步吗?
python manage.py makemigrations auctions然后
python manage.py migrate然后返回到
python manage.py runserverhttps://stackoverflow.com/questions/67309419
复制相似问题