好吧,我试图在Django中制作一个简单的应用程序,并在其中从管理面板上传文件。用户会看到一个简单的搜索页面,在其中输入一个ID,因此需要生成相关文件。该文件应为pdf格式。因此,如果可以的话,我可以在浏览器中查看pdf文件,并使用一个下载按钮下载该文件。该代码托管在Github上,链接为https://github.com/tahseen09/delhipatho
答案 0 :(得分:0)
将STATIC_ROOT = '/static/'
添加到您的settings.py
urls.py
urlpatterns = [
path('',views.index, name='index'),
path('admin/', admin.site.urls),
]
if settings.DEBUG:
urlpatterns += static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
views.py
from report.models import query
def index(request):
if request.method == "POST":
form = Form(request.POST)
if form.is_valid():
query = query.objects.filter(id__exact =form.cleaned_data['id'])
return render(request, 'ans.html', {'query':query})
else:
form = Form()
return render(request,"index.html", {'form':form})
ans.html
{% load static %}
<!DOCTYPE HTML>
<html>
<head>
<title>Delhi Patho</title>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no" />
<link rel="stylesheet" href="{% static 'assets/css/main.css' %}" />
</head>
<body class="is-preload">
<p><a href="{{query.repo.url}}">Download</p>
<!-- Scripts -->
<script src="{% static 'assets/js/main.js' %}"></script>
</body>
</html>