我是 Python 和 Django 的新手。我正在尝试为我的个人项目建立一个网站,您可以在其中上传和查看pdf文件,但会出错。有人请帮助我确定我需要在哪里进行纠正。
这是我的模特。py
from django.db import models
from datetime import datetime
class PDF(models.Model):
pdffile = models.FileField(upload_to='PDFs/')
Title = models.CharField(max_length=500)
Subject = models.CharField(max_length=500)
created_at = models.DateTimeField(default=datetime.now, blank=True)
def __str__(self):
return self.Subject
views.py
from django.shortcuts import render
from django.http import HttpResponse
from .models import PDF
def index(request):
return HttpResponse("<h1>This is the Home Page</h1>")
def pdf_view(request, PDF_id):
a = PDF.objects.get(id=id)
with open('a.pdf', 'rb') as pdf:
response = HttpResponse(pdf.read(), contenttype='application/pdf')
response['Content-Disposition'] = 'inline;filename=some_file.pdf'
return response
pdf.closed
urls.py
from django.conf.urls import include, url
from pdfreader import views
urlpatterns = [
url(r'^$',views.index,name='index'),
url(r'^(?P<PDF_id>[0-9]+)/$' , views.pdf_view, name='detail' ),
]