success-pdf-download

This commit is contained in:
sofordar 2025-01-30 17:27:48 +06:00
parent 039890c725
commit d1f41dcd38
19 changed files with 28 additions and 13 deletions

View File

@ -1,10 +1,11 @@
from django.conf import settings
from django.contrib import admin
from django.urls import path, include, re_path
from django.urls import path, include
import resulation.views
from django.conf.urls.static import static
from django.urls import path
from django.urls import path, re_path
from . import views
from django.views.static import serve
urlpatterns = [
path('', resulation.views.Home, name='home'),
@ -12,6 +13,9 @@ urlpatterns = [
path('viewresulation/', resulation.views.Viewresulation, name='viewresulation'),
path('deed/', resulation.views.Contract, name='deed'),
path('mou/', resulation.views.Mou, name='mou'),
re_path('downloads/<int:attendance_id>/<str:filename>', resulation.views.file_download, name='file-download'),
path('rcm/downloads/<int:attendance_id>/<str:filename>', resulation.views.file_download, name='file-download'),
path('rcm/', resulation.views.file_download, name='download_file'),
]
if settings.DEBUG:

View File

@ -1,9 +1,12 @@
from django.shortcuts import render, HttpResponse, get_object_or_404
from django.shortcuts import render, HttpResponse, get_object_or_404, Http404, redirect
from .models import resulation
from datetime import datetime
import os, uuid
import misdghs.settings
from django.http import FileResponse
from django.http import FileResponse, StreamingHttpResponse
from urllib.parse import unquote
from misdghs import settings
from django.core.files.storage import default_storage
# Create your views here.
def Home(request):
@ -27,15 +30,17 @@ def Savepdf(request):
# Create a new seminar instance with text fields
if attendance_file:
new_filename = f"attendance_{uuid.uuid4()}.pdf"
path1 = os.path.join('media', 'attendance_files', new_filename)
new_filename1 = f"attendance_{uuid.uuid4()}.pdf"
path1 = os.path.join('media', 'attendance_files', new_filename1)
with open(path1, 'wb') as destination:
for chunk in attendance_file.chunks():
destination.write(chunk)
if res_con_mou_file:
new_filename = f"res_con_{uuid.uuid4()}.pdf"
path2 = os.path.join('media', 'resolution_files', new_filename)
new_filename2 = f"res_con_{uuid.uuid4()}.pdf"
path2 = os.path.join('media', 'resolution_files', new_filename2)
print(path2)
with open(path2, 'wb') as destination:
for chunk in res_con_mou_file.chunks():
@ -48,8 +53,8 @@ def Savepdf(request):
m_number=m_number,
m_date=m_date,
m_venue=m_venue,
attendance_file=path1,
res_con_mou_file=path2,
attendance_file=new_filename1,
res_con_mou_file=new_filename2,
)
seminar.save()
print(path1)
@ -66,13 +71,18 @@ def Savepdf(request):
# Render the form template
return render(request, 'resulation/resulation.html')
def Viewresulation(request, pk):
def Viewresulation(request):
# Get all records from database
pdf_records = resulation.objects.all().order_by('-m_date') # Latest first
return render(request, response, 'resulation/viewresulation.html', {'pdf_records': pdf_records})
return render(request, 'resulation/viewresulation.html', {'pdf_records': pdf_records})
def file_download(request, attendance_id, filename):
# Construct the full path to the file
file_path = os.path.join(settings.MEDIA_ROOT, 'attendance_files', filename)
# Use sendfile to stream the file directly
return FileResponse(open(file_path, 'rb'), content_type='application/pdf')
def Contract(request):

View File

@ -1,6 +1,7 @@
{% extends "base.html" %}
{% block content %}
{% load static %}
<div class="table-container">
<div class="table-header">
<h2>PDF Records</h2>
@ -34,7 +35,7 @@
<td>{{ record.m_date|date:"d M Y" }}</td>
<td>{{ record.m_venue }}</td>
<td>
<a href="{{ record.attendance_file.url }}">Download</a>
<a href="{% url 'file-download' attendance_id=record.eid filename=record.attendance_file %}">Download</a>
</td>
<td>
{% if record.res_con_mou_file %}