Vitul Rana
Vitul Rana

Reputation: 31

Cannot Upload File in Django through HTML Form and Cant set value in form while Posting

Views.py:Unable to Set the Value of From in the database by user's email id and File Upload from this HTML form doesnt work but in admin panel it works properly

def compose_msg(request):
    if request.method == 'POST':
        form = MessageForm(request.POST, request.FILES)
        if form.is_valid():
            form.From = request.user.email //Cant Set the value by this in Databse//
            instance = Message(File= request.FILES["File"])
            instance.save()
        return HttpResponseRedirect('/Portal/compose/')
    else:
        form = MessageForm()
    context = {
        "form":form,
    }
    return render(request,"form.html",context)

Forms.py

class MessageForm(forms.ModelForm):
    class Meta:
        model = Message
        fields = [
            "subject",
            "To",
            "message",
            "File",
        ]

Models.py :Model used in the Program

class Message(models.Model):
    subject = models.CharField(max_length=120)
    To = models.EmailField(max_length=254)
    From = models.EmailField(max_length=254)
    message = models.TextField(blank=True,
                               null=True)
    timestamp = models.DateTimeField(auto_now=False, auto_now_add=True)
    File = models.FileField(blank=True, null=True)

    def __unicode__(self):
        return self.subject

Rendered HTML Form

<!DOCTYPE html>
    <head>
        <link rel="stylesheet" href='/static/css/default.css' />
        <link rel="stylesheet" href='/static/css/bootstrap.min.css' />
        <script src='/static/js/jquery.min.js'></script>
        <script src='/static/js/bootstrap.min.js'></script>
        <title>



        </title>
        </head>
    <body>
        <div class="container">

        <nav class="navbar container-fluid">
        <ul class="nav nav-pills nav-justified">
            <li><a href="/Portal/inbox/">Inbox</a></li>
            <li><a href="/Portal/compose/">Compose</a></li>
            <li><a href="/Portal/outbox/">Sent</a></li>
            <a class="Logout btn btn-primary btn" href="/Portal/logout/" role="button">Logout</a>
        </ul>
    </nav>

    <form role="form" class="form-horizontal" method="post">
        <input type='hidden' name='csrfmiddlewaretoken' value='53Dtir4AphDETyjVnvMhma9T4kav9cbC' />
        <div class="form-group"><label class="control-label" for="id_subject">Subject</label><input class="form-control" id="id_subject" maxlength="120" name="subject" placeholder="Subject" required="required" title="" type="text" /></div>
<div class="form-group"><label class="control-label" for="id_To">To</label><input class="form-control" id="id_To" maxlength="254" name="To" placeholder="To" required="required" title="" type="email" /></div>
<div class="form-group"><label class="control-label" for="id_message">Message</label><textarea class="form-control" cols="40" id="id_message" name="message" placeholder="Message" rows="10" title="">
</textarea></div>
<div class="form-group"><label class="control-label" for="id_File">File</label><div class="row bootstrap3-multi-input"><div class="col-xs-12"><input class="" id="id_File" name="File" title="" type="file" /></div></div></div>
        <div class="form-group"><button class="btn btn-primary" type="submit">Submit</button></div>
    </form>

        </div>
    </body>
</html>

Upvotes: 1

Views: 865

Answers (1)

Selcuk
Selcuk

Reputation: 59305

You should edit your form tag like the following as multipart/form-data encoding is required for file uploads:

<form role="form" class="form-horizontal" method="post" enctype="multipart/form-data">

Edit: To modify the From value, you can do the following:

if form.is_valid():
    instance = form.save()
    instance.From = request.user.email
    instance.save()

Upvotes: 3

Related Questions