url.py settings.py with external storage with dropbox - django

I would like to use dropbox as external storage. I have been working on this for a week and I think identified the problem.
url.py has:
urlpatterns += static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
Settings.py contains :
STATIC_URL = "/static/"
STATIC_ROOT = os.path.join(BASE_DIR, "static")
MEDIA_URL = "/media/"
MEDIA_ROOT = os.path.join(BASE_DIR, "media")
DEFAULT_FILE_STORAGE = "storages.backends.dropbox.DropBoxStorage"
DROPBOX_OAUTH2_TOKEN = #mytoken
I obtain the error:
ApiError('d5b8d0d14e950566defb5ca736ebda9a', GetTemporaryLinkError('path', LookupError('not_found', None)))
because it can't find the static there:
<img src="{% static 'logo.png' %}" height = 300></img>
How do I configure STATIC_ROOT and STATIC_URL (and MEDIA_ROOT/MEDIA_URL) in order to link to dropbox url? I'm expecting something like :
STATIC_URL = 'https://%s/%s/' % (AWS_S3_CUSTOM_DOMAIN, AWS_LOCATION)
Thank you in advance.

if you have a model like:
class MyModel(models.Model)
photo = models.ImageField(upload_to="photos")
access the photo using MyModel.photo because it contains a URL that points to the photo in dropbox.

Related

Django not rendering static image files localted in MEDIA directory

im relatively new to Django. I have an app where I specify MEDIA_URL and MEDIA_ROOT in settings.py file in project root. My pared down project structure looks like this:
/
../files
../files/media
../files/media/logo.png
../solid/settings.py
../solid/urls.py
/web/templates (contains all my HTML)
inside settings I have
MEDIA_ROOT = BASE_DIR / 'files'
MEDIA_URL = '/media/'
DEFAULT_FILE_STORAGE = BASE_DIR /'files'
in my template web/template/index.html I'm trying to reference it
<img src="{{% MEDIA_ROOT %}}/images/ourlogo.png" class="" alt="Logo" height="40">
In solid/urls.py I have:
urlpatterns = [
path('admin/', admin.site.urls),
path('',web_views.index,name="index"),
######################################################
path('login/',login_page,name="login"),
path('logout/',log_out,name="logot"),
path('register/',register,name="register"),
#######################################################
path('upload/',web_views.upload,name="upload"),
path('list/',web_views.list_uploads,name="list"),
path('details/<int:oid>', web_views.view_download,name='view_download'),
path('download/<str:filename>', web_views.download_file,name='download_file'),
#######################################################
path('paypal/', include("paypal.standard.ipn.urls")),
path('payment/',web_views.upgrade,name="payment"),
path('paypal-cancel/', PaypalCancelView.as_view(), name='paypal-cancel'),
path('paypal-return/', PaypalReturnView.as_view(), name='paypal-return'),
#######################################################
]+static('s/',document_root=settings.MEDIA_ROOT)
the image is not rendering.
settings.py
STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR,'static')
STATICFILES_DIRS =[
os.path.join(BASE_DIR,'project_name/static')
]
MEDIA_URL ='/media/'
MEDIA_ROOT = os.path.join(BASE_DIR,'media')
project_name/urls.py
add this two lines with urlspatterns
urlpatterns = [
path('admin/', admin.site.urls),
] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT) + static(settings.STATIC_URL,document_root=settings.STATIC_ROOT)
if want to get the image from database
<img src="{{ img.photo_2.url }}" alt="" class="img-fluid">
here "img" is the dictionary key name. photo_2 is the database field name. and to show the image you have put the .url after that.
static image
<link href="{% static 'img/favicon.png' %}" rel="icon">

Media files in Django

I think I'm doing everything correctly but the images won't show on the website although they get added successfully in the media folder...
media/product_images
inside media is the placeholder image + product_images folder
Model Field
image = models.ImageField(upload_to='product_images/', default='placeholder.png')
Settings
STATIC_URL = '/static/'
STATICFILES_DIRS = [
BASE_DIR / 'static'
]
MEDIA_URL = '/media/'
MEDIA_ROOT = BASE_DIR / 'media'
Urls
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)

