从表单上传文件

时间:2014-04-24 22:48:57

标签: python django django-models django-forms

我有ModelForm我用来上传文件而且它不想工作。我昨天有工作,但我不知道是做了什么让它突然停止。

这是我的ModelForm

class UserForm(forms.ModelForm):
description = forms.CharField(max_length=500, widget=forms.Textarea(attrs={'cols': 80, 'rows': 5}))
class Meta:
    model = UserProfile
    exclude = {'user', 'description'}

这里是Model

def get_image_path(instance, filename):
    return os.path.join('images', str(instance.id), filename)

def get_video_path(instance, filename):
    return os.path.join('videos', str(instance.id), filename)

def get_randfile_path(instance, filename):
    return os.path.join('randfile', str(instance.id), filename)

class UserProfile(models.Model):
    user = models.OneToOneField(User)
    description = models.CharField(max_length=500, blank=True, null=True)
    photo = models.FileField(upload_to=get_image_path, blank=True, null=True)
    video = models.FileField(upload_to=get_video_path, blank=True, null=True)
    rand_file = models.FileField(upload_to=get_randfile_path, blank=True, null=True)

    def photo_name(self):
        return os.path.basename(self.photo.name)

    def video_name(self):
        return os.path.basename(self.video.name)

这里是View

def detail(request, user_id):
    user = get_object_or_404(User, pk=user_id)
    userprofile = get_object_or_404(UserProfile, user=user)
    year = datetime.now().year
    userform = UserForm()
    delvid = Del_Video()
    if request.method == 'POST':
        userform = UserForm(request.POST, request.FILES, instance=user)
        delvid = Del_Video(request.POST)
        if userform.is_valid():
            userprofile.description = request.POST.get('description', userprofile.description)
            userprofile.photo = request.FILES.get('photo', userprofile.photo)
            userprofile.video = request.FILES.get('video', userprofile.video)
            userprofile.rand_file = request.FILES.get('rand_file', userprofile.rand_file)
            userprofile.save()
        else:
            messages.add_message(request, messages.INFO, 'Invalid Form')
            userform = UserForm()
        if delvid.is_valid():
            if request.POST['Delete']:
                userprofile.video.delete()
        else:
            delvid = Del_Video()
    context = {'user': user, 'year': year, 'userform': userform, 'userprofile': userprofile, 'delvid': delvid}
    return render(request, 'users/detail.html', context)

以下是模板中的表单:

<div class="row">
    <div class="col-md-4">
        <h4>Update User:</h4>
        {% if userform.errors %}
            <h6>{{ userform.errors }}</h6>
        {% endif %}
        {% if messages %}
            {% for message in messages %}
                <h5>{{ message }}</h5>
            {% endfor %}
        {% endif %}
        <form action="{% url 'users:detail' user.id %}" method="post" enctype="multipart/form-data" data-ajax="false">
            {% csrf_token %}
            {{ userform.as_p }}
            <input type="submit" value="Update">
        </form>
    </div>
</div>

0 个答案:

没有答案