I am facing this problem when I try to upload files it doesn’t work from the html page but from the admin panel it works.
my model is:
class contrat(models.Model):
contrtID = models.CharField(max_length=25,unique=True)
SHRPath = models.FileField(upload_to='contrats/')
post_date = models.DateTimeField(default=timezone.now)
author = models.ForeignKey(User,on_delete=models.CASCADE)
def __str__(self):
return self.contrtID
def delete(self, *args, **kwargs):
self.SHRPath.delete()
super().delete(*args, **kwargs)
def get_absolute_url(self):
return reverse('Scheduling')
class Meta:
ordering = ('-post_date',)
The form is:
class contrat_F(forms.ModelForm):
contrtID = forms.CharField(label='Code',max_length=25)
class Meta:
model= contrat
fields=('contrtID','SHRPath')
The Views.py is:
class Contratadd(LoginRequiredMixin, CreateView):
model = contrat
template_name = 'Home/Scheduling/Add_contract.html'
form_class= contrat_F
def form_valid(self, form):
form =contrat_F(request.POST, request.FILES)
form.instance.author = self.request.user
return super().form_valid(form)
the urls.py is:
path('Add_Contrat/', views.Contratadd.as_view(), name='Add_Contrat'),
the page html code is:
<form action="" method="post" enctype="multipart/form-data">
{% csrf_token %}
<div class="border p-2 mb-3 mt-3 border-secondary">
<div class="form-row">
<div class="form-group col-md-12 mb-0">
{{ form.contrtID|as_crispy_field }}
</div>
<div class="form-row">
<div class="form-group col-md-12 mb-0">
{{form.SHRPath}}
</div>
</div>
</div>
<input class="btn btn-success mb-4" type="submit" value="ADD Contrat">
</form>
BUT IT DOESN’T UPLOAD THE FILE! where is my mistake please?
the error is:
name ‘request’ is not defined