我有一个Django应用程序,正在其中上传一些json文件。尝试访问这些文件时出现404(未找到)错误。
文件将加载到OpenLayers层中。如果我在项目静态文件中使用json,则效果很好。
文件已正确上传到项目文件夹中的“ uploaded_files”目录。但是,当尝试通过object.file.url访问html页面中的这些文件时,我总是会出错。
如果我将MEDIA_URL更改为“ / uploaded_files /”,则会出错,因为它希望找到core /或admin / url。
示例:http://127.0.0.1:8000/uploaded_files/file.json
settings.py文件:
STATIC_URL = '/static/'
STATICFILES_DIRS = [
os.path.join(BASE_DIR, 'static'),
]
MEDIA_URL = '/core/uploaded_files/' # core is the application in django project. I have admin and core applications.
MEDIA_ROOT = os.path.join(BASE_DIR, 'uploaded_files')
urls.py文件:
from django.conf import settings
from django.conf.urls.static import static
from django.contrib.staticfiles.urls import staticfiles_urlpatterns
urlpatterns += staticfiles_urlpatterns()
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
models.py文件:
def get_upload_file_name(instance, filename):
return "%s_%s" % (str(time()).replace('.', '_'), filename)
class Estado(models.Model):
estado = models.CharField(max_length=2)
def __str__(self):
return self.estado
class TipoFile(models.Model):
tipo = models.CharField(max_length=50)
def __str__(self):
return self.tipo
class FileEstadoMunicipio(models.Model):
estado = models.ForeignKey(Estado, on_delete=models.CASCADE, null=False)
municipio = models.CharField(max_length=100, null=False)
tipo = models.ForeignKey(TipoFile, on_delete=models.CASCADE, null=False)
dat_criacao = models.TimeField(default=datetime.datetime.now(), null=False)
file = models.FileField(upload_to=get_upload_file_name, null=False)
html文件:
{% load static %}
var layer = new ol.layer.Vector({
source: new ol.source.Vector({
format: new ol.format.GeoJSON({dataProjection: 'EPSG:32722'}),
url: '{{ alagamento.file.url }}'
# url: '{% static "jsonfiles/cadastro_eixos_logradouros.json" %}' works fine
}),
style: style,
opacity: 0.3
});
在html页面中出现错误:
获取http://127.0.0.1:8000/core/uploaded_files/1562551137_926604_SuscetibilidadeMovimentoDeMassa.json 404(未找到)
请告知您是否发现我做错了。