I am trying to build a django app and host it on webfaction.
My model looks like this:
class Post(models.Model):
title = models.CharField(max_length=512)
image = models.ImageField(upload_to='blogImages/')
body = models.TextField()
visible = models.BooleanField()
date_created = models.DateTimeField(auto_now_add=True)
date_updated = models.DateTimeField(auto_now=True)
tags = models.ManyToManyField('Tag', null=True, blank=True)
def __unicode__(self):
return self.title
The settings look like this:
MEDIA_ROOT = '/home/myself/webapps/dev_static/media/'
MEDIA_URL = 'http://dev.example.com/static/media/'
STATIC_ROOT = '/home/myself/webapps/dev_static/'
STATIC_URL = 'http://dev.example.com/static/'
When I go to my server and try to upload an image, I get this error:
SuspiciousOperation at /admin/blog/post/add/
Attempted access to '/home/myself/wrong/path/appname/blogImages/Portal2-Logo.jpg' denied.
I'm trying to figure out where the wrong path could come from. Where else should I look for the wrong path?
i have had the same problem, solved with
image = models.ImageField(upload_to='/blogImages/')
instead of (upload_to='blogImages/')
The error is returning the old static media path. It started working correctly using the the correct path after I restarted Apache.
Related
class Movie(models.Model):
image = models.ImageField(default='default.jpg')
title = models.CharField(max_length=255)
release_year = models.IntegerField()
number_in_stock = models.IntegerField()
daily_rate = models.FloatField()
genre = models.CharField(max_length=255)
date_created = models.DateTimeField(default=timezone.now)
def __str__(self):
return self.title
Admin interface
The above code is my models class and i have given a default value to the
ImageField .
After saving the data my image field is still empty.
Whats the prooblem ?
Have you added this in your setting.py :
STATIC_URL = '/static/'
STATICFILES_DIRS=[
os.path.join(BASE_DIR,'static')
]
Also, you will have to add the image in the static folder in your project for it work properly.
Adding on to this answer, the Media URL/Root and Static URL/Root collectively help Django process images for your application.
When you add the default image in models.py, make sure to put '/static/image.jpg' and once you're rending in the template load static with {% load static %}.
I have a model called Photo and looks like this...
class Photo(models.Model):
uuid = models.UUIDField(primary_key=True, default=uuid.uuid4)
created_at = models.DateTimeField(auto_now_add=True)
photo = models.FileField()
title = models.CharField(max_length=30)
kind = models.CharField(max_length=30)
size = models.CharField(max_length=30)
strength = models.CharField(max_length=30)
combinations = models.CharField(max_length=30)
favors = models.CharField(max_length=30)
availability = models.BooleanField(default=True)
iscarousel = models.BooleanField(default=False)
def __str__(self):
return "%s %s %s %s %s %s %s %s %s %s" % (self.created_at, self.photo, self.title,
self.kind, self.size, self.strength, self.combinations, self.favors, self.availability,
self.iscarousel)
I am can save to my aws s3 bucket but I do not have a url path to it, or so I believe this is the case. How can I save my model where once I upload my photo the url gets saved in my model as well. So I am thinking that I will need a url field and I know there is a models.UrlField or something like that. I am just curious though how I tell django to extract that url and add it to some field so I can later access it. If I don't have it I don't know how I am suppose to retrieve it to later show it on my web application
IF you use ImageField instead of FileField you will get more related to images. However both have a url attribute you can access from a django template or when querying for a specific image.
In your model it would look something like this:
image = models.ImageField(upload_to='users/%Y/%m/%d/', blank=True)
In a Django Template you would access it like this:
<img src="{{ img_obj.image.url}}" alt="connect" style="max-height:300px">
Here is a reference to more info on how to set that up
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
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?