在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(在 app 文件夹下)
from django.conf.urls import url
from flash import views
urlpatterns = [
url(r'^$', views.index, name='index')
]
使这项工作在语义上“正确”的方法是显着重新连接您的代码:
首先,您需要修改基本文件夹中的 urls.py,以便抽认卡 URL 采用抽认卡 ID。这将允许您访问 flashcard/1/、flashcard/2/ 等并查看相应 flashcard 的数据。
网址.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 并呈现有问题的抽认卡:
视图.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() 找到下一张卡片:
模型.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()
最后,我们将用一个链接到新“抽认卡”视图的 a href 替换模板中的按钮,并将序列中下一张卡片的 ID 传递给它:
模板.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 以查看所有内容如何协同工作。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句