0

我有一个名为的表Trend,其中包含以下字段:

class Trend(models.Model):
   title             = models.CharField(max_length=40)
   short_description = models.CharField(max_length=200)
   long_description  = models.CharField(max_length=2000)
   date              = models.DateField('data published')
   rating            = models.IntegerField()
   picture           = models.ImageField(upload_to='images')
   category          = models.ForeignKey(Geographic, related_name='entries')

我通过发送这张表的数据context并用一个表显示信息。在同一页面中,我有一些表单来创建过滤器,Trends然后在将这些过滤器应用于表的内容之后,我希望允许用户保存该信息。

基本上,我想获得当前的上下文:

trend = Trend.objects.filter(Q(category__country__contains='Denmark'))
return render(request, "trend-list.html", "{"trends":trend})

然后将当前的趋势列表保存到一个新的数据库中,表的模型如下:

class TrendLists(models.Model):
    name = models.CharField(max_length=40, blank=False)
    list = models.ManyToManyField(Trend)
    data = models.DateField()

我的问题是

如何获取页面的当前上下文以将信息保存在数据库中?

我希望我们能一起找到解决办法。

按要求更新。

我无法添加所有代码,因为它太多了,但这是表格的一部分,也是我用来询问趋势列表名称的表格。

<form action="#" method="post">
    <a data-toggle="modal" href="#name_list_trend" class="btn" style="color:white; background: #415dff;">Gem som ny liste</a> <!-- Save the list -->
    <input type="button" value="Vaelg tilfaeldige">
    <input class="input_orange" type="button" value="Sorter efter sogeord">
    <input type="text">
</form>

{% comment %} MODAL window to introduce the name of the list {% endcomment %}
<div id="name_list_trend" class="modal hide fade in" style="display: none;">
    <div class="modal-header">
        <a class="close" data-dismiss="modal">×</a>
        <h3>Do you want to save the current trend list?</h3><br>
        <h3>Introduce the name of the list:</h3>
    </div>
    <form action="#" method="post">{% csrf_token %}
        <div class="modal-body">
            <label for="name_list">Name: </label>
            <input type="text" name="name_trend_list" value="" id="name_list" />
            <input type="text" value="" name="trends" style="display:none;">
        </div>
        <div class="modal-footer">
            <input type="submit"  class="btn btn-success" value="Send >>">
        </div>
    </form>

    <table class="table table-striped" >
        {% comment %} Check if there are elements in the trend list{% endcomment %}
        {% if trends %}
            <thead>
                <tr>
                    <th>Rating</th>
                    <th></th>
                    <th>Title</th>
                    <th>Date</th>
                    <th></th>
                </tr>
            </thead>
        {% endif %}
        <tbody>
            {% for trend in trends %}
                <tr>
                    <td>
                        <div class="trend_star_{{ trend.rating }}"></div>
                    </td>
                    <td>
                        <div class="thumbnail">
                            <img src="{{ MEDIA_URL }}{{ trend.picture }}" title="{{ trend.title }}">
                        </div>
                    </td>
                    <td class="trend_description">
                        <h2>{{ trend.title }}</h2>
                        <div><p>{{ trend.short_description }}</p></div>
                    </td>
                    <td>
                        {{ trend.date|date:"d/m/Y" }}
                    </td>
                    <td>
                        <a href="{% url read_trend trend.pk %}"  class="btn">Read</a>
                    </td>
                </tr>
            {% endfor %}
        </tbody>
    </table>

如您所见,我不知道如何处理表单,因为我不知道如何获取当前上下文trends并保存在Trendlist数据库中。

也许,我以错误的方式思考问题。

4

0 回答 0