Django:上传图片无法找到上传的图片

时间:2017-04-11 17:10:33

标签: django image upload find

我已经尝试过搜索stackoverflow,但找不到任何东西。我的问题是,我似乎无法找到我上传的图片所在的文件夹。好像其他一切都有效?

在这里你可以看到我的代码:

Settings.py

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

Views.py

@login_required(login_url="/login/")
def model_form_upload(request):
    if request.method == 'POST':
    form = DocumentForm(request.POST, request.FILES,instance=request.user)
        if form.is_valid():
            form.save()
            print("Form is valid")
            return redirect('/questions/profile')
    else:
        form = DocumentForm(instance=request.user)
     return render(request, 'questions/model_form_upload.html', {
       'form': form
})

Urls.py

url(r'^upload/$', views.model_form_upload, name='upload')

Models.py

import os

def get_image_path(instance, filename):
     return os.path.join('users/', str(instance.id), filename)

class Document(models.Model):
    user = models.ForeignKey(User, on_delete=models.CASCADE)
    profile_image = models.FileField(upload_to=get_image_path, 
    blank=True, null=True)

Forms.py

from questions.models import Document

class DocumentForm(forms.ModelForm):
    class Meta:
        model = Document
        fields = ('profile_image',)

model_form_upload.html

 {% extends 'questions/base.html' %}

 {% block content %}
 <form method="post" enctype="multipart/form-data">
 {% csrf_token %}
 {{ form.as_p }}
 <button type="submit">Upload</button>
 </form>

 <p><a href="{% url 'questions:profile' %}">Return to profile</a></p>
 {% endblock %}

0 个答案:

没有答案