-1

我不喜欢在 Django 中工作。

4

1 回答 1

2

模型.py

from django.db import models

class Upload(models.Model):
    user = models.ForeignKey(User)
    name = models.CharField(max_length=100)
    file = models.FileField(upload_to="files")
    date_upload = models.DateTimeField(auto_now_add=True)

表格.py

from django import forms
from app_name.models import Upload

class UploadForm(forms.ModelForm):
    class Meta:
        model = Upload

视图.py

def upload_file(request):
    if request.method == 'POST':
        form = UploadForm(request.POST, request.FILES)
        if form.is_valid():
            form.save()
            return HttpResponseRedirect('/success/url/')
    else:
        form = UploadFileForm()
    return render_to_response('upload.html', {'form': form})

上传.html

<form enctype="multipart/form-data" action="/upload/" name="test" method="post">
    {% csrf_token %}
    {{form.as_p}}
    <input type="submit" value="Save File">
</form>
于 2013-03-31T02:28:40.310 回答