我正在学习在Django中通过表单上传图片,但出现错误
在/ panel / add-news /下的TypeError 预期的str,字节或os.PathLike对象,而不是InMemoryUploadedFile
没有得到我所缺少的东西
forms.html
<form action="{%url 'add_news' %}" method="post" class="form-horizontal form-bordered" enctype="multipart/form-data">
{% csrf_token %}
<div class="form-group">
<div class="col-md-6">
<input type="text" id="newstitle" name="newstitle" class="form-control" placeholder="News Title" >
</div>
<div class="col-md-6">
<select id="example-chosen" id="newscat" name="newscat" class="select-chosen" data-placeholder="Category..." style="width: 250px;">
<option></option><!-- Required for data-placeholder attribute to work with Chosen plugin -->
<option value="cat1">cat 1</option>
<option value="cat2">cat 2</option>
<option value="cat3">cat 3</option>
</select>
</div>
</div>
<div class="form-group">
<div class="col-md-12">
<textarea type="text" id="newstxtshort" name="newstxtshort" rows="5" class="form-control" placeholder="Short text"></textarea>
</div>
</div>
<div class="form-group">
<div class="col-md-12">
<textarea type="text" id="newstxt" name="newstxt" rows="5" class="form-control" placeholder="Body text"></textarea>
</div>
</div>
<div class="form-group">
<div class="col-md-12">
<input type="file" id="file" name="myfile" rows="5" class="form-control" placeholder="Body text">
</div>
</div>
<div class="form-group">
<div class="col-md-12">
<button type="submit" class="btn btn-sm btn-primary"><i class="fa fa-angle-right"></i> Submit</button>
</div>
</div>
</form>
settings.py中的路径:
MEDIA_URL = '/media/'
MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
main / urls.py中的网址
from django.contrib import admin
from django.conf.urls import include, url
from django.conf import settings
from django.conf.urls.static import static
urlpatterns = [
# path(r"admin/", admin.site.urls),
# path(r"", include('main.urls'))
url(r"admin/", admin.site.urls),
url(r"", include("main.urls")),
url(r"", include("news.urls")),
]
if settings.DEBUG:
urlpatterns += static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
Models.py:
class News(models.Model):
name = models.CharField(max_length=50)
headline = models.TextField() # short_text in place of title in course
body_text = models.TextField()
date = models.DateField(default=timezone.now)
pic = models.TextField()
writer = models.CharField(max_length=50)
catname = models.CharField(max_length=50, default="-")
catid = models.IntegerField(default=0)
show = models.IntegerField(default=0)
def __str__(self):
return self.name
我的处理表单数据的Views.py方法:
def add_news(request):
if request.method == 'POST':
newstitle = request.POST.get('newstitle')
category = request.POST.get('newscat')
shorttext = request.POST.get('newstxtshort')
bodytxt = request.POST.get('newstxt')
if newstitle == "" or shorttext == "" or bodytxt == "" or category == "":
error = "All fields required"
return render(request, 'back/error.html', {'error': error})
fs = FileSystemStorage()
myfile = request.FILES['myfile']
filename = fs.save(myfile, myfile)
media_url = fs.url(filename)
b = News(name=newstitle, headline=shorttext, body_text=bodytxt, pic=media_url, writer='vineet', catname=category,
catid=0, show=0)
b.save()
return redirect('news_list')
return render(request, 'back/news_add.html')
尽管我可以在Django错误页面中看到它,但能够找到我要上传的文件: enter image description here