Hello…
the scnerio is this, I am adding eggs info in a form and render it into anotherform and data is transfering easily but i added two fields in egg form which is pdf field and image field when i uploding these two files it is uploaded successfully and shown in media folder but it is not updating the name of pdf file as well as image and i want to download this pdf file but it is not showing so i cannot see the file and download it.
my model.py file for egg is this
class Egg(models.Model):
eggType = models.CharField(max_length=50)
eggSize = models.IntegerField()
eggWeight = models.IntegerField()
eggPrice = models.IntegerField()
eggPdf = models.FileField(upload_to='media/documents/', null=True, blank=True)
eggImage = models.ImageField(upload_to='media/images/', null=True, blank=True)
class Meta:
constraints = [
models.UniqueConstraint(fields=['id'], name='unique_id')
]
def __str__(self):
return f"{self.eggType} - {self.id}"
my eggindex.html template where i edit the egg info
<div class="container" style="margin-top: 20px;">
<h2>Egg Details</h2>
<div class="card" style="padding-left: 30px;">
<form id="eggform" method="POST" class="post-form" action="/eggsAdd" enctype="multipart/form-data">
{% csrf_token %}
<div class="form-row">
<div class="form-group col-md-6">
<label class="col-sm-2 col-form-label">Egg Type:</label>
<div class="col-sm-4">
{{ form.eggType }}
</div>
</div>
<div class="form-group col-md-6">
<label class="col-sm-2 col-form-label">Egg Size:</label>
<div class="col-sm-4">
{{ form.eggSize }}
</div>
</div>
</div>
<div class="form-row">
<div class="form-group col-md-6">
<label class="col-sm-2 col-form-label">Egg Weight:</label>
<div class="col-sm-4">
{{ form.eggWeight }}
</div>
</div>
<div class="form-group col-md-6">
<label class="col-sm-2 col-form-label">Egg Price:</label>
<div class="col-sm-4">
{{ form.eggPrice }}
</div>
</div><br>
<div class="form-group col-md-6">
<input type="file" name="myfile"><br><br>
</div>
</div><br>
<div class="form-group col-md-6">
<input type="file" name="myimage" class="form-control-file">
<div class="button-group">
<button type="submit" class="btn btn-primary">Submit</button>
</div>
</form>
</div>
</div>
and my eggs.html template where i want to show my eggs info
<body>
{% block content %}
<table class="table table-striped table-bordered table-sm">
<thead class="thead-dark">
<tr>
<th>Egg Type</th>
<th>Egg Size</th>
<th>Egg Weight</th>
<th>Egg price</th>
<th>Egg PDF</th>
<th>Egg Image</th>
<th>Actions</th>
</tr>
</thead>
<tbody>
{% for egg in eggs %}
<tr>
<td>{{ egg.eggType }}</td>
<td>{{ egg.eggSize }}</td>
<td>{{ egg.eggWeight }}</td>
<td>{{ egg.eggPrice }}</td>
<td><a href="{{ egg.eggpdf.name }}">PDF</a></td>
{% if egg.eggImage %}
<td><img src="{% static egg.eggImage.name %}" alt="Egg Image"></td>
{% endif %}
<td>
<a href="/Edit/{{ egg.id }}"><span class="glyphicon glyphicon-pencil" >Edit</span></a>
<a href="/deleteEgg/{{ egg.id }}">Delete</a>
</td>
</tr>
{% endfor %}
</tbody>
</table>
<br>
<br>
<center><a href="/eggsAdd" class="btn btn-primary">Add New Record</a></center>
{% endblock %}
and here is my view for eggs
def eggsAdd(request):
if request.method == "POST":
form = eggCreate(request.POST, request.FILES)
if form.is_valid():
egg = form.save(commit=False) # don't save the form yet
egg_price = eggPrice
total_price += egg_price
# handle the pdf file upload
pdf_file = request.FILES.get('myfile', None)
if pdf_file:
fs = FileSystemStorage()
filename = fs.save(pdf_file.name, pdf_file)
pdf_file_path = fs.url(filename)
egg.eggPdf = pdf_file_path
# handle the image file upload
image_file = request.FILES.get('myimage', None)
if image_file:
fs = FileSystemStorage()
filename = fs.save(image_file.name, image_file)
image_file_path = fs.url(filename)
egg.eggImage = image_file_path
# save the form and redirect to the eggs info page
egg.save()
eggs = Egg.objects.all()
return redirect('/eggsInfo')
else:
form = eggCreate()
return render(request,'html/eggindex.html',{'form':form})
How can i show pdf file name and see the file and download it and also for image?
Note: Here in the view this code egg_price = eggPrice total_price += egg_price
is for adding the egg price and showing it on this template
<div class="container">
<h1></h1>
<div class="card">
<p>Total Price: {{ total_price }}</p>
<div class="form-row">
<div class="form-group col-md-6">
</div><br>
</div>
</div>
</div>
Here is my forms.py file
from django import forms
from .models import Egg, filesUplod
class eggCreate(forms.ModelForm):
class Meta:
model = Egg
fields = '__all__'