I'm trying to accomplish something I believed would be simple: upload an image (via admin), then use the complete URL in a template to display the image.
The problem is I can only print the relative URL: /pics/filename.jpg. I believe the sequence is settings.py (MEDIA_ROOT, MEDIA_URL), urls.py, views.py and then mytemplate.html.I hope someone can find what's missing.
Settings:
STATIC_URL = '/static/'
MEDIA_ROOT = '/Users/ed/code/projects/djcms/pics/'
MEDIA_URL = '/pics/'
Urls.py:
from django.conf import settings
from django.conf.urls.static import url, static
from . import views
urlpatterns = [
url(r'^$', views.index, name='index'),
url(r'^news/(?P<pk>[0-9]+)/$', views.detail, name='detail'),
] + static(r'^$/pics/', document_root = settings.MEDIA_URL)
Views.py
def detail(request, pk):
story = Article.objects.get(pk=pk)
my_pic = Photo.objects.get(pk=pk)
print(story.image.url)
print(my_pic)
print(story.body)
print(request)
context = {'story': story}
return render(request, 'articles/detail.html', context)
The Error with story.image.url:
AttributeError: 'Photo' object has no attribute 'url'
When I remove the .url, I get this partial URL:
pics/intro-bg.jpg
What am I missing? Thanks.
This setup is working for me, maybe it will help you. It is for latest version of Django. Many answers in OS are for older Django versions.
URLS:
from django.conf.urls.static import static
from django.conf import settings
urlpatterns = [
#url
]+ static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
Settings:
MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
MEDIA_URL = '/media/'
Template:
<img src="{{ foo.image.url }}"><br>
Model:
image = models.ImageField(upload_to = 'img/', default = 'img/None/no-img.jpg')
My foo model has an imagefield, when it is stored, I can retrieve the full url through item.image.url based on the above setup.
In urls.py, try: static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
The URLS seem to be causing the problem.
from django.conf import settings
from django.conf.urls.static import url, static
from . import views
urlpatterns = [
url(r'^$', views.index, name='index'),
url(r'^news/(?P<pk>[0-9]+)/$', views.detail, name='detail'),
] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
The logic behind those URL patterns displays my homepage and individual news articles. Each news article has a url of /news/, followed by the article number, i.e. /news/1.
Each article uses my Photo model via a ForeignKey in the Article model. This allows a photo to be tied to a specific article.
However, in my template, the URL is a jumble of news and images:
http://localhost:8000/news/2/img/phones.png.
The news URL correctly finds the news item but not the image associated with the news item. The correct result would display the individual article along with its associated image. Do I need another URL?
The Request object in Django has a method, build_absolute_uri() Using this method you can get absolute url of the image field.
So to get the absolute url, you can use two methods
First method: Getting absolute url in view while doing GET request.
from settings import MEDIA_URL
class ClassName(APIView):
def get(self, *args, **kwargs):
model = models.ModelName.objects.get(name='pran')
absolute_url = self.request.build_absolute_uri('/').strip("/") + MEDIA_URL + str(model.image_field_name)
return Response({'url': absolute_url}, 200)
Method 2: If you are doing POST request and you are using serializer then pass request as context object to it.
ser = serializers.MySerializerName(data=request.data}, context={'request': request})
Related
I want to create a blog site. I already created the homepage of the site and there is 4 articles on my blog site. I want to open an article by clicking on it and it will redirect me to the unique article page. Every article page has few images and headlines and line breakers. How will I upload these to my blog model using django?
Example article page...See the article page's picture
There's a few ways to do this, but I would recommend CKEditor for Django
For the Blog home page and Detail pages, look at django's class based views:
https://docs.djangoproject.com/en/3.2/ref/class-based-views/generic-display/
urls.py:
urlpatterns = [
path('', views.BlogListView.as_view(), name='blog_list'),
path('detail/<int:pk>', views.BlogDetailView.as_view(), name='blog_detail'),
]
# You can also use <slug:slug> instead of <int:pk>
views.py:
from blog.models import Blog
from django.views.generic import ListView, DetailView
class BlogListView(ListView):
model = Blog
class BlogDetailView(DetailView):
model = Blog
For the blog formatting page:
https://github.com/django-ckeditor/django-ckeditor
https://medium.com/djangotube/django-ckeditor-install-with-youtube-and-code-spinet-plugins-b873c643f649
Change up your message/body to a RichTextUploadingField and then you/users can format images with text as you see fit.
models.py
from ckeditor_uploader.fields import RichTextUploadingField
class Blog(models.Model):
title = models.CharField(max_length=100)
message = RichTextUploadingField()
In your settings follow the GitHub guide to setup CKEditor and you're also going to have to add MEDIA_URL and MEDIA_ROOT to your settings.py and project urls.py files.
https://docs.djangoproject.com/en/3.2/howto/static-files/
project/settings.py
MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
MEDIA_URL = '/media/'
project/urls.py
from django.conf import settings
from django.conf.urls.static import static
urlpatterns = [
# ... the rest of your URLconf goes here ...
] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
have the following problem, I have in my settings.py configured in the following way:
RUTA_PROYECTO = os.path.dirname(os.path.realpath(__file__))
MEDIA_ROOT = os.path.join(RUTA_PROYECTO,'fotos')
MEDIA_URL = '/media/'
In my urls file I have:
from django.conf.urls.static import static
Urlpatterns = patterns ('',
....
) + static (settings.MEDIA_URL, document_root = settings.MEDIA_ROOT)
I have in my models
def upload_name(instance, filename):
return '{}/{}'.format(instance.persona.nro_doc, filename)
class FotosPersona(models.Model):
persona = models.ForeignKey(Personas,related_name='fotos_persona')
tipo_foto = models.CharField(max_length=25)
foto = models.ImageField(upload_to=upload_name)
fecha = models.DateField(auto_now=True)
class Meta:
db_table = 'fotos_persona'
When I save the data, the image is stored in a folder within the following structure:
Project
|
|
|----Project
|---- settings.py
|---- media
|---- urls.py
.....
But when I want to display the image in a template, it is not displayed.
When in the browser I entered the
http://localhost:8000/media/other_folder/image_file.jpg url
it returns me an HTTP 500 error.
I know my answer is a little late, but this might be useful considering your set up. Essentially, you have given the MEDIA_URL as "/media/". This is all right.
However, you would be facing a problem with reference to the URL patterns. You will probably have a generic URL along the lines of:
path('<index>', views.index, name='index'),
In such a scenario this URL would be processing everything that comes from the media URL. Although this is not ideal, only for the purpose of development I would recommend changing your URL patterns to the following:
urlpatterns = static (settings.MEDIA_URL, document_root = settings.MEDIA_ROOT) + [
...
]
Now in the hierarchy of processing URL patterns "/media/" would come before any URL that has a general pattern. Now media URLs would be processed before everything else.
Hopefully, this can address your query.
I can upload an image through the admin page, but the image can not be found when I navigate to the url that is generated by django. (404 error)
The files are being uploaded to the folder:
project_root/media/eventbanner/1/
I have tried multiple solutions but none seem to work for my situation.
Django 1.10 is being run local on Ubuntu 16.04.
The url I get is:
http://localhost:8000/media/eventbanner/1/banner_image.jpg
Media root folder is located at:
/home/username/xxx/xxx/project_name/media
Code in HTML file:
<div class="banner-image">
<img src="{{ event.eventbanner.banner_image.url }}"/>
</div>
url.py code:
from django.conf.urls import url, include
from django.contrib import admin
from . import views
from django.conf import settings
from django.conf.urls.static import static
app_name = 'events'
urlpatterns = [
url(r'^$', views.index, name='index'),
url(r'^details/(?P<event_id>[0-9]+)/$', views.details, name='details'),
url(r'^details/(?P<event_id>[0-9]+)/addcomment/$', views.add_comment, name='add_comment'),
] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
settings.py
STATIC_URL = '/static/'
STATICFILES_DIRS =[os.path.join(BASE_DIR, 'static'),]
MEDIA_ROOT = os.path.join(BASE_DIR, 'media/')
MEDIA_URL = '/media/'
models.py
def validate_only_one_instance(obj):
model = obj.__class__
if (model.objects.count() > 0 and obj.id != model.objects.get().id):
raise ValidationError("Can only create 1 %s instance" % model.__name__)
class EventBanner(models.Model):
event = models.OneToOneField(Event, unique=True)
banner_image = models.ImageField(upload_to=get_image_path, blank=True, null=True)
def clean(self):
validate_only_one_instance(self)
The real problem here is that there is no relationship between this url http://localhost:8000/media/eventbanner/1/banner_image.jpg and this location on disk /home/username/xxx/xxx/project_name/media.
In a production application you'd have a web server where you'd store your Media content, the serving URL would be MEDIA_ROOT and you'd append ImageField.url to this value to get a valid image path.
What you need here is to set up a web server for your media images. At first that sounds like a lot of work, but Django provides a shortcut...
Serving Files in Development
You have some work you need to do to have the media files served locally. It requires some changes to your urls.py ...
from django.conf import settings
from django.views.static import serve
# ... the rest of your URLconf goes here ...
if settings.DEBUG:
urlpatterns += [
url(r'^media/(?P<path>.*)$', serve, {
'document_root': settings.MEDIA_ROOT,
}),
]
This uses the views.serve bit and should only be used in DEBUG mode. It overrides the path to media files(django's term for user uploaded content like ImageField). This will redirect those requests through the serve view. Best I can tell this is a mini web server that will map those request routes to locations on disk and allow those locations to be reachable via HTTP urls.
As of at least Django 1.8, there is a helper function static() that will set this up for you and ensure that it only functions in debug mode.
Your urls.py should look something like this:
urlpatterns = [
# ... the rest of your URLconf goes here ...
] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
...to simply quote the documentation.
Make sure that your MEDIA_URL is set to a relative path like /media/ and that your MEDIA_ROOT is an absolute filesystem path like /home/foo/project/media.
I'm using django admin site (v1.9.6) and recently installed file-browser for managing images and documents. I followed installation steps and file-browser is shown in admin interface but I cant upload images and documents. When trying to access them 404 errors shows up (also thumbnails are not shown in file-browser page):
Page not found (404)
Request Method: GET
Request URL: http://127.0.0.1:8000/media/uploads/aaaaaaaaaaalula.png
But the file is there, if I make
ls media/uploads/aaaaaaaaaaalula.png
it shows the file, so it's uploaded.
my settings.py:
MEDIA_ROOT = os.path.join(BASE_DIR, 'media') MEDIA_URL = '/media/'
ADMIN_MEDIA_PREFIX = '/media/admin/'
and url.py
from django.conf.urls import patterns, include, url
from django.contrib import admin
from filebrowser.sites import site
urlpatterns = [
url(r'^admin/filebrowser', include(site.urls)),
url(r'^admin/', include(admin.site.urls)),
url(r'^$', include(admin.site.urls)),
]
Thanks in advance for help.
C.
Ok, here is the solution:
https://docs.djangoproject.com/en/1.9/howto/static-files/#serving-static-files-during-development
The issue was that I was testing django-filebrowser with development server
You are not putting url for displaying image.
In setting.py-
MEDIA_ROOT = os.path.join(BASE_DIR, "media/")
MEDIA_URL = '/media/'
In URLs.py--
Url(r'^media/(?^<path>.*)$','django.views.static.serve',{'document_root':
settings.MEDIA_ROOT,}),
I think it can help you
You can do:
from django.conf.urls.static import static
from django.conf import settings
urlpatterns = [
#your urls
]
if settings.DEBUG: # will be 'True' in development
urlpatterns += static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
I have created a page where a user can add a new item (notes in this case) and I am making use of CBV which I have recently started learning.
This is my model form
class NoteForm(forms.ModelForm):
class Meta:
model = Note
fields = ('title', 'note', 'tags')
This is the view in views.py
class NoteCreate(CreateView):
model = Note
form_class = NoteForm
template_name = "add_note.html"
Then this is the url as I used in the urls.py of the app
from django.conf.urls import patterns, url
from . import views
from madNotes.views import NoteCreate, NoteIndex,
urlpatterns = patterns(
'',
url(r'^notes/add/$', NoteCreate.as_view(), name="new_note"),
url(r'^$', NoteIndex.as_view()),
url(r'^(?P<slug>\S+)/$', views.NoteDetail.as_view(), name="entry_detail"),
)
NB: I used the same url as the main page at 127.0.0.1:8000 in the projects urls.py file and it worked.
I have seen several tutorials and even the docs and can't seem to find what I am doing wrong. Will I also need to add a function in order for it to be saved in the db or the CBV will do it all?
EDit: The error I get is this
Page not found (404)
Request Method: GET
Request URL: http://127.0.0.1:8000/notes/add/
Here is the project's urls.py
from django.conf.urls import patterns, include, url
from django.contrib import admin
from MadNotez import settings
from registration.backends.default.views import RegistrationView
from madNotes.forms import ExRegistrationForm
if settings.DEBUG:
import debug_toolbar
urlpatterns = patterns('',
url(r'^__debug__/', include(debug_toolbar.urls)),
url(r'accounts/register/$', RegistrationView.as_view(form_class = ExRegistrationForm), name='registration_register'),
url(r'^accounts/', include('registration.backends.simple.urls')),
url(r'^admin/', include(admin.site.urls)),
url('^markdown/', include('django_markdown.urls')),
url('^notes/', include('madNotes.urls')),
#url(r'^$', views.NoteCreate.as_view(), name="new note"), when I used it here it worked
)
you say that is the urls.py of the app, which means it is included by the project's urls.py.
As you show now, all the app's URIs go under the notes prefix:
url('^notes/', include('madNotes.urls')),
so as things stand at present the correct URI for the page is
http://127.0.0.1:8000/notes/notes/add/
In order to clean things up a bit, I'd suggest to modify the app's urls.py to
url(r'^add/$', NoteCreate.as_view(), name="new_note"),
so that the page can be reached at
http://127.0.0.1:8000/notes/add/
This way all the app's pages/services are available under the notes prefix and with a simple name that is consistent with their action (i.e. add, delete, etc)