Can someone help me. I have FileField() for upload files like .doc or .pdf from admin panel. I can upload file like admin but I can't download this file like user. The download link does not work?
models.py
class Book(models.Model):
"""Model representing a book (but not a specific copy of a book)."""
title = models.CharField(max_length=200)
author = models.ForeignKey('Author', on_delete=models.SET_NULL, null=True)
summary = models.TextField(max_length=1000, help_text="Введите краткое содержание книги")
genre = models.ManyToManyField(Genre, help_text="Выберите жанр книги")
language = models.ForeignKey('Language', on_delete=models.SET_NULL, null=True)
specifications = models.FileField(upload_to='router_specifications')
book_detail.html
<p><strong>Download:</strong>Download</p>
Did you define the MEDIA_URL and MEDIA_ROOT settings variables and add view to serve the media files?
https://docs.djangoproject.com/en/2.1/howto/static-files/#serving-files-uploaded-by-a-user-during-development
Related
I need to upload photos to specific directories.
I have below model code.
#reversion.register()
class Student(BaseModel):
uuid = models.UUIDField(default=uuid.uuid4, verbose_name=_("Unique Identifier"), unique=True)
user = models.OneToOneField(User, on_delete=models.PROTECT, db_index=True, verbose_name=_("User"))
photo = models.ImageField(upload_to="student/", null=True, blank=True, verbose_name=_("Photo"))
std_status = models.IntegerField(choices=STD_STATUS, default=1, verbose_name=_("Stu Sta"))
std_no = models.AutoField(primary_key=True, verbose_name=_("Stu Num"))
name = models.CharField(max_length=100, db_index=True, verbose_name=_("Name"))
surname = models.CharField(max_length=100, db_index=True, verbose_name=_("Surname"))
For the photo line. It uploads photos to student/ directory. But i need to have subdirectories with User name and dates. Like "student/jenifer_argon/2019223/2022/07/01"
I can set "student/" directory but username and std_no and date should be dynamically set. student/user/std_no/date user and std_no should come from above model on creation runtime.
How can i do this?
It is like; everything will be on same form. But std_no and user will be set first than they will be used to set upload diretory definition and image will be uploaded to that directory.
A function can be passed to upload_to that is passed the instance and filename, you can construct your path using these
from datetime import date
def student_photo_path(instance, filename):
today = date.today()
return 'student/{0}/{1}/{2}/{3}/{4}/{5}'.format(
instance.user.username,
instance.std_no,
today.year,
today.month,
today.day,
filename
)
class Student(BaseModel):
...
photo = models.ImageField(upload_to=student_photo_path, null=True, blank=True, verbose_name=_("Photo"))
...
I am working on Django2.2.6 apps and one of the model filed is FroalaField where users can upload images from their machine local storage, the default Django models.FileField has a defualt upload_to attribute like this models.FileField(upload_to=get_blog_filepath, null=True, blank=True), how can I configure FroalaField to upload images to S3 by passing the url, I have looked at the docs and didn't find something useful in my case.
from django.db import models
from froala_editor.fields import FroalaField
class BlogPost(models.Model):
short_title = models.CharField(max_length=200, null=True, blank=False, db_column='db_short_title')
title = models.CharField(max_length=200)
blurb = FroalaField(max_length=300) # how I can do upload_to S3
report_file = models.FileField(upload_to=get_blog_filepath, null=True, blank=True)
I have an Artwork and ArtworkPhoto models in Django.
ArtworkPhoto has an FK to the Artwork and stores some additional data on the photo.
class ArtworkPhoto(models.Model):
title = models.CharField(max_length=200, verbose_name='Omschrijving', blank=True)
photo = FileBrowseField("Image", max_length=200, directory="artwork/",
extensions=[".jpg", ".png"], blank=False, null=False)
artwork = models.ForeignKey(Artwork, null=False, verbose_name='kunstwerk')
Every Artwork has X photos, all working fine.
Now I want to provide authenticated visitors with an upload form to add an artwork and photo's.
How can I save a posted file to the ArtworkPhotoModel? It's a Grapelli FileBrowser > Filebrowsefield ...
Thanks, i'm a bit stuck here. ...
model.py
class Category(models.Model):
name = models.CharField(max_length =255, unique = True)
slug = models.SlugField(max_length= 255, unique = True)
description = models.TextField()
is_published = models.BooleanField(default=True)
def __unicode__(self):
return "%s" %self.name
I have model like above, so on admin page I want field description has tool ckeditor , but I don't know how to use it, so anybody can help me?
You need to install plugin like this https://github.com/django-ckeditor/django-ckeditor. Follow the installation instructions on github
Then in your models.py add:
from ckeditor.fields import RichTextField
and change this line:
description = models.TextField()
to this:
description = RichTextField()
Finally migrate you app
Im trying to make a shopping portal with a recommendation system
Not able to display images in the templates...
The HTML pages when opened without running the server opens them though
tried setting the MEDIA_URL and MEDIA_ROOT
settings as given:
MEDIA_ROOT = "/home/jose/Estore/estore/images/"
MEDIA_URL = '/images/'
The Model I defined is as given:
#Model for Books
from django.db import models
class Book(models.Model):
title = models.CharField(max_length=100)
section = models.CharField(max_length=50)
authors = models.CharField(max_length=150)
subject = models.CharField(max_length=50)
publisher = models.CharField(max_length=50)
publication_date = models.DateField(blank=True, null=True)
price = models.DecimalField(max_digits=6,decimal_places=2)
photo = models.ImageField(upload_to='product_photo/books/',blank=True)
description = models.TextField()
rating = models.DecimalField(max_digits=2,decimal_places=1)
def __str__(self):
return self.title
Have you set your urlconf?
http://docs.djangoproject.com/en/dev/howto/static-files/#serving-other-directories
here is how to do that. You need to set this in order for static serving to work.
Did this help?