我正在嘗試制作一個保存評論的電子商務網站(CS50 專案 2)。評論之前是保存的,但后來我將 ForeignKeys 添加到我的評論模型中以將其鏈接到 Listings 和 User 模型。現在,每當我嘗試保存評論時,都會發生此錯誤。
IntegrityError at /listing/1/
NOT NULL constraint failed: auctions_comments.user_id
Request Method: POST
Request URL: http://127.0.0.1:8000/listing/1/
Django Version: 3.2.5
Exception Type: IntegrityError
Exception Value:
NOT NULL constraint failed: auctions_comments.user_id
并且這行代碼被高亮了comment.save()。
這是我的models.py:
class User(AbstractUser):
pass
class Listings(models.Model):
CATEGORY = [
("Miscellaneous", "Miscellaneous"),
("Movies and Television", "Movies and Television"),
("Sports", "Sports"),
("Arts and Crafts", "Arts and Crafts"),
("Clothing", "Clothing"),
("Books", "Books"),
]
title = models.CharField(max_length=64)
description = models.CharField(max_length=500)
bid = models.DecimalField(max_digits=1000000000000, decimal_places=2)
image = models.URLField(null=True, blank=True)
category = models.CharField(max_length=64, choices=CATEGORY, default=None)
class Comments(models.Model):
listing = models.ForeignKey(Listings, on_delete=models.CASCADE, default="")
user = models.ForeignKey(User, on_delete=models.CASCADE, default="")
comment = models.CharField(max_length=500)
視圖.py
@login_required(login_url='login')
def listing(request, id):
listing = Listings.objects.get(id=id)
comment_obj = Comments.objects.filter(listing=listing)
form = CommentForm()
if request.method == "POST":
form = CommentForm(request.POST)
if form.is_valid():
comment = form.save(commit=False)
comment.listing = listing
comment.save()
else:
return render(request, "auctions/listing.html",{
"auction_listing": listing,
"form": form,
"comments": comment_obj
})
return render(request, "auctions/listing.html",{
"auction_listing": listing,
"form": form,
"comments": comment_obj
})
html
{% block body %}
<img src ="{{ auction_listing.image }}" style = "height: 10%; width: 10%;">
<h4 class = "text">{{ auction_listing.title }}</h4>
<h6>Description: {{ auction_listing.description }}</h6>
<h6>Category: {{ auction_listing.category }}</h6>
<h6>Price: ${{ auction_listing.bid }}</h6>
<form action = "{% url 'listing' auction_listing.id %}" method = "POST">
{% csrf_token %}
{{ form }}
<input type = "submit" value = "Save">
</form>
{% for comment in comments %}
<h6> {{ comment.comment }} </h6>
{% endfor %}
<button type = "button">Add to Watchlist</button>
{% endblock %}
我認為問題在于views.py中的comment.save()和html中的表單,其中顯示“{% url'listing'auction_listing.id %}”,但我不知道如何解決它.
謝謝大家的幫助!
表格.py
class ListingsForm(forms.ModelForm):
class Meta:
model = Listings
fields = ['title', 'description', 'bid', 'image', 'category']
class CommentForm(forms.ModelForm):
class Meta:
model = Comments
fields = ['comment']
uj5u.com熱心網友回復:
我假設用戶用來發表評論的表單沒有讓他們指定他們的listingID 或userID。如果是這種情況,那么兩者listing和userinComments都將為 null,這是導致錯誤的原因,因為不允許外鍵為 null。您必須在視圖中手動填寫這些缺失的資料。看起來您已經確定了串列值并將其添加到表單實體中。你只需要為用戶做同樣的事情。該request引數有一個user欄位,您可以使用它來計算userID。
if form.is_valid():
comment = form.save(commit=False)
comment.listing = listing
comment.user = request.user
comment.save()
轉載請註明出處,本文鏈接:https://www.uj5u.com/qianduan/479882.html
