I'm Developing an app with django .
I want to customize django admin interface , but i cant add a custom font to it .
I Want to use a custom font for Persian Language .
Here is What i did but not get a correct result :
Step 1 :
I create a css file named admin-extra.css in this directory :
/templates/css/admin-extra.css
After That i Changed the postition of myappname before django.contrib.admin in Installed Apps Like This :
INSTALLED_APPS = [
'django.contrib.auth',
'pool',
'django.contrib.admin',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'fcm_django'
]
And the admin-extra.css is like this :
body {
margin: 0;
padding: 0;
font-size: 40px;
color: #333;
background: #fff;
}
And Finally I put admin-extra in a file named base_site.css in \templates\base_site.html and it's content is like this :
{% extends "admin/base.html" %}
{% load static from staticfiles %}
{% block title %}{{ title }} | {{ site_title|default:_('Django site admin') }}{% endblock %}
{% block extrastyle %}{{ block.super }}<link rel="stylesheet" type="text/css" href="{% static "css/admin-extra.css" %}" />{% endblock %}
{% block branding %}
<h1 id="site-name">{{ site_header|default:_('Django administration') }}</h1>
{% endblock %}
{% block nav-global %}{% endblock %}
But i cant see the result ,
what i've been missed or wrong ?
any suggestions will be helpfull .
Note : This is not a duplicate post .
And This is Admin.py file :
from django.contrib import admin
# Register your models here.
from .models import *
#admin.register(Ticket)
class TicketAdmin(admin.ModelAdmin):
list_display = ('id','title','body','answer')
#admin.register(Activation)
class ActivationAdmin(admin.ModelAdmin):
list_display = ('activecode','user_phone','createtime')
list_filter = ('activecode','user_phone','createtime')
search_fields = ('activecode','user_phone','createtime')
#admin.register(App)
class AppAdmin(admin.ModelAdmin):
list_display = ('version','versionurl','bonprice','rahnama')
list_filter = ('version','versionurl','bonprice','rahnama')
search_fields = ('version','versionurl','bonprice','rahnama')
#admin.register(Bid)
class BidAdmin(admin.ModelAdmin):
list_display = ('user','bidtime','competition','maxbon','bonnumber')
list_filter = ('user','bidtime','competition','maxbon','bonnumber')
search_fields = ('user','bidtime','competition','maxbon','bonnumber')
You should override the admin.py file.
from django.contrib import admin
class MyModelAdmin(admin.ModelAdmin):
class Media:
css = {
'all': ('/templates/css/admin-extra.css ',)
}
admin.site.register(MyModel,MyModelAdmin)
You need to add base_site.html as follows:
templates
admin
base_site.html
with:
{% extends "admin/base.html" %}
{% load static from staticfiles %}
{% load i18n grp_tags %}
{% block title %}{{ title }} | {% get_site_title %}
{% endblock %}
{% block extrastyle %}
<link rel="stylesheet" type="text/css" href="{% static "css/admin-extra.css" %}" />
{% endblock %}
{% block branding %}
{# Use the div#grp-branding for branding elements, e.g. a logo #}
{# <div id="grp-branding"></div> #}
{% endblock %}
{% block nav-global %}
{% endblock %}
I am not sure if you still have this problem or if the versions make things different, but here is what I thought could solve the problem.
https://docs.djangoproject.com/en/3.0/howto/static-files/
According to the documentation above, .css as a static file is placed in different directory as .htmls. If you have the same configuration as official example, you can create a directory called "static" under your app folder, and put your css files there.
{% static %} points to that folder. So if you write {% static "css/admin-extra.css" %} in your html, your css should be in APP_FOLDER/static/css/admin-extra.css.
Hope it works.
Related
how to completely remove the recent action panel from Django admin UI
I have done a lot of searching and all leads to how to clear the data in the panel from the database .. but what am looking to do is completely remove the panel from my admin Userinteraface .. any help on how to do this?
what I've tried :
tried to override the base_site.html for the admin and it worked for override colors and styles and other blocks but not working with the sidebar block
{% extends "admin/base.html" %}
{% load i18n static %}
{% block title %}{{ title }} | {% trans "G-Attend" %}{% endblock %}
{% block extrastyle %}
<link rel="stylesheet" type="text/css" href="{% static 'custom_admin_styles/admin_override.css' %}">
{% endblock %}
{% block branding %}
<h1 id="site-name">
<b style="color: #1f76bc;size: 80;">
G
</b> Attend </h1>
{% endblock %}
{% block sidebar %}
<div id="content-related">
</div>
{% endblock %}
{% block nav-global %}{% endblock %}
The right solution for this was like :
creating a file called index.html inside my templates/admin/ directory , then inside the HTML file I used this code :
{% extends "admin/index.html" %}
{% block sidebar %}
{% endblock %}
so I need to extend index.html instead of base_site.html
To override the Django admin side bar
You nee to extends admin/base_site.html
{% extends "admin/base_site.html" %}
{% load i18n static %}
<!-- -->
{% block sidebar %}
<div id="content-related">
Your custom side bar here.
</div>
{% endblock %}
NB : Inside your templates folder, you should have created an admin subfolder. And place the custom .html file in it.
More info Here
I'm trying to load my own css file to replace base.css in order to customize Django Admin
{% extends "admin/base.html" %}
{% load static %}
{% block title %}VSPM{% endblock %}
{% block branding %}
<h1 id="site-name">{{ site_header|default:_('Django administration') }}</h1>
{% endblock %}
{% block stylesheet %}{% static "admin/css/theme.min.css" %}{% endblock %}
{% block nav-global %}{% endblock %}
But the css file isn't loading in Django admin. What am I doing wrong?
{% block extrastyle %}{% static "admin/css/theme.min.css" %}{% endblock %}
Change your block name and try
Go to your project.
Copy this folder /virualent_path//lib/python3.6/site-packages/django/contrib/admin/static/admin to 'static' folder of your project.
Add this string to the settings.py: STATIC_ROOT = os.path.join(PROJECT_DIR, 'static')
Then run this command: python3.6 manage.py collectstatic
according to the flask-admin docs I can extend the main flask-admin dashboard by creating the file templates/admin/index.html and extending admin/master.html. The HTML would look like this:
{% extends 'admin/master.html' %}
{% block body %}
<h1>HELLO</h1>
{% endblock body %}
But i can't find any information on how to extend the model CRUD pages: List, Edit and Create. I need to extend the Create and Edit User page so i can add js code to the form template.
Is there a template where i can extend just like admin/master.html example?
Just found it in flask-admin docs. I had to create templates/edit_user.html and templates/create_user.html. For list_users is also the same, theres is an example in the docs.
In edit_user.html
{% extends 'admin/model/edit.html' %}
{% block body %}
<h1>User Edit View</h1>
{{ super() }}
{% endblock %}
In create_user.html
{% extends 'admin/model/create.html' %}
{% block body %}
<h1>Create View</h1>
{{ super() }}
{% endblock %}
and then add this to the User model View:
class UserView(ModelView):
edit_template = 'edit_user.html'
create_template = 'create_user.html'
admin.add_view(UserView(User, db.session))
As for DOC, this is the default command:
admin = Admin(app, name='microblog', template_mode='bootstrap3')
Add your own CSS here /static/css/main.css:
{% block head_css %}
{{ super() }}
<link rel="stylesheet" href="{{ url_for('static', filename='css/main.css', _external=True) }}" ></link>
{% endblock %}
In the static folder of my DjangoServer is located a template of the default webpage. It's decorated with some template blocks.
If I load this template file, the path to the template is shown in the browser, it looks like, that the code is not loaded.
If I store the template in an app/template folder and I extend this file. It works very well. I use the tutorial of Django but it still not working.
Settings.py
django.contrib.staticfiles is added to INSTALLED_APPS
STATIC_URL = '/static/'
STATICFILES_DIRS = (
os.path.join(BASE_DIR, 'static'),
)
STATIC_ROOT = os.path.join(BASE_DIR, 'staticfiles')
projectRootFolder/static/html/basePage.html
{% load static from staticfiles %}
<!DOCTYPE html>
<html lang="en">
<head>
{% block head %}
<title>{% block title %}My amazing site{% endblock %}</title>
{% endblock %}
</head>
<body>
{% block body %}
<header>
{% block header %}
<header> -- HEADER BANNER --</header>
{% block menu %}<nav></nav>{% endblock %}
{% endblock %}
</header>
<section>
{% block section %} SECTION {% endblock %}
</section>
{% block footer %}
<footer> -- FOOTER --</footer>
{% endblock %}
{% endblock %}
</body>
<script type="text/javascript" src="{% static 'angularjs/SOME_ANGULAR_FILES_LOADED.js' %}"></script>
</html>
app/template/app/index.html from an app
{% load static from staticfiles %}
{% static "html/basePage.html" %}
{% block menu %}<nav>App A</nav>{% endblock %}
{% block section %} Lorem Ipsum{% endblock %}
app/views.py
from django.template import loader
from django.shortcuts import render
from django.http import HttpResponse
# PAGE CALLS
def index(request):
template = loader.get_template('mainControll/index.html')
context = {}
return HttpResponse(template.render(context, request))
Output
What is the mistake i've made? How can I load this template correctly?
Warning, this answer assumes your goal for asking the question is to get things to work, instead of helping you troubleshoot a probable permission problem just so that you can run into another problem. That is, I'm assuming your end goal is not a html document inside a non-html document.
What can you do to get your output to work:
It looks like you want to include a template into another template, you can do that with {% extends "basePage.html" %}. Your template does then need to be at a location where templates are found, not where static pages are found.
E.g. If your app is called 'myapp' then under myapp/templates/ is a one possible place, assuming the TEMPLATES setting has APPDIRS = True
This would mean changing index.html to be
{% extends "basePage.html" %}
{% block menu %}<nav>App A</nav>{% endblock %}
{% block section %} Lorem Ipsum{% endblock %}
See https://tutorial.djangogirls.org/en/template_extending/ for example of this and https://docs.djangoproject.com/en/1.11/topics/templates/#configuration for configuring things to that your basePage.html can be found
I am new to Django and I am practicing template inheritance. I am currently having trouble inheriting templates on the 3rd level. The base level is a template that my whole site uses (ex: navbars). The second level is the content of my site. However this content is a bit lengthy so I took a portion(contactform.html) of it and created its own HTML file for that portion.
I am able to get my home.html into my index.html like so
{% load staticfiles %}
<!DOCTYPE html>
<html>
<head lang="en">
<link href="{% static "css/boothie.css" %}" rel="stylesheet" type="text/css">
<script src="{% static "js/boothie.js" %}"></script>
<script src="{% static "js/jquery.easing.1.3.js" %}"></script>
<title>Boothie</title>
</head>
<body>
{% block content %}{% endblock %}
</body>
</html>
Within my home.html I want to include my contactform.html. This is what I have so far.
{% extends "index/index.html" %}
{% load staticfiles %}
{% block content %}
...
...
...stuff...
<!-- contact -->
{% block contactform %}{% endblock %}
{% endblock %}
My contactform.html:
{% extends "home/home.html" %}
{% load staticfiles %}
{% block contactform %}
<section id="contact">
<!-- HTML! -->
</section>
{% endblock %}
This is what is currently in my home/views.py:
from django.shortcuts import render
from django.views import generic
class HomeView(generic.TemplateView):
template_name = "home/home.html"
my home/urls.py:
from django.conf.urls import patterns, url
from home.views import HomeView
urlpatterns = patterns('',
url(r'^$', HomeView.as_view(), name="home"),
)
TEMPLATE_DIRS:
TEMPLATE_DIRS = (
os.path.join(BASE_DIR, 'templates'),
os.path.join(BASE_DIR, 'home'),
)
here is a picture of my project structure:
Instead of inheriting, just include the contactform template in the home template. In home/home.html put:
<section id="contact">
{% include 'home/contactform.html' %}
</section>
You dont need to extend, instead include
Example, keep the contents of the contact_form.html with just the required html content (without the extends, and the block tag, etc..), and then include the html snippet. Now, django would do the magic for you - The included snippet would have all the context variables too.
{% extends ".." %}
{% load staticfiles %}
{% block content %}
...
...
...stuff...
{% include /path/to/contactform.html %}
{% endblock %}