Is there a way to add my django-tenant schema name to MEDIA_ROOT?

I have modified my Geonode project, which is a GeoDjango project, to enable multi-tenancy using django-tenants. I am currently not able to view my thumbnails due to broken routing...
How do I correctly route my generated thumbnails like this: http://d3.demo.com(current_tenant_domain_url)/uploaded/d3(tenant)/thumbs/document-8a72dc8c-0151-11eb-a488-1062e5032d68-thumb.png
The thubnail url that is currently generated is as follows: http://localhost:8000/uploaded/thumbs/document-fcdea3a4-015c-11eb-a488-1062e5032d68-thumb.png?v=c1855f6a
urls.py
urlpatterns += staticfiles_urlpatterns()
urlpatterns += static(settings.LOCAL_MEDIA_URL,
document_root=settings.MEDIA_ROOT)
My current settings.py
MEDIA_ROOT = os.getenv('MEDIA_ROOT', os.path.join(PROJECT_ROOT, MEDIAFILES_LOCATION))
MEDIA_URL = os.getenv('MEDIA_URL', '%s/%s/%s/' % (FORCE_SCRIPT_NAME, MEDIAFILES_LOCATION, MULTITENANT_RELATIVE_MEDIA_ROOT))
Any help will be appreciated
settings.py
MIDDLEWARE = [
'django_tenants.middleware.main.TenantMainMiddleware'
]
DATABASE_ROUTERS = (
'django_tenants.routers.TenantSyncRouter',
)
STATIC_URL = '/static/'
STATIC_ROOT = 'staticfiles'
STATICFILES_DIRS = (
os.path.join(BASE_DIR, "static"),
)
MEDIA_URL = '/media/'
DEFAULT_FILE_STORAGE = 'django_tenants.files.storages.TenantFileSystemStorage'
MEDIA_ROOT = os.path.join(BASE_DIR, 'public', 'media')
urls.py
urlpatterns = [...] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)

i can't use static files from my Django app

I went to this route http://127.0.0.1:8000/static/corefiles/chatbot-data.xlsx but i get Page not found (404) error...
I added those to the URLs:
+ static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
File directory is /rootproject/core/static/corefiles/testexel.xlsx
My settings.py :
STATIC_URL = '/static/'
STATICFILES_DIRS = (
normpath(join(BASE_DIR, 'static')),
normpath(join(BASE_DIR, 'upload')), )
STATIC_ROOT = normpath(join(BASE_DIR, 'assets'))
Static files are to be used in your project, if you need to view a document at the specified url, you need to specify media url and media root in the settings:
MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
MEDIA_URL = '/media/'
Also add those to the URLs:
+ static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)

Why can't I find my static folder in my project

I'm working on a Django project and I can't see my static folder, I'm also having a problem displaying images.
And when I inspect the image div in the src it's written unknown, Here's how I display the image from the admin
``<img class="rounded-circle account-profile" src="{{ user.Profile.profile_photo.url }}" alt="profile">``
User is the current user, the profile and then the name of the column.
My folder structure is route-folder( project-folder, media-folder and app-folder )
My static settings
```STATIC_URL = '/static/'
SITE_ROOT = os.path.dirname(os.path.realpath(__file__))
STATICFILES_DIRS = [
os.path.join(SITE_ROOT, "static/"),
]```
The Url:
if settings.DEBUG:
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
I think you are trying to retrieve a media image not a static one. I recomend using Whitenoise package to serve static files.
Follow instruction in this tutorial: http://whitenoise.evans.io/en/stable/django.html
Check you have all this setup:
# Settings
STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR, 'staticfiles')
MEDIA_URL = '/media/'
MEDIA_ROOT = os.path.join(BASE_DIR, "media")
STATICFILES_DIRS = (os.path.join(BASE_DIR, 'static'),)
In the templates, you should remember to add the {% load static %} to grant access to the actual files.