Upload and view PDF’s Django admin

Asked

Viewed 781 times

2

I wanted to ask a question, I am trying to upload a PDF file, on Jango, and I wanted to know if you like I view this pdf, I have the following code:

py.models

class Edital(models.Model):
    # ...

class DataFile(models.Model):
    Arquivo = models.FileField()

    def chage_view(self):
        return self.Arquivo

class Meta:
    verbose_name_plural = 'Editais'

Numero = models.CharField(primary_key=True, max_length= 15) 
pro_reitoria = models.ForeignKey('Pro_reitoria', default="")
Arquivo = models.FileField(upload_to= "", default="")

def __str__(self):
    return self.Numero

py Settings.

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

in my admin gets like this the file

inserir a descrição da imagem aqui

When I click the loaded file appears Page not Found(404)

inserir a descrição da imagem aqui

How do I view the PDF? I appreciate anyone who can help me!

1 answer

2


I believe the only detail missing is that you enable Django to serve the static and media files, which he does not do by default. To do this, just do this on your urls.py of the project:

from django.conf.urls.static import static

# depois das suas definições de urls

if settings.DEBUG:
urlpatterns += static(
    settings.STATIC_URL, document_root=settings.STATIC_ROOT) + static(
    settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
  • Thank you very much friend, solved my problem!!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.