Can't view thumbnail created in django admin - python

I am trying to create my own thumbnails within the Django admin and I believe they are being properly pointed to. However I cannot view the thumbnail. This is what I see:
When I click on the thumbnail, I get redirected to this URL and all I see is an empty white page.
http://localhost:8000/media/media/Screen_Shot_2016-04-08_at_12.55.25_PM.png
It is the correct URL but I do not see anything within the page.
Here is what code I have:
Within my settings.py
All files will go to cherngloong/uploads and /media/ will be the public front facing representation of the MEDIA_ROOT
MEDIA_ROOT = os.path.join(BASE_DIR, 'uploads')
MEDIA_URL = '/media/'
Within cherngloong/cherngloong/urls.py I appended to the list to serve static files:
urlpatterns = [
url(r'^admin', include(admin.site.urls)),
url(r'', TemplateView.as_view(template_name="index.html"), name="index"),
url(r'^api/', include('api.urls'))
] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
My cherngloong/api/urls.py:
urlpatterns = [
url(r'ig$', most_recent)
]
cherngloon/api/models.py:
class Media(models.Model):
...
...
file = models.FileField(upload_to="media/", default=None)
cherngloong/api/admin.py:
class MediaAdmin(admin.ModelAdmin):
search_fields = ["name", "file"]
list_display = ("name", "media_type", "url", "album", "display", "thumbnail")
def display(self, media_obj):
return '%s' % (media_obj.file.url, media_obj.file.name)
def thumbnail(self, media_obj):
location = media_obj.file.url
thumbnail_html = "<img border=\"0\" alt=\"\" src=\"{1}\" height=\"80\" />".format(location, location)
return thumbnail_html
thumbnail.allow_tags = True
display.allow_tags = True
Project structure:

I can't see any inconsistency on the code you have. I just created a project on GitHub with the same structure, and code you posted here, and everything worked like a charm, you can test it yourself, see the code here: https://github.com/vladir/cherngloong. I used the last version of Django, I know you are using a version below Django 1.9 because you are still using allow_tags, but even so your code seems good for me. Perhaps my code can help you to find what is happening.
I found why it is happening. Remove this line:
url(r'', TemplateView.as_view(template_name="index.html"), name="index"),
from your cherngloong/cherngloong/urls.py, and it will render ok.
It seems as this URL is too open, and it interferes with the thumbnails URL.
It could work for you instead:
url(r'^$', TemplateView.as_view(template_name="index.html"), name="index"),
I updated my code on the repo, so that you can test that it is working.

Related

Django "TemplateView" and "/media/" url conflict

