I am trying to create an online music player. I was trying to play a song in django. But it was not able to play since it was not able to detect the audio file. I have set up the media in django as I read on the internet but it's not working.
Here is the screenshot of my project directory:
detect and music are two apps. I am currently working with music app.
Settings.py(included this at last of file).
STATIC_URL = '/static/'
STATICFILES_DIRS = [
os.path.join(BASE_DIR, "static")
]
MEDIA_ROOT = os.path.join(BASE_DIR,"media")
MEDIA_URL = '/media/'
urls.py:
from django.conf.urls import *
from django.contrib import admin
from detect import views
from music import views
from django.conf import settings
from django.conf.urls.static import static
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^music/$',views.play,name = 'play')
]
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
urlpatterns += static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
views.py:
from django.shortcuts import render
from django.http import HttpResponse
def play(request):
return render(request,'music/song.html',{})
song.html:
<html>
<audio id="Player" autoplay>
<source src="{{MEDIA_URL}}/audio/test.mp3"/>
</audio>
</html>
Pass MEDIA_URL like to render like this
render(request,'music/song.html',{'MEDIA_URL': settings.MEDIA_URL}) and make sure to include from django.conf import settings in your views.py.
Related
Hope you are all having a very good Friday!
I am running a django project on nginx, and my project is each time going into this url - https://sitename.com/internal-nginx-static-location_uat/project_name/static/images/success.svg
I didn't configure this internal-nginx-static, and If I load the same project on another website, it works fine, is it something I can handle in code, or I have make changes in server conf.
Here is my URL.py Files
from django.conf import settings
from django.contrib import admin
from django.conf.urls.static import static
from django.urls import path, include
urlpatterns = [
path('', include('portal.urls')),
path('admin/', admin.site.urls),
] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
and settings file
STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(DATA_DIR, 'static')
MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
MEDIA_URL = '/media/'
I'd like to allow users to upload profile pictures and then to display them.
Here's my model.py:
from django.db.models import CharField, ImageField, Model
class Eater(Model):
name = CharField(max_length = 30)
image = ImageField(upload_to = 'images/eaters/')
def __str__(self):
return str(self.name)
Here's my urls.py:
from django.conf import settings # settings is an object, not a module, so you can't import from it. :(
from django.conf.urls.static import static
from django.urls import path
from .views import EaterView, IndexView, post, sudo
app_name = 'socialfeedia'
urlpatterns = [
path('', IndexView.as_view(), name = 'index')
] + static(settings.MEDIA_URL, document_root = settings.MEDIA_ROOT) # Static returns a list of path, not a path itself.
This is at the end of my settings.py:
MEDIA_URL = '/media/'
MEDIA_ROOT = BASE_DIR.joinpath('media/')
Here's a line out of my index.html:
<img class="post-profile-picture" src="{{ post.eater.image.url }}" alt="?"/>
I successfully uploaded a file into this field - it got stored at:
mysite/media/images/eaters/test_picture_64.jpg
The image loads successfully if I visit it here:
http://127.0.0.1:8000/socialfeedia/media/images/eaters/test_picture_64.jpg
However, the img that shows up in my generated file is this:
<img class="post-profile-picture" src="/media/images/eaters/test_picture_64.jpg" alt="?">
This file doesn't resolve - I just see a ? (the alt) instead. Should I be using something else to get the correct path to the file instead? Something like this maybe?
{% media post.eater.name %}
(Except no such thing as media exists as far as I can tell...)
It should be including socialfeedia/ (the app name) at the start of the url but it isn't... it doesn't seem very much like what I've seen of Django so far to expect me to manually hardcode that in...
my guess is to move
+ static(settings.MEDIA_URL, document_root = settings.MEDIA_ROOT) # Static returns a list of path, not a path itself.
from urls.py file of your app socialfeedia to the global urls.py file under the project package and don't forget to set the namespace socialfeedia in the corresponding urls:
from django.conf import settings
from django.urls import path, include
from django.contrib import admin
[..]
urlpatterns = [
[..]
path('socialfeedia/', include('socialfeedia.urls', namespace='socialfeedia')), # HERE
path('admin/', admin.site.urls), # Admin Area
]
# DEBUG = True
if settings.DEBUG:
from django.conf.urls.static import static
urlpatterns += static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
let me know if this helps you.
Adding {% get_media_prefix %} at the beginning of src worked for me but I'm pretty sure it's not the right solution.
<img class="post-profile-picture" src="{% get_media_prefix %}{{ post.eater.image.url }}" alt="?"/>
Examined the documentation of django and this post Django MEDIA_URL and MEDIA_ROOT but, I'm still having issues, at first It was a SQlight issue so, I updated to latest Django from Django 2 now I'm getting:
AttributeError: 'Settings' object has no attribute 'MEDIA_Root'
Settings.py
STATIC_URL = '/static/'
MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
MEDIA_URL = '/media/'
urls.py
from django.contrib import admin
from django.urls import path
from django.conf import settings
from django.conf.urls.static import static
urlpatterns = patterns('',
path('admin/', admin.site.urls),
] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT),
Also tried this:
urlpatterns = [
path('admin/', admin.site.urls),
] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT),
Removed the comma at the end:
Bad Code:
+ static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT),
Good Code:
+ static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
I guess you actually imported wrong settings. Re-check your actually code. Instead of importing this (upper-case):
from django.conf import Settings
you should import this (lower-case)
from django.conf import settings
Also, as was pointed in comments, this is an error (or you incorrectly pasted the code here):
'MEDIA_Root'STATIC_URL = '/static/'
I have a Django server setup on development as well as the production server. The development server loads the static files but the production server gives 404 on loading (although it renders the URL).
I have already used the collectstatic method to accumulate my static files.
settings.py:
STATIC_ROOT = os.path.join(BASE_DIR, 'static/')
STATIC_URL = '/static/'
urls.py (main_project)
from django.contrib import admin
from django.urls import path, include
from django.conf import settings # new
from django.conf.urls.static import static # new
urlpatterns = [
path('', include('stock_management.urls', namespace='stock_management')),
path('auth/', include('django.contrib.auth.urls')),
path('admin/', admin.site.urls),
]
# if settings.DEBUG: # new
# urlpatterns += static(settings.STATIC_URL,
# document_root=settings.STATIC_ROOT)
# urlpatterns += static(settings.MEDIA_URL,
# document_root=settings.MEDIA_ROOT)
urls.py (App: stock_management) :
from django.urls import path, include
from .views import *
from django.conf import settings
app_name = 'stock_management'
urlpatterns = [
# Stock:
path('', stock_list, name='homepage'),
path('stock/', stock_list, name='stock_list'),
path('stock/add', stock_create_view, name='add_stock'),
path('stock/<pk>/edit', stock_edit, name='stock_edit'),
# Item:
path('items/', item_list, name='item_list'),
path('item/<pk>/edit', item_edit, name='item_edit'),
path('item/<pk>/delete', item_delete, name='item_delete'),
# API
path('api/items', item_list_API, name='item_list_API'),
# Gallery:
path('items/gallery', item_gallery, name='item_gallery'),
]
# if settings.DEBUG:
# # test mode
# from django.conf.urls.static import static
# urlpatterns += static(settings.STATIC_URL,
# document_root=settings.STATIC_ROOT)
# urlpatterns += static(settings.MEDIA_URL,
# document_root=settings.MEDIA_ROOT)
I want my static files to load in the server also.
When you set DEBUG=False in settings.py Django stops serving static files. You need to configure a web server like Nginx for static files.
Here a helpful tutorial: https://www.digitalocean.com/community/tutorials/how-to-deploy-a-local-django-app-to-a-vps
first post so be gentle.
I am trying to display a static image in django. I can upload the image to my media directory but when I try to display it I get a nothing. If I copy the url to the browser I get ...
Page not found (404)
Request Method: GET
Request URL: http://127.0.0.1:8000/media/Will_and_Matt.jpg
Raised by: django.views.static.serve
detail.py
...
<img src="{{ student.photo.url }}" class="img-responsive">
...
model.py
class Student(models.Model):
photo = models.FileField( blank=True, null=True)
project urls.py
from django.conf.urls import url, include
from django.contrib import admin
from django.conf import settings
from django.conf.urls.static import static
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^opencmis/', include('opencmis.urls')),
]
if settings.DEBUG:
urlpatterns += static(settings.STATIC_URL, document_root=settings.STATIC_URL)
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_URL)
settings.py
STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR, 'static')
MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
MEDIA_URL = '/media/'
My update form works as it created the media folder and places images in it.
The URL pattern seems to match /media as it doesn't give an error there it just doesn't display the image.
Any ideas?
document_root=settings.STATIC_URL should be document_root=settings.STATIC_ROOT and similar for MEDIA_ROOT. You want the filesystem path in there, not the URL.
– dhke