温馨提示:本文翻译自stackoverflow.com,查看原文请点击:django - How to store user review to database separately

django - 如何将用户评论分别存储到数据库

发布于 2020-03-27 10:19:15

我正在尝试将用户评论存储到单独的数据库表中。例如有两个图像(例如image1和image2),如果用户对image1进行评论,我想将用户评论存储至image1表。如果有人对image2进行注释,则数据存储到image2表中。怎么做请帮助我。

view.py

def review(request):
    address = request.session['address']
    image1 = Offers.objects.filter(address=address)
    image2 = Ads.objects.filter(address=address)
    if 'username' in request.session:
        if request.method == 'POST':
            form = Comments(request.POST)
            if form.is_valid():
                review = request.POST.get('review')
                id = request.POST.get('id_off')
                dt = datetime.now().strftime('%Y-%m-%d %H:%M:%S')
                result1 = Image1(review=review, id= id, date_time= dt)
                result2 = Image2(review=review, id= id, date_time= dt)
                result1.save()
                result2.save()
                return render(request, 'index.html', {'image1': image1, 'image2': image2})
            return redirect('/review/')
        return redirect('/review/')
    return redirect('/review/')

index.html

{% for item in image1 %}
              <div class="col-md-4">
                  <div class="divi" style="height: 910px">
                    <img src="{{ item.image.url }}" alt="Images" width="300px" height="200px"/>
                    <p>From Date: {{ item.from_date }}</p>
                    <p>TO Date: {{ item.to_date }}</p>
                    <p>Description: {{ item.description }}</p>
                    <p>Address: {{ item.address }}</p>
                  <!--if this ids ad:
                    <form method="post" action="#">
                        elif:-->
                        <form method="post" action="#">
                        {% csrf_token %}
                        <input type="text" name="id_off" value="{{ item.id }}" style="display: none">
                        <input type="text" name="review">
                        <button type="submit">Send Review</button>
                    </form>
                </div>
              </div>
        {% endfor %}
            </br>
        {% for items in image2 %}
            <div class="col-md-4">
                <div class="divi" style="height: 410px">
            <img src="{{ items.image.url }}" alt="Images" width="300px" height="200px"/>
                    <p>Description: {{ items.description }}</p>
                    <p>Address: {{ items.address }}</p>
                    <p>id: {{ items.id }}</p>
                    <form method="post" action="#">
                        {% csrf_token %}
                        <input type="text" name="id_off" value="{{ items.id }}" style="display: none">
                        <input type="text" name="review">
                        <button type="submit">Send Review</button>
        </form>
                </div>
              </div>
        {% endfor %}

urls.py

urlpatterns = [
    path('review/', views.review, name="review"),

]

查看更多

查看更多

提问者
kumar
被浏览
43
sachin mathew 2019-07-03 21:14

假设你有一个Image模特

class Image(models.Model):
    file=models.ImageField()

和一个Comment模型

class Comment(models.Model):
    comment=models.CharField(max_length=150)

您可以使用Foreignkey定义Image对象和Comment之间的关系

class Comment(models.Model):
    image = models.ForeignKey(Image, on_delete=models.CASCASE)
    comment=models.CharField(max_length=150)

因此,每个图像对象可以有多个注释

发布
问题

分享
好友

手机
浏览

扫码手机浏览