I'm using django-cms admin style. I managed to change the default DjangoCMS logo, by following the solution mentioned here:
Django cms 3.4.1 admin dlogo
Now the logo is a static one, but I want it to be dynamic, meaning it should get the image path from the database, where the location is stored.
As these admin pages are not render through views.py, I'm not able to sent the querysets to it.
Can anyone suggest how to do it?
using context_processors we can do this.
first need to get this: https://github.com/divio/djangocms-admin-style/blob/master/djangocms_admin_style/templates/admin/inc/branding.html
branding.html file must be put inside templates folder under admin/inc folder, so the structure will be like this templates/admin/inc/branding.html
now suppose through context_processor we got company_logo, that holds the logo url from database.
then in branding.html <div id="header-logo"> would be like:
<div id="header-logo">
{% if company_logo %}
<img src="{{ company_logo.url }}" style="height:inherit;">
{% else %}
<a class="icon-logo" href="/"><span>django CMS</span></a>
{% endif %}
</div>
Related
I am trying to make a blogging website. I know django provides argument templates like
{% include images.html with value=sense %}
The above code directly works in HTML and hence everything works. The images are stored in a backend database and connected to everystory by some logic. The user can use the names of the images and call whenever they need to use it
When I try the above code directly in the backend it doesn't work because I think once something is rendered then it doesn't rerender by django HTML
I wish to paste some form of links in the django story backend. such that when it renders in HTML automatically the page should show pics in the appropriate place. If anyone has any idea how to do this kindly let me know.
So when loading stories in the database the user can put some form of links for images in the database and while rendering all images come in a certain format as specified in the block in the blog.So there can be any number of images and the count is not longer fixed as shown in the pics below where I am trying to render a image called sense from the backend which doesn't work.. whereas it directly works in the frontend.
<p>{{object.story_title}}</p>
<p>{{MEDIA_ROOT}}</p>
<p>{{object.story}}</p>
{% include "blogdescription/image.html" with value=sense %}
Thank you for your time.
with regards
Let me start saying that doing exactly what you want is not possible because Jinja will compile and render {{object.story}} and not its content (the include). It does not seem possible to use nested Jinja syntax to load any resources, includes, extends, urls, etc.
Which explains why when you place the include in the template it works but does not inside your model field.
What seems possible is to load an HTML image with a explicit URL to the resource, lets say, the content inside your text field is:
<div style="display: flex; justify-content: center; align-items: center;">
<img src="/static/myimage.jpg" alt="Object Image">
</div>
Template.html (source):
{% block content %}
{{obj.title}}
<br>
{{obj.body|safe}}
{% endblock %}
Alternatively, it is possible to generate a HTML file to render dynamically based on Object.field. Note that this solution is a heavy load on the server, for every request will generate a dynamic file to be rendered.
Obj field value:
{% extends 'base.html' %}
{% block content %}
{{obj.title}}
<hr>
{% include 'includes/image.html' %}
{% endblock %}
views.py:
def story(request, id):
obj = Story.objects.get(id=id)
f = open(f'templates/generated/dynamic_template.html', 'w+')
f.write(obj.body)
f.seek(0)
return render(request, 'generated/dynamic_file.html', {'obj': obj})
I have created a Django App and want to provide a custom Login page with only the possibility to use a Google login.
I have implemented the Google login based on this post: https://www.section.io/engineering-education/django-google-oauth/
It actually works fine and when I hit localhost/account/login I am getting a login page:
I actually do not want a sign up and a "local" sign in option, I only need a Google login.
To achieve this I have created the following folder structure to overwrite the login template (based on https://learndjango.com/tutorials/django-login-and-logout-tutorial):
MyApp/templaltes/registration/login.html
login.html has the following content:
{% extends "base.html" %}
{% load i18n %}
{% load socialaccount %}
{% block content %}
<div class="row">
<center><h1>{% trans 'LoginHeading' %}</h1></center>
</div>
{% if user.is_authenticated %}
<p>Welcome, You are logged in as {{ user.username }}</p>
{% else %}
Login With Google
{% endif %}
{% endblock %}
I have also added 'DIRS': [str(BASE_DIR.joinpath('templates'))] to the TEMPLATES section in settings.py.
Unfortunately I am still seeing the Django default login page. What am I missing?
(I have also restarted the dev server to make sure it is not an issue there with the same result)
I have figured it out. After reading the documentation of Allauth throughly I found this:
For instance, the view corresponding to the account_login URL uses the
template account/login.html. If you create a file with this name in
your code layout, it can override the one shipped with allauth.
So I changed the folder structure to account/login.html and it works.
To do this thing you can simply create an account folder under the template and add login.html inside it.
This will override your login page.
Make sure that folder name must be account.
In your case, you can rename your folder from "registration" to "account".
Like following:
I am sure this will work well.
Simple solution is to add
SOCIALACCOUNT_LOGIN_ON_GET=True
to your settings.py and it should skip/bypass the sign up form.
Credits: https://stackoverflow.com/a/70680165/11605100
Still does not work?
Stop the server and then;
python manage.py migrate
python manage.py makemigrations
python manage.py runserver
I have django-admin-interface installed in my django app. The problem is that when I try to upload a logo it says that all is okay but the logo doesn't show.
Logo uploaded correctly but not showing
I want to change the default Django logo.
Is there any way to accomplish this?
Create a new template and extends the 'admin' template like below to override the default admin template.
{% extends "admin/base.html" %}
{% block branding %}
<h1 id="site-name">MY_LOGO_OR_TEXT</h1>
{% endblock %}
In the code above, change the MY_LOGO_OR_TEXT section with your html logo or just text to be shown on the admin panel like you wanted.
ADMIN
I am currently learning Django and making my first steps. I try to build a webgallery to learn all the basic stuff. I successfully displayed some images using static files. So I tried saving Images through ImageFields and "upload_to" in my DB, saving it to my static directory. I tried to display everyone of them with a for loop in an tag. My img displays properly with using a {% static %} tag but when I try to insert a {{ }} Tag it isn't working, although it's the same url it doesn't work.
I tried changing my STATIC FILE in settings.py
I tried various other forms of nesting my {{}} in there
Reading the docs to staticfile https://docs.djangoproject.com/en/2.2/howto/static-files/
This thread Display an image located in the database in Django
This thread https://docs.djangoproject.com/en/2.2/topics/files/#using-files-in-models
My Code:
<p>Overview</p>
{% block content %}
<div>
{% for image in images %}
{{ image.img_photo }} <!-- webgalleries/test.jpg -->
{% load static %}
<img src="{% static 'webgalleries/test.jpg' %}" alt="{{ image }}"> <!-- working -->
<img src="{% static '{{ image.img_photo }}' %}" alt="{{ image }}"> <!-- not working -->
{% empty %}
<p>No content</p>
{% endfor %}
</div>
{% endblock content %}
I expect the output to be an img from my static directory.
A hint, some advice or other forms of help is highly appreciated.
Thank you so much!
okay if you want to display images from database you should do these steps :
1- go to your settings.py and write this code there ,
MEDIA_ROOT= os.path.join(BASE_DIR,"media")
MEDIA_URL= "/media/"
2- then create new folder in your project called 'media' and create folder inside 'media' called 'images' (finally result will be like this 'media/images' )
3- go to your model.py in your class that having 'img_photo'
and you should write the model like this
class Images(models.Model):
img_photo = models.ImageField(upload_to='images/',null=True, blank=True)
def get_image(self):
if self.img_photo and hasattr(self.img_photo, 'url'):
return self.img_photo.url
else:
return '/path/to/default/image'
def __str__(self):
return self.img_photo
4- go to admin.py then write :
from yourapp.models import Images
then add this line below
admin.site.register(Images)
then open your terminal or console and write :
1- python manage.py makemigrations
2- python manage.py migrate
5- in html code you must write :
{% for image in Images %}
<img src="{{ image.get_image }}" >
{% endfor %}
go to admin panel and upload any photo for test
I'm working on a simple blog app in Django, and i'm having trouble figuring out how to dynamically generate the five most recent posts in a side bar. Each of my views are class based and they extend a generic template, each view maps to one template which I believe is the correct way to do it. I've looked for a way to do this using template tags, but it seems Django doesn't like you to put any logic inside of your templates.
The problem I believe is that I want this to exist within my base.html because I want the recent posts to be displayed site-wide, is a view even supposed to map to your base.html or does that cause problems, i'm pretty new with this. I don't know how to approach this, whether i'm supposed to create a new view for base.html or if I should use my template tags, or if I should extend an existing view(but if I do that it won't be site wide?).
I essentially want the following(they're ordered in reverse chronological order)
{% for post in post_list[:4] %}
{{ post.title }}
{% endfor %}
You can use a template tag. More specifically, an inclusion tag is what you need. This allows you to insert a rendered snippet anywhere inside your template via a small view-like piece of code.
For example, create a templatetags/blog_tags.py file (it's important that you create the templatetags folder within your app; Django searches for them here by default) in your blog app and add the following:
from django import template
register = template.Library()
#register.inclusion_tag('blog/snippets/recent_posts.html')
def render_recent_blogposts():
return {
# This is just an example query, your actual models may vary
'post_list': BlogPost.objects.all().order_by("published_on")[:4]
}
now create a blog/snippets/recent_posts.html template (it can be anywhere as long as it mathecs the #register.inclusion_tag(...) above.):
<ul>
{% for post in post_list %}
<li> {{ post.title }}</li>
...
{% endfor %}
</ul>
finally, in your original template, you can now render your template tags:
<aside>
{% load blog_tags %}
{% render_recent_blogposts %}
</aside>