我正在尝试使用Django上传图像,并且没有错误。但是媒体文件夹中没有文件或目录。除了图像字段之外,所有其他字段都在table中更新。
Models.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()
views.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))
的index.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目录中手动创建媒体目录。
答案 0 :(得分:1)
不是:
<form action="/upload/submit/" method="POST" encrypt="multipart/form-data">
它的
<form action="/upload/submit/" method="POST" enctype="multipart/form-data">
即。 enctype
不是encrypt
另外,您应该使用Form
或ModelForm
来执行此操作,make your life much easier。