Django extend tab into tabular.html existing file - templates

How do I extend/edit the "admin/edit_inline/tabular.html"?
I have this into my settings.py:
TEMPLATES = [
{
"BACKEND": "django.template.backends.django.DjangoTemplates",
"DIRS": [os.path.join(BASE_DIR, "Metamanager/templates")],
.....
}
My admin.TabularInLineinside the admin.py:
class FileInline(admin.TabularInline):
model = UploadFile
template = os.path.join(BASE_DIR, "Metamanager/templates/tabular.html")
and into my custom template:
{% load i18n admin_urls static admin_modify %}
{% block table_content %}
<td class="original"><p>holaaaaaaaaaaaaa</p></td>
{%endblock%}
not showing the td into the body as I don't know what need to extend into the custom html exactly from tabular.html.
I want to make the link redirect to a view I have don for direct downloading
I need to extend the tabular.html auto created by Django bear in mind I don't have Forms at all, the screenshoot is a Django FileField rendered from models.py

Related

Django: how to use {{ something }} inside css file

I have a model called Post with a FileField which is an image.
I want to do something like this:
.article-image{
background-image: url("../img/{{ post.postimage }});
}
but obviously it does not work, so I tried to make inline css instead:
<div class="article-image" style="background-image:{% static 'img/{{ post.postimage }}' %}"></div>
but it does not work too.
How can I fix this
for the first case you can't do that because it's CSS file not Django template.
but for the second case, you can use template tags like this
create templatetags/my_custom_tag.py in your app and add the following code into it.
from django import template
from your_base_app.settings import STATIC_URL
register = template.Library()
#register.simple_tag
def show_image(img):
return STATIC_URL + 'img/' + img;
you should load your tags in the top of your django template like that
{% load my_custom_tag %}
your django template should be like this
<div class="article-image" style="background-image:url({% show_image post.postimage.url %})"></div>

djangocms integration filer_image with apphook objects

I want to add filer_image field to my Gift class instance object. It works as apphook in django-cms. The main problem is that after making migrations and open the view where the form is I don't have loaded js.
I already added all tags:
{% load staticfiles i18n cms_tags sekizai_tags menu_tags thumbnail filer_tags filer_image_tags %}
The model is:
class Gift(models.Model):
filer_image = FilerImageField(related_name="book_covers")
The form:
class GiftForm(ModelForm):
class Meta:
model = Gift
fields = '__all__'
widgets = {
'name': forms.TextInput(attrs={'class': 'basic-input full-width'}),
}
The rendered output:
The thumbnail and input view
Please tell me what am I doing wrong with these. It seems to me like some js files are not loaded. After click it opens FileImageFiler gallery, but I also cannot select any image.
Ok, i find the soliton. Basically I added {{ form.media }} after {{ csrf_token } and I have extended the form class with:
class Media:
extend = False
css = {
'all': [
'filer/css/admin_filer.css',
]
}
js = (
'admin/js/core.js',
'admin/js/vendor/jquery/jquery.js',
'admin/js/jquery.init.js',
'admin/js/admin/RelatedObjectLookups.js',
'admin/js/actions.js',
'admin/js/admin/urlify.js',
'admin/js/prepopulate.js',
'filer/js/libs/dropzone.min.js',
'filer/js/addons/dropzone.init.js',
'filer/js/addons/popup_handling.js',
'filer/js/addons/widget.js',
'admin/js/related-widget-wrapper.js',
)
That is all!

showing dynamic data from database in all views

I'm using Django 2.0
I have a Model within my notes application
notes/models.py
class ColorLabels(models.Model):
title = models.CharField(max_lenght=50)
In the sidebar navigation which will be displayed on all pages, I want to show the list of color labels and it will show on all pages.
How can I show dynamic data in all views or say on all pages?
Use a custom template context processor.
This will add the context (data) you need into the templates rendered by all views.
First create the context processor (notes/context_processors.py):
from .models import ColorLabel
def color_labels(request):
return {'color_labels': ColorLabel.objects.all()}
Then add it to the context_processors option of your template renderer in settings.py:
TEMPLATES = [{
'BACKEND': '...',
'OPTIONS': {
'context_processors': [
'notes.context_processors.color_labels',
...
],
},
}]
Finally, you will be able to use it in your templates:
templates/base.html
<nav>
{% for color_label in color_labels %}
{{ color_label.title }}
{% endfor %}
</nav>

Django, viewing models from different app

I am super new to python programming and django and i got the basics out of the way. I created a project with two apps, home and video. In my video models.py i have the following data:
class Video(models.Model):
name = models.CharField(max_length=200)
description = models.TextField(blank=True, null=True)
I want to do something with this in my home app in the views.py, such as display the data in an html page and currently it is set up as followed:
from video.models import Video
def display_video(request):
video_list = Video.objects.all()
context = {'video_list': video_list}
return render(request, 'home/home.html', context)
in my home.html
{% if video_list %}
{% for video in video_list %}
<p>{{ video.name }}</p>
<p>{{ video.description }}</p>
{% endfor %}
{% else %}
<p>no videos to display</p>
{% endif %}
my home.html always returns "no videos to display"
But when i query Video.objects.all() in my video app it finds 2 objects. any help is appreciated.
I decided to delete the project and started over brand new but this time I used class views instead of function views. I'm not exactly sure why it didn't run but using class views it worked like a charm. So the equivalent in class views as simple as possible is.
from video.models import Video
class IndexView(generic.ListView):
template_name = 'home/index.html'
context_object_name = 'top_three'
def get_queryset(self):
return Video.objects.all()[:3]
In settings, check the following is there.
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [],
'APP_DIRS': True,
'OPTIONS': {
'context_processors': [
'django.template.context_processors.debug',
'django.template.context_processors.request',
'django.contrib.auth.context_processors.auth',
'django.contrib.messages.context_processors.messages',
],
},
},
]
It worked for me without using class. I just did what we normally do.
from django.shortcuts import render
from AllBlogs.models import Post # getting this post form AllBlogs app
# Create your views here.
def home (request):
# sort posts according to date in descending order
# only a single query is generated and then hit the database that's how this will not affect the performance of database
latest_blogs = Post.objects.all().order_by("-date")[:3]
return render(request,'Home/home.html',{'blog':latest_blogs})
And this is my template
<!--separated it from the home.html because we can use it in AllBlogs.html
and it save our work and time by just including without copying the same data several times-->
{% load static %}
<li>
<!--add field name which we used in models-->
<article class="blog">
<a href="{% url 'blogDetails_page' blog.slug %}"> <!--Different slug for different blog as slug will create a different url-->
<!--dtl filter to add back or url dynamic url creation-->
<image src="{% static 'images/'|add:blog.image_name %}" alt="{{blog.title}}">
<div class="blog__content">
<h3>{{blog.title}}</h3>
<p>{{blog.excerpt}}</P>
</div>
</a>
</article>
</li>
And this is the Post model
# having post data
class Post(models.Model):
title = models.CharField(max_length=150)
date = models.DateField(auto_now=True) # put the date in the field at which time we are saving the data
image_name = models.CharField(max_length=50) # later on we upload files right now we get images from static folder
excerpt = models.CharField(max_length=200)
content = models.TextField(validators=[MinLengthValidator(10)]) # TextFields same like CharField
slug = models.SlugField(unique=True, db_index=True) # set it as unique because we are going to use it as a primary key
author = models.ForeignKey(Author,on_delete=models.SET_NULL,related_name='posts',null=True) # one to many relation
tags = models.ManyToManyField(Tag) # ManyToManyRelation with tag
# represent post entry by it post title
def __str__(self):
return self.title

Passing currently logged user to submit_line.html in Django

I have been trying to get the currently logged user object in submit_line.html to control which buttons it displays. However I have realized that the user object is not passed to the context for submit_row. What would be the best way to pass a the currently logged user object to submit_row. Should I create a new template tag?
Nicola
You can get the current User by accessing the request object:
{{ request.user }}
Just make sure 'django.core.context_processors.request' is included in context_processors setting in settings.py:
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [
...
],
'APP_DIRS': True,
'OPTIONS': {
'context_processors': [
...
'django.core.context_processors.request', # this entry is important
],
},
},]
Yes you can also use {{ request.user }} in your template to get login user. And also you can use template tag to get login user for that
first create .py file let's consider 'templatetags.py' and write this view there
#register.assignment_tag(takes_context=True)
def user_tag(context):
request = context['request']
return request.user
Now load this templatetags.py file in your html file i.e.'submit_line.html'
{% load templatetags %}
{% user_tag as user %}
{% if user %}
{{ user }}
{% endif %}
Hope this will help you.