我正在尝试使用 Django 上传图像并且没有错误。但是媒体文件夹内没有文件或目录。除了图像字段,所有其他字段都在表中更新。
模型.py
from django.db import models
# Create your models here.
class Player_Profile(models.Model):
name=models.CharField(max_length=50, null=True)
email=models.EmailField(max_length=50)
profile_picture=models.ImageField(upload_to='profile_picture/%y%m%d', blank=True, null=True)
age = models.BooleanField()
视图.py
from django.shortcuts import render_to_response
from django.template import RequestContext
from upload.models import Player_Profile
def home(request):
return render_to_response('upload/index.html',context_instance=RequestContext(request))
def submit(request):
if request.method == 'POST':
username=request.POST.get('username')
email=request.POST.get('email')
age=request.POST.get('age')
pic=request.FILES.get('myfile')
profile_obj=Player_Profile(profile_picture=pic,name=username, email=email,age=age).save()
return render_to_response('upload/welcome.html',context_instance=RequestContext(request))
索引.html
<form action="/upload/submit/" method="POST" encrypt="multipart/form-data">
{% csrf_token %}
User Name :<input type="text" name="username" id="usrname"/><br/>
Age :<input type="text" name="age" id="age"/><br/>
Email :<input type="email" name="email">
<input type="file" name="myfile" /><br/>
<input type="submit" name="submit" value="Upload" />
和内部设置
MEDIA_ROOT = '/home/mridul/Desktop/Django/interim/pic/uploadpic/media'
MEDIA_URL = '/media/'
并在 uploadpic 目录中手动创建媒体目录。