我想当用户选择或上传媒体(图像(时,ImageField(upload_to="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)
我 models.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>
我想发布图像的模板(下一页.html(:
<img src="{{ user.propic.url }}">
尝试将其添加到您的主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)