Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.3k views
in Technique[技术] by (71.8m points)

django: FileField model - 404

This is my first Django project and I have one problem. file1 is saved to media folderfiles however when I try to download the file I'm getting 404. Any help is appreciated!

127.0.0.1:8000/about/files/2021/01/22/pyqt_tutorial_EB2ZapN.pdf

models.py

class Links(models.Model):
    file1 = models.FileField(upload_to = 'files/%Y/%m/%d/')
    is_published = models.BooleanField(default = True)
    publish_date = models.DateTimeField(default = datetime.now, blank = True)

html

        {% for link in links %}
        <div class="links1">
            <h3><a href = "{{ link.file1 }}">Download Link</a></h3>
            <p>{{ link.publish_date }}</p>
        </div>
        {% endfor %}

urls.py>

urlpatterns = [
    path('admin/', admin.site.urls),
    path('about/', about, name = 'about')
] + static(settings.MEDIA_URL, document_root = settings.MEDIA_ROOT)

settings.py>

# Media Folder Settings
MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
MEDIA_URL = '/media/'

Update: from the admin panel if I click on the link I can see it. Maybe In the {{ link.file1 }} url something need to be changed?

question from:https://stackoverflow.com/questions/65837925/django-filefield-model-404

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

The href in your template should be {{ link.file1.url }} to properly construct the path to the file.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...