Domanda

I am unable to upload my file upload program. I created a small project in django to upload and download files. This project was/is working very fine. Now i am trying this in Mezzanine for which i have created following models:

from django.db import models
from django.contrib.auth.models import User
from mezzanine.pages.models import Page
from time import time

def get_upload_file_name(instance, filename):
    return "galleries/%s_%s" %(str(time()).replace('.','_'), filename)


GENDER = (('----','Please Select ...'),('male','Male'), ('female','Female'))
class AllUsers(models.Model):
    FullName = models.CharField(max_length=300)
    DOB = models.DateField()
    Gender = models.CharField(max_length=7, choices = GENDER)
    HomeAddress = models.TextField()
    Contact = models.CharField(max_length=300)
    Email = models.EmailField()
    CV = models.FileField(upload_to = get_upload_file_name)

forms like :

class UserForm(forms.ModelForm):
    class Meta:
        model = AllUsers
        fields = ['FullName' ,'DOB' ,'Gender','HomeAddress','Contact','Email','CV','UserDepartment']

Html page for form

<form action="/users/" method="post" enctype="multipart/form-data">{%csrf_token%}
     <fieldset>
        {{id}}
    <legend>Users Profile</legend>
    {% fields_for form %}
    <div class="form-actions">
        {% block account_form_actions %}
        <input class="btn btn-primary btn-lg pull-right" type="submit" value="Submit">
        {% endblock %}
    </div>
    </fieldset>
</form>

Problem: I upload my file and press submit button, on submission it moves me to html form page and remove the file i uploaded. and ask me to upload file again.

È stato utile?

Soluzione

I am not deleting this question, because this may help!

Your forms do not upload files. No worries. See:

SYMPTOM You have FileField() and ImageField() fields in your form and the files are not uploaded when you submit this form

PROBABLE CAUSE You might be missing the request.FILES argument when intantiate your Form object.

SOLUTION Always make sure that you are passing request.FILES when intantiate your Form object:

form = MyForm(request.POST, request.FILES)

*More Details : * https://code.djangoproject.com/wiki/NewbieMistakes

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top