Django模型中是否有任何字段,用户没有填充,但是计算机可以填写? (CS50项目2)

发布于 2025-01-30 19:51:14 字数 1994 浏览 2 评论 0原文

我创建了一个Django表单,该表格可为列表创建并保存评论。我希望该评论仅保存到对其进行评论的列表中(现在它保存到所有列表中)。有什么方法可以告诉网页要将其保存到哪个列表以及哪个用户写了评论?

Models.py

class Comments(models.Model):
    comment = models.CharField(max_length=500)

Views.py

@login_required(login_url='login')
def listing(request, id):
    listing = Listings.objects.get(id=id)
    form = CommentForm
    if request.method == "POST":
        form = CommentForm(request.POST)
        if form.is_valid():
            comment = form.save()
            comment.save()
        else:
            return render(request, "auctions/listing.html",{
               "auction_listing": listing,
               "form": form,
               "comments": Comments.objects.all() 
               })
    return render(request, "auctions/listing.html",{
        "auction_listing": listing,
        "form": form,
        "comments": Comments.objects.all()
    })

HTML

<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 %}

感谢您的所有帮助!

这是我现在尝试保存新评论时收到的错误。让我知道您是否需要查看评论表格。

IntegrityError at /listing/1/
NOT NULL constraint failed: auctions_comments.listing_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.listing_id

I have created a django form that creates and saves comments to listings. I want the comment to only save to the listing that it is commented to (right now it saves to all the listings). Is there any way to tell the webpage which listing to save it to and which user wrote the comment?

models.py

class Comments(models.Model):
    comment = models.CharField(max_length=500)

views.py

@login_required(login_url='login')
def listing(request, id):
    listing = Listings.objects.get(id=id)
    form = CommentForm
    if request.method == "POST":
        form = CommentForm(request.POST)
        if form.is_valid():
            comment = form.save()
            comment.save()
        else:
            return render(request, "auctions/listing.html",{
               "auction_listing": listing,
               "form": form,
               "comments": Comments.objects.all() 
               })
    return render(request, "auctions/listing.html",{
        "auction_listing": listing,
        "form": form,
        "comments": Comments.objects.all()
    })

html

<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 %}

Thanks for all the help!

This is the error I am now receiving when I try to save a new comment. Let me know if you need to see the comment form.

IntegrityError at /listing/1/
NOT NULL constraint failed: auctions_comments.listing_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.listing_id

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

一百个冬季 2025-02-06 19:51:14

我希望该评论仅保存到列表中,该列表将其评论给 - ,以便将评论保存到当前或特定的清单中,您可以在评论和评论之间创建关系清单。

更新您的评论模型

class Comments(models.Model):
    listing = models.ForeignKey(Listings, on_delete=models.CASCADE) # for one-to-many relation
    comment = models.CharField(max_length=500)

,并在保存您的表格通过列表对象时,并在使用当前列表进行过滤时,

@login_required(login_url='login')
def listing(request, id):
    listing = Listings.objects.get(id=id)
    comment_obj = Comments.objects.filter(listing=listing) 
    form = CommentForm() # you have to initialize form properly
    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
    })

我建议您查看 foreferkey.related_name_name

I want the comment to only save to the listing that it is commented to - In order to save comments to current or specific listing you've to create a relation between comment and listing.

Update your comment model like this

class Comments(models.Model):
    listing = models.ForeignKey(Listings, on_delete=models.CASCADE) # for one-to-many relation
    comment = models.CharField(max_length=500)

and while saving your form pass listing object and while rendering filter it with current listing

@login_required(login_url='login')
def listing(request, id):
    listing = Listings.objects.get(id=id)
    comment_obj = Comments.objects.filter(listing=listing) 
    form = CommentForm() # you have to initialize form properly
    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
    })

I'll suggest to look at ForeignKey.related_name

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文