图片未在Django中上传且未创建媒体文件夹

时间:2020-03-11 01:55:06

标签: python django image upload

这是我的代码 我尝试将图像上传到媒体文件夹中。所有其他字段均已上传,但图像未移动到媒体文件夹,并且当我进行迁移时,请勿创建媒体文件夹

url.py

 from django.contrib import admin
from django.urls import path,include
from django.contrib.staticfiles.urls import staticfiles_urlpatterns
from django.conf.urls.static import static # for media
from django.conf import settings # for media file install pillow package
urlpatterns = [
    path('admin/', admin.site.urls),
    path('user/', include('AdminDashboard.urls')),

]
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
urlpatterns += staticfiles_urlpatterns()

Model.py

from django.db import models
from django.core    import validators   # this library is used for form validation
from django.core.validators import  ValidationError

# Create your models here.
class movie(models.Model):
     def min_length_check(self):
        if len(self)> 10:
            raise validators.ValidationError("value must be greater than 10")

     title = models.CharField(max_length=255)
     description = models.TextField(max_length=300)
     created_at = models.DateTimeField(auto_now_add=True) # this will add at one time 
     updated_at = models.DateTimeField(auto_now=True)    # created and updated every
     image = models.ImageField(upload_to='images',default='default.png')

View.py

from django.shortcuts import render, redirect
from django.http import HttpResponse ,HttpResponseRedirect # this include for model for  http request
from .forms import movieForm
#from .models import movie

# Create your views here.
def index(request):
    return render(request,"index.html")

def userRegister(request):
    return render(request,"userReg.html")
def adminRegister(request):
    return render(request,"admin/adminReg.html")

def movieRegister(request, id=0):

    if request.method == 'POST':
        form = movieForm(request.POST, request.FILES)
        if form.is_valid():


            form.save()
            return HttpResponse("image have been save")
        else:
            return HttpResponse(print(form.errors))

    else:

        form = movieForm()
        return render(request,'admin/movieadd.html',{'form': form}) 

    return render(request, "admin/movieadd.html",{'form': form})

def dashboard(request):
    form = movieForm
    return render(request, "admin/dashboard.html",{'form': form})


def viewAlluser(request):
    return render(request,"admin/viewalluser.html")

def deletuser(request):
    return  

Setting.py

# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/2.2/howto/static-files/
STATIC_URL = '/static/'
STATICFILES_DIRS= [os.path.join(BASE_DIR,'assets'),] # this variable have been created for  adding static resourcess

MEDIA_ROOT = os.path.join(BASE_DIR,'media')
MEDIA_URL = '/media/'

所有字段均已上传到数据库,但未创建媒体文件夹或未在其中上传图像。 我已经完成了所有迁移。

0 个答案:

没有答案