Code sequence
self.driver.save_screenshot('./logs/{}.png'.format(step.id))
log = Logs(step=step, attachment='./logs/{}.png'.format(step.id), aditional_data={},
flow_instance=self.flowinstance)
log.save()
The picture is not saved at all in the Docker web container and cannot be accessed through the log.
error given
Logs model
class Logs(models.Model):
step = models.ForeignKey(Step, on_delete=models.CASCADE)
attachment = models.ImageField(upload_to='logs/', blank=True)
aditional_data = models.JSONField()
flow_instance = models.ForeignKey(FlowInstance, on_delete=models.CASCADE)
settings.py
BASE_DIR = os.path.dirname(os.path.dirname(__file__))
MEDIA_ROOT = os.path.join(BASE_DIR, 'media').replace('\\', '/')
MEDIA_URL = '/media/'
urls.py i added
+ static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
In docker-compose I have selenium-hub, browser instance, worker / beat. All this works correctly, the only problem is when the picture should be taken and saved, which does not happen.
Related
Today I tried to have images in my project and the idea is simple - create news with an image, title, and description.
I wonder why when I set up my media files
So I make my news in this view:
class NewsCreate(views.CreateView):
template_name = 'web/create_news.html'
model = News
fields = ('title', 'image', 'description')
success_url = reverse_lazy('home')
Here is the model:
class News(models.Model):
TITLE_MAX_LENGTH = 30
title = models.CharField(
max_length=TITLE_MAX_LENGTH
)
image = models.ImageField(
upload_to='news/',
blank=True
)
description = models.TextField()
Here is the set-up in settings.py:
MEDIA_ROOT = BASE_DIR / 'mediafiles'
MEDIA_URL = '/media/'
Here is the urls.py file:
from django.contrib import admin
from django.urls import path, include
urlpatterns = [
path('admin/', admin.site.urls),
path('', include('University_Faculty.web.urls')),
]
if settings.DEBUG:
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
I've noticed that when I try to go to none existing URL this happens : wrong ulr page showing media as a correct one
This is the result in my media folder after 10+ POST requests it shows in the database that it is actually creating the news, but the images won't go anywhere: no files media folder
You need to correct
MEDIA_ROOT = BASE_DIR / 'media'
Hope this will work for you.
add this to settings.py
MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
I have a django-admin panel and a Windows Virtual Private Server.
What i want to do is upload files from django admin panel to a directory like this :
C:\site\media
and i dont want to upload files to django app folder .
This is my settings.py file :
STATIC_ROOT = os.path.join(BASE_DIR,'static')
STATIC_URL = '/static/'
MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
MEDIA_URL = '/media/'
if not os.path.exists(MEDIA_ROOT):
os.makedirs(MEDIA_ROOT)
and This is my model :
class Pictures(models.Model):
product = models.ForeignKey('Products', models.DO_NOTHING)
picurl = models.ImageField()
def __unicode__(self):
return self.title
class Meta:
managed = False
db_table = 'pictures'
verbose_name_plural = "ProductPicturess"
def __str__(self):
return '%s------- (%s)' % (self.product.title,self.picurl)
How should i change it's values ?
Thank you .
After Searching alot and triying different values finally i found the solutin .
Simply I changed MEDIA_ROOT to this :
MEDIA_ROOT = os.path.join(BASE_DIR, '../../../realwamp/wamp64/www/myproject/pictures/')
And volla , My Problem is solved .
With three pairs of dots like ../../../ I back to the C:\ directory and then go to my www folder .
I want to upload images in the django admin interface. During development everything works fine but when I put the files on my server it doesn't work.
I have two different paths on my Server. One where I put all my source files and one where I put all the static files.
Path for source files: /htdocs/files/project/
Path for static files: /htdocs/html/project/
If I upload an image, then it is saved in /htdocs/files/project/media/. But I want to save it in /htdocs/html/project/. How can I change the path?
Here are my settings:
STATIC_URL = '/static/'
MEDIA_URL = '/media/'
MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
STATICFILES_DIRS = (
os.path.join(BASE_DIR, 'static'),
'/var/www/ssd1257/htdocs/html/'
)
And here is my model:
class News(models.Model):
title = models.CharField(max_length=200, null=False)
date = models.DateField(null=False, default=datetime.now)
text = models.TextField(null=False, blank=True)
image = models.ImageField(upload_to="./news/")
The uploaded files are saved normally in the following path
MEDIA_URL + the path specified in “upload_to” attribute in the model class
So in your case,
MEDIA_ROOT = os.path.join(BASE_DIR, 'media') = “/htdocs/files/project/media”
Django will create the path if it doesn’t exits
But I didn’t get the dot in-frontront of ‘upload_to’ to path(“./news/“)
So if you want to change the path where uploaded files are stored, simply change the MEDIA_ROOT
Note, please provide the absolute full path
I guess it will be
MEDIA_ROOT = '/var/www/ssd1257/htdocs/html/project'
Also, its better to rename the uploaded files before saving to avoid file_name conflicts
def get_news_image_path(instance, filename):
path_first_component = ‘news/‘
ext = filename.split('.')[-1]
timestamp = millis = int(round(time.time() * 1000))
file_name = ‘news_’ + str(instance.id) + str('_logo_image_') + timestamp + str('.') + ext
full_path = path_first_component + file_name
return full_path
class News(models.Model):
title = models.CharField(max_length=200, null=False)
date = models.DateField(null=False, default=datetime.now)
text = models.TextField(null=False, blank=True)
image = models.ImageField(upload_to=get_news_image_path)
Now the uploaded files will be saved in
'/var/www/ssd1257/htdocs/html/project/news’
You are Done
In addition, also set appropriate MEDIA_URL
Ex: MEDIA_URL = “media
So when generated URL for the upload images will be
MEDIA_URL + upload_to path
Also, configure the web server to serve these URLs from appropriate locations
from django.core.files.storage import FileSystemStorage
upload_storage = FileSystemStorage(location=UPLOAD_ROOT, base_url='/') #upload root set to your project directory
class News(models.Model):
title = models.CharField(max_length=200, null=False)
date = models.DateField(null=False, default=datetime.now)
text = models.TextField(null=False, blank=True)
image = models.ImageField(upload_to='/', storage=upload_storage)
Self-inflicted by this setting:
MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
Change that to:
MEDIA_ROOT = '/htdocs/html/project/'
I have a project that has 3 apps, one named stores with store models, products with product models, and api which is the rest framework app that serves the Json results to clients. I set the media root in settings.py as MEDIA_ROOT = '/photos/' and the upload works for both product and store models. The main problem here is that for some reason the rest framework returns a url that references the api app instead of the products or stores apps for the media root url. here are my models
class Product(models.Model):
def get_image_path(instance, filename):
return '/Products/' + filename
picture = models.ImageField(width_field=None, max_length=100, blank =True, null =True)
store:
class Store(models.Model):
def __str__(self):
return self.name
def get_image_path(instance, filename):
return os.path.join('productphotos', 'stores', filename)
picture = models.ImageField(width_field=None, max_length=100, blank =True, null =True)
How do i set the mediaroot to the project directory instead so that all apps in the project reference it as mediaroot instead of themselves?
The upload works and upoads the pictures to the instructed directories in the root of the project (where manage.py is found), but the rest framework thinks it should get the media from the api app.. what's the proper way of doing this? here are screenshots:
the path uploaded to
the path returned in json
The MEDIA_URL setting is the URL path in the browser. The MEDIA_ROOT setting is the root directory on your server, and should be an absolute path.
MEDIA_URL = '/pictures/'
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
MEDIA_ROOT = os.path.join(BASE_DIR, 'uploaded_pictures')
Also, if you want Product and Store pictures to go into different sub directories, for example pictures/products/ and pictures/store/, you'd need to set the upload_to argument on the model field. For example
picture = models.ImageField(upload_to='products/', ... )
Edit: To serve static and media files during development, add this at the end of the urls.py
if settings.DEBUG:
from django.contrib.staticfiles.urls import staticfiles_urlpatterns
from django.conf.urls.static import static
urlpatterns += staticfiles_urlpatterns()
urlpatterns += static(
settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
I am having some problems with serving user uploaded files from my Django application:
from models.py:
class Picture (models.Model):
title = models.CharField(max_length=48)
date_added = models.DateTimeField(auto_now=True)
content = models.ImageField(upload_to='pictures')
From the Django admin the files get uploaded to the user_res/pictures/ folder.
from the project's settings.py:
MEDIA_ROOT = 'user_res'
MEDIA_URL = '/user_res/'
STATIC_ROOT = ''
STATIC_URL = '/static/'
Every time I try to reference a static resource (namely css or js files), everything works fine using URLs such as
http://localhost:8000/static/<subfolder>/main.css.
However, I cannot access user uploaded files (which get created by the admin interface in the user_res/pictures folder with a relative URL such as
user_res/pictures/test.jpg
the URL is dynamically created with this line of code from a Django Picture model callable:
return '<img src="{}"/>'.format(self.content.url)
I have no dedicated url-s for either static or media files in the url.py file.
Does anybody have any idea as to how to make Django serve the media files? I understand that for live environments I will need to configure an http server to serve that particular directory, but for now I want to maintain a lightweight development suite.
Thank you.
Edit your urls.py file as shown below.
from django.conf import settings
from django.conf.urls.static import static
urlpatterns = patterns('',
# ... the rest of your URLconf goes here ...
) + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
edit your projects settings.py to look like:
#Rest of the settings
MEDIA_URL = '/media/'
MEDIA_ROOT = 'media'
STATIC_ROOT = ''
STATIC_URL = '/static/'
Please read the official Django documentation about serving files uploaded by a user carefully. Link to docs: https://docs.djangoproject.com/en/1.5/howto/static-files/#serving-files-uploaded-by-a-user
I think the url attribute returns a relative URL ( Django's FileField documentation ), so you should have:
return '<img src="{}"/>'.format(MEDIA_URL + self.content.url)
Relative URLs won't work, as a user visiting "http://localhost/books/" would be requesting "http://localhost/books/user_res/pictures/test.jpg".