0

i am trying to upload file but unable to do so, my function in view,

def user_in(request):
if not request.user.is_authenticated:
return render(request, 'accounts/logout.html')
else:
if request.method == 'POST':
form_new = Fileupload(request.POST, request.FILES )
if form_new.is_valid():
return redirect('in')
else:
form_new = Fileupload()
return render(request, 'accounts/in.html', {'form_new': form_new})

my form,

class Fileupload(forms.Form):
BrowseFile=forms.FileField()
class Meta:
model=User

and my template,

<form action="." method="POST" enctype="multipart/form-data" >
<h3>Welcome to DropBox<br><br></h3>
{% csrf_token %}
{{form_new.as_p}}
<p><input type="submit" value="Save" ></p>
<br>
<a href="{% url 'logout' %}">Logout</a>
{%else%}
<p>You must login first</p>
<a href="{% url 'login' %}">Logout</a>
{% endif %}
<br>
</form>

after pressing submit button i do not see any doc in media location. Am i doing something wrong or anything missing?

Thanks in advance.

1
  • check the docs Commented Oct 4, 2017 at 4:48

1 Answer 1

2

First of all, You have to use forms.ModelForm instead of forms.Form if you want to use model=.... in class Meta:.

So I'll change like this...

class Fileupload(forms.ModelForm):
    class Meta:
        model=User # I guess BrowseFile field may be FileField()

If your model User is like this, you don't have to make BrowseFile field in your Fileupload form.

class User(...):
    BrowseFile = models.FileField()

So let's go further..

def user_in(request):
    if not request.user.is_authenticated:
        return render(request, 'accounts/logout.html')
    else:
        if request.method == 'POST':
            form_new = Fileupload(request.POST, request.FILES )
            if form_new.is_valid():
                return redirect('in')
        else:
            form_new = Fileupload()
        return render(request, 'accounts/in.html', {'form_new': form_new})

In your views, there is NOTHING you're saving what you've got in POST.

so let's change it ...

def user_in(request):
    if not request.user.is_authenticated:
        return render(request, 'accounts/logout.html')
    else:
        if request.method == 'POST':
            form_new = Fileupload(request.POST, request.FILES )
            if form_new.is_valid():
                form.save() # if you want it, you have to use forms.ModelForm 
                return redirect('in')
        else:
            form_new = Fileupload()
        return render(request, 'accounts/in.html', {'form_new': form_new})

Now you can see uploaded files in your somewhere upload folder.

Note: you HAVE TO setup upload file location(MEDIA_ROOT) and MEDIA_URL in your settings.py !

Sign up to request clarification or add additional context in comments.

2 Comments

regarding your sugeestion: class Fileupload(forms.ModelForm): class Meta: model=User # I guess BrowseFile field may be FileField() If your model User is like this, you don't have to make BrowseFile field in your Fileupload form. class User(...): BrowseFile = models.FileField() i used django 'user' inbuit model
If you're using inbuilt model, BrowseFile may not be exist. If you want to use that field, you can extend your User model by 2 solutions. First is making your own User model which inherits AbstractBaseUser, Second is making UserInfo model connected with User model One-To-One relationship.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.