Django : accessing uploaded picture from ImageField - django

Hi I'm having a problem about referring to uploaded images in HTML.
I'm using ImageField to save profile pictures :
picture = models.ImageField(upload_to='stories/static/stories/profile_images/', blank=True)
As you can see, It'll upload to <App folder>/static/stories/profile_images/
Then I refer it in HTML like this :
<div class="profilepic" style="background-image: url('/{{ x.writer.picture }}');"></div>
Which directs to
ROOT/stories/static/stories/profile_images/.jpg
Instead of
ROOT/static/stories/profile_images/.jpg
Any ideas?
(I'm trying to reverse-truncate the first 8 character, but there is not built-in template filter available)

since your upload_to is stories/static/stories/profile_images/, it IS showing right path:
ROOT/stories/static/stories/profile_images/.jpg
and one more thing, you need .url to get the picture with its path
{{ x.writer.picture.url }}

Related

How do I remove <p> tag from django richtextfield?

I am getting p tag displayed after I post an image or question with ckeditor editor in Django. .How can I remove the tag and get the image or text be displayed correctly.
You need to explicitly state that the content in template should be considered safe. You can use safe filter (Django docs) to do that so for variable text_content in your template you should do {{ text_content | safe }}.

Ckeditor - user friendly image upload

I have ckeditor 4 with image upload on my website. Everything works great the only issue is that it visually looks very bad. Is it possible to style or change the UI for the image upload as I expect users will have a hard time understanding it. I just want a simple upload button nothing fancy
Update:
I discovered the easy image plugin and it does exactly what i need, just a simple image upload button. The issue is it requires a cloud service subscription so it wont work for me.
You can add style and customize the input, one way could be:
{%if field.name == 'picture' %}
<label for ="id_picture" id="icon_upload" class="fa fa-camera"></label>
<input type="file" name="picture" id="id_picture" accept="image/*"style="display: none;" >
{% endif %}
the field name usually is take of the field name model,, but it can change depending on how the form is passed to a template.
https://docs.djangoproject.com/en/3.2/ref/forms/fields/
I used this to remove all the dialog tabs and just kept the image upload tab
CKEDITOR_CONFIGS = {
'default': {
'removeDialogTabs': 'image:advanced;image:Link;image:info',
}

Custom image in Django template via static

I am programming a website powered by Django 1.6.3. Under news you always see a big story and on the side recent articles. Therefore, I used Bootstrap 3 and wrote a static html page serving my requirements.
Now I would like to program the logic behind this. I save all my files under static that consists of the folders css, fonts (from Bootstrap), img and js. The image folder has some subfolder e.g. news. To create a new entry on the news page I would like to open the admin page, add a news_entry and select one image that should be under the titel. How is it possible to include the image in the page? My approach was:
<img src="{% static {{ news.image_name }} %}" class="img-title">
Unfortunately I get an parsing error. Image_name is a property of my news model.
You need to access the url attribute of your image_name.
Try:
<img src="{{ news.image_name.url }}" class="img-title">
Similar question here.

Django: show raw html (from database) as html, not rendered

I have created a rich text area using tinymce. I have now saved this to my database and want to display it. It gets saved in the database as html with tags, etc. To display it I have tried:
In views:
content = get_content_from_db()
render_to_response("template.html", {"content":content})
In template:
{{content}}
This shows the content with the tags like so:
<strong>My text</strong>
instead of just displaying the text as bold.
I have also tried using the verbatim tag, but this (doh!) displays as:
{{content}}
How should I do this? Do I need to save it differently? I save it the way I save any other TextArea except the field is a blob.
Use safe filter
{{ content|safe }}

Django CMS - Call a cmsplugin in a template tag

My problem
With Django CMS 2.3.3, when creating a Page I use cmsplugin_picture* next to a couple of other cmsplugins. In my cms template, instead of doing:
{% placholder "content" %} //calling the Django Page including all plugins...
I would like to call each cmsplugin seperately, but how would I do that?
I looked at Django tag template (filters) here and also studied Django CMS template tags here, but neither seem to suggest that possibility. I have to say I am a beginner so I might not have connected the dots...
What I try to achieve:
In my template I have a IMG tag (outside of the {% placeholder "content" %} tag) which I want to populate with an image url that I define in my Page/cmsplugin_picture. So I am looking for a placeholder tag that allows me to grab that image. In my wildest dreams I would name it:
{% show_placeholder "content" request.current_page.get_cmsplugin_picture %}
Obviously the above doesn't work, but does something like this exist?
**I have also tried cmsplugin_filer, but to me it isn't necessarely more beneficial to fix this particular problem.*
EDIT:
What I mean by Page/cmsplugin_picture -> In a Django CMS Page you can select between your installed cmsplugins to add to a Page. In my case I select cmsplugin_picture and upload an image (within that plugin). This image I want to 'call' in my Django Template. So it is a not a static url, but dynamic.
You should make a second placeholder where your img tag is (and optionally limit the types and amount of plugins using CMS_PLACEHOLDER_CONF (http://docs.django-cms.org/en/2.3.3/getting_started/configuration.html#cms-placeholder-conf).