N
N
NyxDeveloper2021-01-13 16:34:35
Django
NyxDeveloper, 2021-01-13 16:34:35

The file from the form does not get into request.FILES?

I am doing adding files to a django site. Everything would be fine, but the object model with the FileField also has a ForeignKey to bind the file to a specific object. I tried to follow the instructions in the documentation, but I either turned the wrong way somewhere, or I don’t understand something. I would be glad to any example, maybe someone has already encountered a similar problem.

models.py

#   документ
class Doc(models.Model):
    file = models.FileField('Файл', upload_to='docs')
    idProj = models.ForeignKey(Project, verbose_name='Проект', on_delete=models.SET_NULL, null=True)
    description = models.CharField('Примечание', max_length=2000, blank=True)

    class Meta:
        verbose_name = 'Документ'
        verbose_name_plural = 'Документы'

    def __str__(self):
        return self.file.name


forms.py
class AddDocForm(forms.ModelForm):
    class Meta:
        model = Doc
        fields = [
            'file',
            'description'
        ]

        widgets = {
            'file': forms.FileInput(attrs={
                'class': 'form-file-input mt-2 mb-2'
            })
        }

    def __init__(self, *args, **kwargs):
        super(AddDocForm, self).__init__(*args, **kwargs)
        self.fields['description'].widget = forms.Textarea(attrs={'class': 'form-control'})


views.py
def AddDocView(request, objId):
    if request.method == 'POST':
        form = AddDocForm(request.POST, request.FILES)
        if form.is_valid():
            form.save()    #да, я знаю, что поле idProj здесь не обрабатывается, интерпретатор вообще до сюда не доходит
            return redirect('/base/edit_project/' + str(objId))
    if request.method == 'GET':
        form = AddDocForm
        return render(request, 'addDoc.html', {
            'form': form,
            'objId': objId
        })

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Dr. Bacon, 2021-01-13
@NyxDeveloper

ok google, "django file not uploading", response enctype="multipart/form-data"

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question