I am writing an SPA with Django 1.11 (switching to 2.0 is no an option), as backend, getting all the data from Django Rest Framework API and I route my app via React routing.
Here is my my main urls.py :
urlpatterns = [
url(r'^api/', include('text_cms.urls')),
url(r'^api/', include('photos_admin.urls')),
url(r'^admin/', admin.site.urls),
url('', TemplateView.as_view(template_name='index.html'),
]
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
And here is my settings.py file:
MEDIA_ROOT = os.path.join(BASE_DIR, 'media').replace('\\', '/')
MEDIA_URL = '/media-files/'
The issue is, that the particular url setting
url('', TemplateView.as_view(template_name='index.html'),
is messing up media url, and files uploaded by user cannot be reached by url link, even though they are saved to folder, I just get a 404 error. When I comment my "Template as view" url, delete it or just give it another address, like url('main/') - everything works fine again.
I've tried to serve the template from the other app and registering it in the main urls.py file, but it did not work too
urlpatterns = [
url(r'^', views.IndexView),
]
views.py
def IndexView(request):
return render(request, 'main/index.html', {})
url('', TemplateView.as_view(template_name='index.html'),
You are missing a closing ). It also appears that you url pattern is incorrect as well. Should be
url(r'^$' , TemplateView.as_view(template_name='index.html')),

Django Admin: Determine correct image upload path

I am new to Django and I am currently having problems in showing uploaded images in Django Admin. I have followed many posted Q and A here in stackoverflow but of those worked in my problem. I hope any active of the coding ninja here could help me with this problem. Here is the detailed view of the problem:
I have defined the MEDIA_ROOT and MEDIA_URL in settings.py
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
MEDIA_ROOT = os.path.join(BASE_DIR, "media")
MEDIA_URL = "/media/"
This is my upload model in models.py:
from django.utils.html import mark_safe
class ImageDetails(models.Model):
image = models.ImageField(null=True)
def image_img(self):
if self.image:
return mark_safe('<img src="%s" height="125px" width="125px"/>' % (self.image.url))
else:
return '(No image found)'
image_img.short_description = 'Thumbnail'
In my Application urls.py:
urlpatterns = [
url(r'^inputImage', views.inputImage, name='inputImage'),
url(r'', views.index),
]
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
In my admin.py:
class ImageDetailsAdmin(admin.ModelAdmin):
fields = ["image"] #for file upload
list_display = ("image_img",)
admin.site.register(ImageDetails, ImageDetailsAdmin)
The image was successfully stored at ProjectDIR/media. The HTML returns the url: http://127.0.0.1:8000/media/imagename.jpg at img tag. But the page fails to load the image (I will be redirected to index page whenever when using the url http://127.0.0.1:8000/media/imagename.jpg). I am using Django version 1.10
As suspected, this problem is about URLs in Django. The problem occurred because I declared the following urlpattern in an app urls.py:
url(r'', views.index, name='index'),
I solved the problem by changing the code to:
url(r'^$', views.index, name='index'),
And adding the following code at the project's main urls.py:
if settings.DEBUG:
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)

Tell me the way to use Django's models.FileField

I'm using models.FileField like this below.
It's a fantastic django function. Because, it makes user can upload within Django administration page without any code.
So, I clicked Part image url Link. But, I got a error message below.
my urls pattern is below.
urls.py
urlpatterns = [
url(r'^parts/', include('parts.urls', namespace='parts')),
url(r'^admin/', include(admin.site.urls)),
url(r'^$', views.MW_Hello.as_view(), name='Hello'),
]
Do I have to add url mapping in url patterns?
You need to configure your urls and settings for managing the static files
https://docs.djangoproject.com/en/1.8/howto/static-files/
https://docs.djangoproject.com/en/1.8/topics/files/

wagtail return server error 500 when insert embed

env:
Django==1.7.8
wagtail==0.8.8
execute "manage.py runserver" to launch the website
I copy a example.mp4 file to /static dir, then can open it in browser#127.0.0.1:8000/statice/example.mp4
but when I insert http://127.0.0.1:8000/statice/example.mp4 to page as a embed component, the server return a 500 error.
then I use another internet url, http://viemo.com/86036070, and wagtail still show a 500 error.
the embed url will be send to /admin/embeds/chooser/upload/
I decide to review the related source code to find out, but...
top urls.py:
urlpatterns = patterns('',
url(r'^django-admin/', include(admin.site.urls)),
url(r'^admin/', include(wagtailadmin_urls)),
url(r'^search/', include(wagtailsearch_urls)),
url(r'^documents/', include(wagtaildocs_urls)),
url('^sitemap\.xml$', sitemap),
url(r'', include(wagtail_urls)),
)
and admin_usrls:
from django.conf.urls import url
from wagtail.wagtailadmin.forms import PasswordResetForm
from wagtail.wagtailadmin.views import account, chooser, home, pages, tags, userbar, page_privacy
from wagtail.wagtailcore import hooks
urlpatterns += [
url(r'^$', home.home, name='wagtailadmin_home'),
url(r'^failwhale/$', home.error_test, name='wagtailadmin_error_test'),
url(r'^explorer-nav/$', pages.explorer_nav, name='wagtailadmin_explorer_nav'),
url(r'^pages/$', pages.index, name='wagtailadmin_explore_root'),
url(r'^pages/(\d+)/$', pages.index, name='wagtailadmin_explore'),
url(r'^pages/new/(\w+)/(\w+)/(\d+)/$', pages.create, name='wagtailadmin_pages_create'),
url(r'^pages/new/(\w+)/(\w+)/(\d+)/preview/$', pages.preview_on_create, name='wagtailadmin_pages_preview_on_create'),
url(r'^pages/usage/(\w+)/(\w+)/$', pages.content_type_use, name='wagtailadmin_pages_type_use'),
url(r'^pages/(\d+)/edit/$', pages.edit, name='wagtailadmin_pages_edit'),
url(r'^pages/(\d+)/edit/preview/$', pages.preview_on_edit, name='wagtailadmin_pages_preview_on_edit'),
url(r'^pages/preview/$', pages.preview, name='wagtailadmin_pages_preview'),
url(r'^pages/preview_loading/$', pages.preview_loading, name='wagtailadmin_pages_preview_loading'),
url(r'^pages/(\d+)/view_draft/$', pages.view_draft, name='wagtailadmin_pages_view_draft'),
url(r'^pages/(\d+)/add_subpage/$', pages.add_subpage, name='wagtailadmin_pages_add_subpage'),
url(r'^pages/(\d+)/delete/$', pages.delete, name='wagtailadmin_pages_delete'),
url(r'^pages/(\d+)/unpublish/$', pages.unpublish, name='wagtailadmin_pages_unpublish'),
url(r'^pages/search/$', pages.search, name='wagtailadmin_pages_search'),
url(r'^pages/(\d+)/move/$', pages.move_choose_destination, name='wagtailadmin_pages_move'),
url(r'^pages/(\d+)/move/(\d+)/$', pages.move_choose_destination, name='wagtailadmin_pages_move_choose_destination'),
url(r'^pages/(\d+)/move/(\d+)/confirm/$', pages.move_confirm, name='wagtailadmin_pages_move_confirm'),
url(r'^pages/(\d+)/set_position/$', pages.set_page_position, name='wagtailadmin_pages_set_page_position'),
url(r'^pages/(\d+)/copy/$', pages.copy, name='wagtailadmin_pages_copy'),
url(r'^pages/moderation/(\d+)/approve/$', pages.approve_moderation, name='wagtailadmin_pages_approve_moderation'),
url(r'^pages/moderation/(\d+)/reject/$', pages.reject_moderation, name='wagtailadmin_pages_reject_moderation'),
url(r'^pages/moderation/(\d+)/preview/$', pages.preview_for_moderation, name='wagtailadmin_pages_preview_for_moderation'),
url(r'^pages/(\d+)/privacy/$', page_privacy.set_privacy, name='wagtailadmin_pages_set_privacy'),
url(r'^pages/(\d+)/lock/$', pages.lock, name='wagtailadmin_pages_lock'),
url(r'^pages/(\d+)/unlock/$', pages.unlock, name='wagtailadmin_pages_unlock'),
url(r'^choose-page/$', chooser.browse, name='wagtailadmin_choose_page'),
url(r'^choose-page/(\d+)/$', chooser.browse, name='wagtailadmin_choose_page_child'),
url(r'^choose-external-link/$', chooser.external_link, name='wagtailadmin_choose_page_external_link'),
url(r'^choose-email-link/$', chooser.email_link, name='wagtailadmin_choose_page_email_link'),
url(r'^tag-autocomplete/$', tags.autocomplete, name='wagtailadmin_tag_autocomplete'),
# 登陆
url(r'^login/$', account.login, name='wagtailadmin_login'),
url(r'^account/$', account.account, name='wagtailadmin_account'),
url(r'^account/change_password/$', account.change_password, name='wagtailadmin_account_change_password'),
url(r'^account/notification_preferences/$', account.notification_preferences, name='wagtailadmin_account_notification_preferences'),
url(r'^logout/$', account.logout, name='wagtailadmin_logout'),
url(r'^userbar/(\d+)/$', userbar.for_frontend, name='wagtailadmin_userbar_frontend'),
url(r'^userbar/moderation/(\d+)/$', userbar.for_moderation, name='wagtailadmin_userbar_moderation'),
]
# This is here to make sure that 'django.contrib.auth.views.login' is reversed correctly
# It must be placed after 'wagtailadmin_login' to prevent this from being used
urlpatterns += [
url(r'^login/$', 'django.contrib.auth.views.login'),
]
# Import additional urlpatterns from any apps that define a register_admin_urls hook
for fn in hooks.get_hooks('register_admin_urls'):
urls = fn()
if urls:
urlpatterns += urls
I don't find which view /admin/embeds/chooser/upload/ will be routed to. but I am sure there is a view function map to this url, because I add a "print" in django.forms.forms.is_valid method, and it be triggered.
can anybody help me? thanks in advance, and sorry for my D-level english.
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
update:
I create a new wagtail project, and write a simple page model, then I insert a embed to the content field, a RichTextField, the server return OK-200, but nothing was insert to content in edit panel.
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
update:
the view for the path is wagtailembeds.views.chooser.chooser_upload. I will check it.
thanks all.

How to organize and access URLS and TEMPLATES in django

i have just begun learning Django, and i stick with the principle that the fastest and best way to learn is through practice. I'm on the process of building my first web app, and i would really appreciate your help on the following :
I'm working on getting the front end to show up. But i'm having a hard time understanding the way the URLS work.
I have the following directory :
/myApp
/myApp
/public
/templates
/account
login.html
base.html
settings.py
urls.py
...
/account
urls.py
views.py
I have this on my myApp(the main app) 'urls.py' file
urlpatterns = patterns('',
...
url(r'^$', include(account.urls), name='account'),
)
And inside my account urls.py file, i already have the ff :
...
urlpatterns = patterns(
'account.views',
url(r'^$', 'login_user', name='login'),
)
I already defined following on the account views.py file :
def login_user(request):
return render(request, 'account/login.html')
So i guess the request should render my login.html file.
But i get the error that,
NameError at /
name 'account' is not defined
Therefore, i've figured that there must be something wrong with my settings.py file, right?
So here it is if it serves some purpose (just the important stuffs) :
...
BASE_DIR = os.path.join(os.path.dirname(__file__), '.')
...
ROOT_URLCONF = 'myApp.urls'
WSGI_APPLICATION = 'myApp.wsgi.application'
TEMPLATE_DIRS = [
os.path.join(BASE_DIR,'templates'),
]
STATIC_ROOT = os.path.join(BASE_DIR,'static')
STATIC_URL = "/static/"
STATICFILES_DIRS = [
os.path.join(BASE_DIR,'public'),
]
Right now, i really need to at least get the front end working. I hope the details i gave you gives you an idea of how i am organizing my file right now.
Additional note : I want to just create one template directory for the whole app. And as you can see on the structure, the template folder is inside the main app. How do i configure it inside the settings such that the apps use the main template folder?
You should put the account.urls in quotes. Also remove the $ sign from the regex:
url(r'^', include('account.urls')),
And in the account/urls.py file you should correct the base module name from oauth to the acount (your view is account.views.login_user, not the oauth.views.login_user):
urlpatterns = patterns('account.views',
....
)

Categories