我猜想当用户选择或上传媒体(图像)时,ImageField(upload_to =“ pics”)参数必须在项目目录中创建一个名为“ pics”的文件夹。但是它没有用,甚至没有创建'pics'文件夹。图像名称仅存储在数据库中,但是当我获取图像时,找不到图像。请帮助
data=tableOne.objects.get(phone=phone1)
是否将从数据库中获取我的图像?
我的settings.py:
MEDIA_URL = '/media/'
MEDIA_ROOT = os.path.join(BASE_DIR,'media')
我的项目urls.py:
from django.conf.urls.static import static
urlpatterns = urlpatterns + static(settings.MEDIA_URL,document_root=settings.MEDIA_ROOT)
我的模型。py:
class tableOne(models.Model):
name = models.CharField(max_length=100)
email = models.CharField(max_length=100)
phone = models.CharField(max_length=100)
password = models.CharField(max_length=100)
propic = models.ImageField(upload_to="pics")
我的views.py:
def imageupload(request):
name=request.POST.get('name')
email=request.POST.get('email')
phone=request.POST.get('phone')
password=request.POST.get('password')
p=request.POST.get('picture')
data=tableOne(name=name,email=email,phone=phone,password=password,propic=p)
data.save()
return HttpResponse("Image inserted")
我的views.py我在哪里获取图片:
def login(request):
name1=request.POST['name1']
phone1=request.POST['phone1']
request.session['details']=phone1
data=tableOne.objects.get(phone=phone1)
log_name=data.name
if(name1==log_name):
return render(request,"nextpage.html",{ 'user' : data } )
我在其中上传我的详细信息的模板:
{% load static %}
<form method="POST" action="imageupload">
{% csrf_token %}
<input type="text" placeholder="name" name="name"><br>
<input type="text" placeholder="email" name="email"><br>
<input type="text" placeholder="phone" name="phone"><br>
<input type="text" placeholder="password" name="password"><br>
<input type="file" name="picture"><br>
<input type="submit">
</form>
<!-- loging in to fetch image -->
<form method="POST" action="login">
{% csrf_token %}
<input type="text" placeholder="name" name="name1"><br>
<input type="text" placeholder="phone" name="phone1"><br>
<input type="submit" value="login">
</form>
</body>
我要在其中发布图片的模板(nextpage.html)
<img src="{{ user.propic.url }}">
答案 0 :(得分:0)
尝试将其添加到主urls.py
,
...
from django.conf import settings
from django.conf.urls.static import static
...
if settings.DEBUG:
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)