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