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

时间:2019-07-03 11:49:24

标签: django django-templates

我正在尝试将用户评论存储到单独的数据库表中。例如有两个图像(例如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"),

]

1 个答案:

答案 0 :(得分:0)

假设您有一个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)

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