如何增加Django /模板中的下一条记录

时间:2019-03-03 23:05:01

标签: python django django-models django-templates django-views

index.html中,我可以使用下面的代码在Django中读取表/模型的第一项。但是,在用户单击next按钮之后-我希望它检索我的表/模型的下一项。 (如何/在何处建立此计数器?)

这是views.py

def index(request):
  flash_dict = {'flashcards': Card.objects.get(pk=1)}
  return render(request, 'flash/index.html', context=flash_dict)

这是index.html

      <div class = "jumbotron">
        {% if flashcards %}
          <p class = 'question'>{{ flashcards }}</p>
          <p class = 'answer'>{{ flashcards.flash_answer }}</p>
          <button type="button" class="btn btn-primary" onclick = "flip()">Flip</button>
          <button type="button" class="btn btn-primary">Next</button>
          {% else %}
            <p>NO ACCESS RECORDS FOUND!</p>
          {% endif %}
      </div>

这是models.py

from django.db import models

# Create your models here.

class Card(models.Model):
  flash_question = models.TextField()
  flash_answer = models.TextField()
  objects = models.Manager()

  def __str__(self):
    return self.flash_question

这是urls.py(在基础项目文件夹下)

from django.contrib import admin
from django.urls import path, include
from flash import views

urlpatterns = [
    path('admin/', admin.site.urls),
    path('',views.index,name='index'),
]

这是urls.py(在应用程序文件夹下)

from django.conf.urls import url
from flash import views

urlpatterns = [
  url(r'^$', views.index, name='index')
]

1 个答案:

答案 0 :(得分:0)

完成这项工作的语义上“正确”的方法是通过大量重新编码:

首先,您需要在基本文件夹中修改urls.py,以使抽认卡URL带有抽认卡ID。这样一来,您可以访问Flashcard / 1 /,Flashcard / 2 /等,并查看相应Flashcard的数据。

urls.py

from django.contrib import admin
from django.urls import path, include
from flash import views

urlpatterns = [
    path('admin/', admin.site.urls),
    path('flashcard/<int:card>/',views.index,name='flashcard'), # Here we are adding a variable to the URL pattern, which will be passed to the view
]

接下来,您需要修改视图,以便它从URL中获取抽认卡ID并呈现相关的抽认卡:

views.py

def flashcard(request, card=1): # Your view will now look for the 'card' variable in your URL pattern.
  flash_dict = {'flashcards': Card.objects.get(pk=card)} # Your 'flashcards' variable now holds the card that corresponds to the ID in the URL
  return render(request, 'flash/index.html', context=flash_dict)

然后,我们将在您的Card模型中编写一个方法,该方法将在调用self.get_next时提取下一张卡片。现在,如果您有Card对象,则可以通过调用card.get_next()找到下一张卡片:

models.py

from django.db import models

# Create your models here.

class Card(models.Model):
  flash_question = models.TextField()
  flash_answer = models.TextField()
  objects = models.Manager()

  def __str__(self):
    return self.flash_question

  def get_next(self):
    next = Card.objects.filter(id__gt=self.id).order_by('id').first()
    if next:
        return next
    # If the current card is the last one, return the first card in the deck
    else:
        return Card.objects.all().order_by('id').first()

最后,我们将用链接到新的“抽认卡”视图的href替换模板中的按钮,并按顺序将下一张卡片的ID传递给它:

template.html

<div class = "jumbotron">
        {% if flashcards %}
          <p class = 'question'>{{ flashcards }}</p>
          <p class = 'answer'>{{ flashcards.flash_answer }}</p>
          <!-- Here we link to the 'flashcard' view, and pass it the ID of the next card in the sequence -->
          <a href="{% url 'flashcard' flashcards.get_next.id %}">Next flashcard</a>
          {% else %}
            <p>NO ACCESS RECORDS FOUND!</p>
          {% endif %}
      </div>

现在,请尝试访问/ flashcard / 1以查看一切如何协同工作。