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="?"/>
Related
My root urls.py
from django.contrib import admin
from django.conf import settings
from django.conf.urls.static import static
from django.urls import path,include
urlpatterns = [
path('admin/', admin.site.urls),
path('', include('pages.urls')), # new
]+static(settings.STATIC_URL,document_root=settings.STATIC_ROOT)
urlpatterns += static(settings.MEDIA_URL,document_root=settings.MEDIA_ROOT)
My pages app urls.py
from django.contrib import admin
from django.urls import path
from pages import views
urlpatterns = [
path('', views.home_page, name='home_page'),
path('<tab>', views.home,name='home'),
]
With this, I am able to access
127.0.0.1:8000/Home
127.0.0.1:8000/About
127.0.0.1:8000/Services
127.0.0.1:8000/Portfolio
All the tabs with url entry.
But, when I create an url entry in html template, {% url 'About' %}
getting NoReverseMatch
Very clever - I see you have 'short circuited' adding extra url patterns with your 'tab' path.
The issue is that django - specifically your templates - do not know what url 'About' refers to as you have not declared it as a name to any route in your url patterns.
i.e. something like:
path('/path/to/about', views.about_function, name='About')
What you can do in your template is hard code the path, for example:
<a href='/hardcode/this/path'> About </a>
Just note that if you change the 'About' page path, you'll need to replace it everywhere it's defined in any of your templates - plus possibly some other side effects.
So I'm using django-hosts so as to have multiple domains for each app. But then I'm having an issue when trying to access the url index for each of the apps in my base.html
I'm trying to have something like this in my base.html file
<div>
<ul>
<li>Home</li>
<li>Laundry</li>
<li>Tailor</li>
</ul>
</div>
and this is my hosts.py file
from django_hosts import patterns, host
from django.conf import settings
host_patterns = patterns(
'',
host(r'www', 'artisan.home_urls', name='www'),
host(r'laundry', 'artisan.laundry_urls', name='laundry'),
host(r'tailor', 'artisan.tailor_urls', name='tailor'),
host(r'admin', settings.ROOT_URLCONF, name='admin')
)
And I have 3 files, namely home_urls.py, laundry_urls.py, and tailor_urls.py and each of them have the same format as the one below
# home_urls.py
from django.urls import path, include
app_name = 'home'
urlpatterns = [
path('', include('home.urls')),
]
# tailor_urls.py
from django.urls import path, include
app_name = 'tailor'
urlpatterns = [
path('', include('tailor.urls')),
]
# laundry_urls.py
from django.urls import path, include
app_name = 'laundry'
urlpatterns = [
path('', include('laundry.urls')),
]
But then, the problem is that only the namespacing for the home.urls is working. For the laundry and tailor urls, I keep getting a namespace error and I have no idea why. I was wondering if maybe I made a mistake in defining the hosts.py file, but I'm just not sure. Any help would be appreciated. If there's any other file you need to understand the question better, just ask. I'd be glad to provide one. Btw, I'm using Django 3.0 with Python 3.7 if that helps.
i'm building my first Django app so i'm know i'm missing a lot of things, but i installed a gallery for my site and is inside site-packages, i set the url inside urls.py and it looks like this :
from django.contrib import admin
from django.urls import path,include
from django.conf import settings
from django.conf.urls.static import static
from contacto.views import Home,Contacto
urlpatterns = [
path('admin/', admin.site.urls),
path('gallery/', include('gallery.urls'),name='gallery'),
path('home/',Home,name='home'),
path('contacto/',Contacto,name='contacto')
]
urlpatterns += static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
I can perfectly access the gallery and its functionalities if i write the url in the browser, but i cannot reference the gallery Url inside my template like the other Urls using {% url 'gallery' %}, i keep getting this error :
Reverse for 'gallery' not found. 'gallery' is not a valid view function or pattern name.
Also heres the gallery url:
from django.urls import path
from gallery.views import ImageView, ImageList, AlbumView, AlbumList, ImageCreate
app_name = 'gallery'
urlpatterns = [
path('', AlbumList.as_view(), name='album_list'),
path('images/', ImageList.as_view(), name='image_list'),
path('image/<int:pk>/<slug>', ImageView.as_view(), name='image_detail'),
path('upload/', ImageCreate.as_view(), name='image_upload'),
path('album/<int:pk>/<slug>/', AlbumView.as_view(), name='album_detail'),
path('album/<int:apk>/<int:pk>/<slug>', ImageView.as_view(), name='album_image_detail')
]
Thanks!
app_name = 'gallery'
urlpatterns = [
path('admin/', admin.site.urls),
path('gallery/', include('gallery.urls')),
path('home/',Home,name='home'),
path('contacto/',Contacto,name='contacto')
]
Change it as shown above and call gallery:album_list for album list, and follow same pattern for others
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.
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