Django Media网址返回404 NOT FOUND

时间:2017-02-13 09:23:33

标签: python django media-url

我有一个django项目。 为了设置我的媒体网址,我按照了django doc Django doc - Managing static files

  

例如,如果您的MEDIA_URL定义为/ media /,您可以通过将以下代码段添加到您的urls.py来执行此操作:

     

来自django.conf导入设置   来自django.conf.urls.static import static

     

urlpatterns = [      #...其余的URLconf都在这里......   ] + static(settings.MEDIA_URL,document_root = settings.MEDIA_ROOT)

我的 settings.py

# ....
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
BASE_URL = 'http://127.0.0.1:8000'
# ...
MEDIA_URL_REL = '/media/'
MEDIA_URL = BASE_URL + MEDIA_URL_REL
MEDIA_ROOT = os.path.join(BASE_DIR, 'media/')

我的 urls.py

from django.conf import settings
from django.conf.urls import url, include
from django.conf.urls.static import static

urlpatterns = [
 # ...
] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)

我上传的文件位于媒体根目录中,但是当我访问媒体网址[ http://127.0.0.1:8000/media/proof/img.pdf ]时,它会返回 HTTP 404 NOT FOUND 。< / p>

1 个答案:

答案 0 :(得分:8)

请不要使用硬编码制作任何网址。这不仅仅是不好的做法。

import myUI class MainUIClass(QtGui.QMainWindow, myUI.Ui_MainWindow): def __init__(self, parent=None): super(MainUIClass, self).__init__(parent) self.setupUi(self) self.startbutton.clicked.connect(self.do_work) self.workerThread = WorkerThread() self.connect(self.workerThread, SIGNAL("myThreading()"), self.myThreading, Qt.DirectConnection) def do_work(self): self.userInputString = self.nameField.Text() self.workerThread.start() def myThreading(self): if userInputString is not None: #Do something class WorkerThread(QThread): def __init__(self, parent=None): super(WorkerThread, self).__init__(parent) def run(self): self.emit(SIGNAL("myThreading()")) if __name__ == '__main__': a = QtGui.QApplication(sys.argv) app = MainUIClass() app.show() a.exec_()

你不能这样写。

您的解决方案,

settings.py

BASE_URL = 'http://127.0.0.1:8000'

urls.py

MEDIA_URL = '/media/'

希望这有效。