找不到 Django 媒体文件 404
Django media file not found 404
我有一个 Django 博客应用程序,在其中发布博客时,还可以附加和提交文件。
这里是 views.py 代码:
def user_own_blog(request):
if request.method == 'POST' and request.FILES['blog_document']:
title_b = request.POST.get('blog_title')
content_b = request.POST.get('blog_content')
file1 = request.FILES['blog_document']
fs = FileSystemStorage()
document_name = fs.save(file1.name, file1)
uploaded_document_url = fs.url(document_name)
b = Blog(title=title_b, content=content_b, blog_document=uploaded_document_url)
b.save()
return render(request, 'mysite/portfolio.html')
else:
return render(request, 'mysite/blog.html')
这里是 MEDIA_ROOT 和 MEDIA_URL 路径名:
STATIC_URL = '/static/'
MEDIA_URL = '/media/'
MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
下面是 mysite 应用程序中 urls.py 的代码:
urlpatterns=[ .....
......
] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
这是项目结构,其中我需要媒体文件夹直接出现在 Assignment1 项目下
文件上传成功后,在/api.
中显示如下
但是正在创建两个媒体路径:/media/media 如下所示:我找不到我可能创建的重复字段。
点击文件 link:出现 404 未找到错误。我认为 MEDIA_ROOT 文件名不正确。
HTML 代码 blog.html:
{% extends 'mysite/base.html' %}
{% load static %}
{% block content %}
<div class="container">
<div class="row">
<div class="col-sm-6 mx-auto" style="margin-top: 70px">
<form action="{% url 'user_own_blog' %}" method="POST" enctype="multipart/form-data">
{% csrf_token %}
<div class="form-group row">
<label for="example-email-input" class="col-2 col-form-label">Title</label>
<div class="col-10">
<input name = "blog_title" class="form-control" type="text">
</div>
</div>
<div class="form-group row">
<label for="example-email-input" class="col-2 col-form-label">Content</label>
<div class="col-10">
<textarea name = "blog_content" class="form-control" rows = "5" cols = "50" type="text"> </textarea>
</div>
</div>
<div class="form-group row">
<label for="example-email-input" class="col-2 col-form-label">Upload File</label>
<div class="col-10">
<input name = "blog_document" class="form-control" type="file">
</div>
</div>
<div class="pull-right">
<button type="submit" class="btn btn-primary float-right">Post</button>
</div>
</form>
</div>
</div>
</div>
{% endblock %}
如果要在根目录定位上传的文件位置:
MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
MEDIA_URL = '/media/'
其他:
MEDIA_URL = '/media/'
MEDIA_ROOT = 'C:/Users/xyz/Assignment1/mysite/media/' #if windows pay attention to the slashes
您的 url 也遵循此模式:
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
blog_document= models.FileField()
views.py
def user_own_blog(request):
if request.method == 'POST' and request.FILES:
form = BlogForm(request.POST,
request.FILES)
blog = Blog()
if form.is_valid():
blog.title_b = form.cleaned_Data['title']
blog.content_b = form.cleaned_Data['content']
blog.file = form.cleaned_Data['blog_document']
blog.save()
return HttpRequestRedirect(reverse('portfolio'))
else:
form = BlogForm()
return render(request,
'mysite/blog.html',
{'form': form})
要检索上传文件 url 有默认方法 .url
。您只需将查询传递给模板 (Modelname.objects.filter(title__iexact='something')
)
{{ query.file.url}}
注意:您需要在forms.py中创建一个表单(如果您不必创建的话)。并使用 cleaned_data 方法从表单中检索值。研究这个。
如果您已经将其添加到 settings.py
文件中
MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
MEDIA_URL = '/media/'
你仍然收到 404 错误,这可能是因为你必须在主 urls.py
中添加 media_url 以及
from django.conf import settings
from django.conf.urls.static import static
urlpatterns = [
# ... the rest of your URLconf goes here ...
] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
这是您可以添加的方式。如果您想阅读更多详细信息,请阅读 this article
我有一个 Django 博客应用程序,在其中发布博客时,还可以附加和提交文件。
这里是 views.py 代码:
def user_own_blog(request):
if request.method == 'POST' and request.FILES['blog_document']:
title_b = request.POST.get('blog_title')
content_b = request.POST.get('blog_content')
file1 = request.FILES['blog_document']
fs = FileSystemStorage()
document_name = fs.save(file1.name, file1)
uploaded_document_url = fs.url(document_name)
b = Blog(title=title_b, content=content_b, blog_document=uploaded_document_url)
b.save()
return render(request, 'mysite/portfolio.html')
else:
return render(request, 'mysite/blog.html')
这里是 MEDIA_ROOT 和 MEDIA_URL 路径名:
STATIC_URL = '/static/'
MEDIA_URL = '/media/'
MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
下面是 mysite 应用程序中 urls.py 的代码:
urlpatterns=[ .....
......
] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
这是项目结构,其中我需要媒体文件夹直接出现在 Assignment1 项目下
文件上传成功后,在/api.
中显示如下但是正在创建两个媒体路径:/media/media 如下所示:我找不到我可能创建的重复字段。
点击文件 link:出现 404 未找到错误。我认为 MEDIA_ROOT 文件名不正确。
HTML 代码 blog.html:
{% extends 'mysite/base.html' %}
{% load static %}
{% block content %}
<div class="container">
<div class="row">
<div class="col-sm-6 mx-auto" style="margin-top: 70px">
<form action="{% url 'user_own_blog' %}" method="POST" enctype="multipart/form-data">
{% csrf_token %}
<div class="form-group row">
<label for="example-email-input" class="col-2 col-form-label">Title</label>
<div class="col-10">
<input name = "blog_title" class="form-control" type="text">
</div>
</div>
<div class="form-group row">
<label for="example-email-input" class="col-2 col-form-label">Content</label>
<div class="col-10">
<textarea name = "blog_content" class="form-control" rows = "5" cols = "50" type="text"> </textarea>
</div>
</div>
<div class="form-group row">
<label for="example-email-input" class="col-2 col-form-label">Upload File</label>
<div class="col-10">
<input name = "blog_document" class="form-control" type="file">
</div>
</div>
<div class="pull-right">
<button type="submit" class="btn btn-primary float-right">Post</button>
</div>
</form>
</div>
</div>
</div>
{% endblock %}
如果要在根目录定位上传的文件位置:
MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
MEDIA_URL = '/media/'
其他:
MEDIA_URL = '/media/'
MEDIA_ROOT = 'C:/Users/xyz/Assignment1/mysite/media/' #if windows pay attention to the slashes
您的 url 也遵循此模式:
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
blog_document= models.FileField()
views.py
def user_own_blog(request):
if request.method == 'POST' and request.FILES:
form = BlogForm(request.POST,
request.FILES)
blog = Blog()
if form.is_valid():
blog.title_b = form.cleaned_Data['title']
blog.content_b = form.cleaned_Data['content']
blog.file = form.cleaned_Data['blog_document']
blog.save()
return HttpRequestRedirect(reverse('portfolio'))
else:
form = BlogForm()
return render(request,
'mysite/blog.html',
{'form': form})
要检索上传文件 url 有默认方法 .url
。您只需将查询传递给模板 (Modelname.objects.filter(title__iexact='something')
)
{{ query.file.url}}
注意:您需要在forms.py中创建一个表单(如果您不必创建的话)。并使用 cleaned_data 方法从表单中检索值。研究这个。
如果您已经将其添加到 settings.py
文件中
MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
MEDIA_URL = '/media/'
你仍然收到 404 错误,这可能是因为你必须在主 urls.py
中添加 media_url 以及
from django.conf import settings
from django.conf.urls.static import static
urlpatterns = [
# ... the rest of your URLconf goes here ...
] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
这是您可以添加的方式。如果您想阅读更多详细信息,请阅读 this article