Admin FileField current url incorrect

2024/10/2 6:36:05

In the Django admin, wherever I have a FileField, there is a "currently" box on the edit page, with a hyperlink to the current file. However, this link is appended to the current page url, and therefore results in a 404 as there is no such page as, for example: http://127.0.0.1:8000/admin/Tank/asset/17/media/datasheet/13/09/05/copyright.html/
For reference, the correct url of the file is: http://127.0.0.1:8000/media/datasheet/13/09/05/copyright.html

Is there any way to fix this problem in the default admin layout? It affects every FileField in my database, and seems to me like a bug. Am I just using it wrong?

Answer

settings.py

add the lines:

import os
BASE_DIR = os.path.realpath(os.path.dirname(__file__))

replace the lines:

MEDIA_ROOT = ''
MEDIA_URL = ''

with

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

this should setup your project to render your media content from the folder /your project directory/media/

urls.py

also add the line:

import settings

add the following line in your url patterns:

url(r'^media/(?P<path>.*)$', 'django.views.static.serve',{'document_root': settings.MEDIA_ROOT, 'show_indexes': False}),

models.py

inside your model add the following line:

File = models.FileField('File',upload_to='./')

define the method in the model

def fileLink(self):if self.File:return '<a href="' + str(self.File.url) + '">' + 'NameOfFileGoesHere' + '</a>'else:return '<a href="''"></a>'
fileLink.allow_tags = True
fileLink.short_description = "File Link"

admin.py

use the field fileLink as a read only field, you can also add it to your list_display

eg

class FileAdmin(admin.ModelAdmin):list_display = ['fileLink']readonly_fields = ['fileLink']
https://en.xdnf.cn/q/70881.html

Related Q&A

Difference between generator expression and generator function

Is there any difference — performance or otherwise — between generator expressions and generator functions?In [1]: def f():...: yield from range(4)...:In [2]: def g():...: return (i for i in…

Django performance testing suite thatll report on metrics (db queries etc.)

I have a complex Django web application that has many person-years of work put into it. It might need optimisation sometime. There are several common operation/flows that I could script with (say) djan…

dev_appserver.py Opens a Text File, Does Not Deploy

It works fine on my other computer, but after setting up Google App Engine and creating the main.py and app.yaml files, I run dev_appserver.py app.yaml in Windows command prompt and instead of deployin…

How to pass a list from a view to template in django

I am trying pass to list from a view to template in Django.In my file wiew.py I define the view named hour # This Python file uses the following encoding: utf-8from django.shortcuts import render from …

Probing/sampling/interpolating VTK data using python TVTK or MayaVi

I would like to visualise a VTK data file (OpenFOAM output) using python. The plot I would like to make is a 1-d line plot of a quantity between two endpoints. To do so, the unstructured data should be…

Make Sphinx generate RST class documentation from pydoc

Im currently migrating all existing (incomplete) documentation to Sphinx.The problem is that the documentation uses Python docstrings (the module is written in C, but it probably does not matter) and t…

inspect.getfile () vs inspect.getsourcefile()

I was just going through the inspect module docs.What exactly is the difference between:inspect.getfile()andinspect.getsourcefile()I get exactly the same file path (of the module) for both.

Get a row of data in pandas as a dict

To get a row of data in pandas by index I can do:df.loc[100].tolist()Is there a way to get that row of data as a dict, other than doing:dict(zip(df.columns.tolist(),df.loc[100], tolist() ))

Find duplicate records in large text file

Im on a linux machine (Redhat) and I have an 11GB text file. Each line in the text file contains data for a single record and the first n characters of the line contains a unique identifier for the rec…

Select the first item from a drop down by index is not working. Unbound method select_by_index

I am trying to click the first item from a drop down. I want to use its index value because the value could be different each time. I only need to select the 1st item in the drop down for this particul…