我试图通过" ReportLab PDF"在python-django中打印项目。我有这样的错误:
/ error / 8' int'中的属性错误对象没有属性' decode'
下面是重要的代码
models.py
class Games(models.Model):
title = models.CharField(max_length=50)
type = models.ForeignKey("Types")
date_of_production = models.DateField()
producer = models.ForeignKey("Producers")
availability = models.BooleanField()
account = models.IntegerField()
language = models.ForeignKey("Languages", default='')
price = models.CharField(max_length=10)
abbr = models.CharField(max_length=5)
def __str__(self):
return self.title
forms.py
from django import forms
class GamesForm(forms.Form):
title = forms.CharField(label= "Nazwa gry", max_length=50)
type = forms.CharField(label= "Typ gry", max_length=10)
date_of_production = forms.DateField(label= "Data produkcji")
producer = forms.CharField(label= "Nazwa producenta", max_length= 10)
availability = forms.BooleanField(label= "Czy jest dostępne?")
account = forms.IntegerField(label= "Ilość dostępnych sztuk")
language = forms.CharField(label= "Języki", max_length=50)
price = forms.CharField(label= "Cena", max_length=10)
abbr = forms.CharField(label= "Skrót waluty", max_length=5)
views.py
from django.shortcuts import render, redirect
from django.http import HttpResponse
from django.http import HttpResponseRedirect
from django.http import Http404
import datetime
from .forms import *
from .models import Games
from .models import Producers
from .models import Types
from .models import Languages
from django.contrib.auth.decorators import login_required
from reportlab.pdfgen import canvas
def printgame (request,id):
#try:
game = Games.objects.get(pk=id)
response = HttpResponse(content_type='application/pdf')
response['Content-Deposition'] = 'filename="report.pdf"'
page = canvas.Canvas(response)
page.setLineWidth(.3)
page.setFont('Verdana', 12)
page.drawString(30,750, "Wydruk gry: ")
page.drawString(30,700, "Tytuł gry: ")
page.drawString(100,700, game.title )
page.drawString(30,650, "Typ gry: ")
page.drawString(130,650, game.type)
page.drawString(30,600, "Data produkcji: ")
#page.drawString(150,600, game.date_of_production)
page.drawString(30,550, "Dostępność: ")
#page.drawString(130,550, game.availability)
page.drawString(30,500, "Ilość: ")
page.drawString(130,500, game.account)
page.drawString(30,450, "Język: ")
page.drawString(130,450, game.language)
page.drawString(30,400, "Cena: ")
page.drawString(130,400, game.price)
page.drawString(30,350, "Skrót waluty: ")
page.drawString(130,350, game.abbr)
page.line(30, 325, 300, 325)
page.drawString(30,250, "Dane techniczne ")
page.drawString(30,200, "ID gry:")
page.drawString(200,200, id)
page.showPage()
page.save()
return response
list.html
<h1>Lista gier</h1>
<table border=1>
<tr>
<th>Nazwa gry</th>
<th>Typ gry</th>
<th>Data produkcji</th>
<th>Nazwa producenta</th>
<th>Czy jest dostępne?</th>
<th>Ilość dostępnych sztuk</th>
<th>Języki</th>
<th>Cena</th>
<th>Skrót waluty</th>
<th>Edycja</th>
<th>Usuwanie</th>
<th>Druk PDF</th>
</tr>
{% for Games in all_games %}
<tr>
<th><a href="/view/{{Games.id}}">{{Games.title}}</a></th>
<th>{{Games.type}}</th>
<th>{{Games.date_of_production}}</th>
<th>{{Games.producer}}</th>
<th>{{Games.availability}}</th>
<th>{{Games.account}}</th>
<th>{{Games.language}}</th>
<th>{{Games.price}}</th>
<th>{{Games.abbr}}</th>
<td><a href="/edit/{{Games.id}}">Edycja</td>
<td><a href="/delete/{{Games.id}}">Usuń</td>
<td><a href="/print/{{Games.id}}">PDF</td>
</tr>
{% endfor %}
</table>
<a href="/new/">Nowa gra</a>
{% endblock %}
new.html
{% extends 'main/layout.html' %}
{% block content %}
<h1>Nowa gra</h1>
<form action="/new/" method="post">
{% csrf_token %}
Nazwa gry:
<input type="text" name="title"> </input></br>
Typ gry:
<input type="text" name="type"> </input></br>
Data produkcji:
<input type="date" name="date_of_production"> </input></br>
Nazwa producenta:
<input type="text" name="producer"> </input></br>
Czy jest dostępne?
<input type="checkbox" name="availability"> </input></br>
Ilość dostępnych sztuk:
<input type="number" name="account"> </input></br>
Języki:
<input type="text" name="language"> </input></br>
Cena:
<input type="number" name="price"> </input></br>
Skrót waluty:
<input type="text" name="abbr"> </input></br>
<input type="submit" value="Dodaj"></input>
</form>
{% endblock %}
启动此网站后,显示行错误:
page.drawString(130,450, game.account)
我想展示游戏的帐号。但我不知道该怎么做以及如何做到这一点。有任何想法吗?每个建议都要求