我results.html寫道,如何在html文件中顯示模型數據的內容?
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Score</title>
</head>
<body>
<h1>Score</h1>
<h2>Your score is {{ scoreresults.result }}</h2>
</body>
</html>
但現在,這部分的<h2>Your score is {{ user.result }} </h2>
{{ user.result }}
是在瀏覽器中的空白。
我在models.py
from django.db import models
from django.contrib.auth.models import User
class ImageAndUser(models.Model):
user = models.ForeignKey("auth.User", verbose_name="imageforegin")
result = models.CharField(max_length=64, null=True)
def __str__(self):
return '{} {}'.format(self.user,self.id)
寫道所以,ImageAndUser模型結果數據。 我無法理解如何在results.html中指定ImageAndUser模型。 此外, 我serializer.py
from .forms import UserImageForm
from rest_framework import serializers
from .models import ImageAndUser
class ImageSerializer(serializers.ModelSerializer):
class Meta:
model = ImageAndUser
fields =(
'image',
'result',
'user',
'id',
)
read_only_fields = (
'user',
)
def create(self, attrs):
attrs['user'] = self.context.get('request').user
print(attrs)
return super(ImageSerializer,self).create(attrs)
寫道:現在,我在views.py
def scoreresults(request):
d = {
'scoreresults': ImageAndUser.objects.result(),
}
return render(request, 'registration/accounts/results.html', d)
中寫道urls.py
from django.conf.urls import url
from . import views
from django.views.generic import TemplateView
urlpatterns = [
url(r'^scoreresults$', TemplateView.as_view(template_name='registration/accounts/results.html'),
name='tcresults'),
]
但沒有奏效。
那麼,我該如何解決這個問題?
你能展示你的觀點嗎? – GwynBleidD
我沒有寫任何與views.py – user7523656
中的results.html相關的東西所以,何時以及如何使用您的html模板? –