I have generated a django-admin for my app and I can access the dashboard. But it contains a logo that says "django admin". I want to change it to my own custom logo. How can I do that?
I have tried adding a base.html file to admin directory and tried to override but for some reason it's not working. It's code is as follows:
{% extends "admin/base.html" %}
{% load theming_tags %}
{% load staticfiles %}
{% block blockbots %}
{{ block.super }}
{# Using blockbots ensures theming css comes after any form media and other css #}
{% render_theming_css %}
<style type="text/css">
#header #branding h1 {
background-image: url("bootstrap_admin/img/logo-140x60.png");
}
</style>
{% endblock %}
{% block branding %}
<a href="{% url 'admin:index' %}" class="django-admin-logo">
<!-- Django Administration -->
<img src="{% static "bootstrap_admin/img/logo-140x60.png" %}" alt="{{ site_header|default:_('Django Admin') }}">
</a>
{% endblock branding %}
I want to change the logo in the top-left corner. How can I achieve what I'm trying to?
your question is answered here
"{% static "bootstrap_admin/img/logo-140x60.png" %}"
this comes from here
django-admin-bootstrap/bootstrap_admin/static/bootstrap_admin/img/logo-140x60.png
after replacing you need to run command python manage.py collectstaticthen this will work
The official way to achieve that is:
You need to override the default templates provided by Django. In your Django settings, your code:: TEMPLATES setting looks like this.
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',
],
},
},
]
This means that Django will look for templates in a directory called templates inside each app, but you can override that by setting a value for TEMPLATES.DIRS.
We change the 'DIRS': [], to 'DIRS': [os.path.join(BASE_DIR, 'templates/')], and create the templates folder. If your STATICFILES_DIRS is empty set it to:
STATICFILES_DIRS = [
os.path.join(BASE_DIR, "static"),
]
Now create a file called base_site.html from the admin app to templates\admin folder you just created. Add the code in it:
{% extends "admin/base.html" %}
{% load staticfiles %}
{% block title %}{{ title }} | {{ site_title|default:_('Django site admin') }}{%
endblock %}
{% block branding %}
<h1 id="site-name">
<a href="{% url 'admin:index' %}">
<img src="{% static 'Your image.png' %}" height="40px" />
</a>
</h1>
{% endblock %}
{% block nav-global %}{% endblock %}
Related
I just want to display profile picture.Img upload successfully but not showing the img.But if i try in admin panel then show me img. Here is my index.html code
{% extends 'Login_app/base.html' %}
{% block body_block %}
{% if user.is_authenticated %}
<p>Hi {{ user_basic_info.username }},</p>
<p>Your Email {{ user_basic_info.email }}</p>
<p> Facebook Profile </p>
<img src="/media/{{ user_more_info.profile_pic }}" width="200px">
{% else %}
<div class="alert alert-primary"></div>You are not logged in!!</div>
{% endif %}
{% endblock %}
Here is my setting.py media setting
import os
MEDIA_ROOT = os.path.join(BASE_DIR, 'media') # This is a tuple
MEDIA_URL = '/media/'
Here the ss of website
You have to add .url after the profile picture.
Try This:
<img src="/media/{{ user_more_info.profile_pic.url }}" width="200px">
OR
In Models.py define a get_absolute_url() method to join the media directory:
def get_absolute_image(self):
return os.path.join('/media', self.image.name)
And in templates do this:
{% for post in posts %}
<img src="{{ post.get_absolute_image }}" width="200px">
{% endfor %}
p.s.- here post is model name
django setting
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR, 'staticfiles')
STATICFILES_DIRS = (
os.path.join(BASE_DIR, "static"),
)
MEDIA_URL = '/media/'
MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
django views
def single_article(request, page, slug):
article = get_object_or_404(Article, slug=slug)
return render(request, "article/single_article.html", locals())
django template
<`enter code here`img class="d-block w-100 img-responsive" style="height: 100%" src="{{ article.picture.url }}" alt="{{ article.title }}"
> Blockquote
you don't have to pass /media/ or use .url to fetch it from db
{% extends 'Login_app/base.html' %}
{% block body_block %}
{% if user.is_authenticated %}
<p>Hi {{ user_basic_info.username }},</p>
<p>Your Email {{ user_basic_info.email }}</p>
<p> Facebook Profile </p>
<img src="{{ user_more_info.profile_pic.url }}" width="200px">
{% else %}
<div class="alert alert-primary"></div>You are not logged in!!</div>
{% endif %}
{% endblock %}
this will work if still image is not loadin add you views
Try to add this code to the project's urls.py:
urlpatterns = [
...
] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
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.
views.py
def search_author(request):
if 'q' in request.GET and request.GET['q']:
q = request.GET['q']
authors =User.objects.filter(username=q)
return render_to_response('search_results.html',
{'authors': author, 'query': q})
else:
return HttpResponse('Please submit a search term.')
def search_form(request):
return render_to_response('search_form.html')
urls.py
url(r'^search_author$',app.views.search_author),
url(r'^search_form$', app.views.search_form)
search_form.html
{% extends "app/layout.html" %}
{% block content %}
<form action="/search_author/" method="get">
<input type="text" name="q">
<input type="submit" value="Search">
</form>
search_results.html
{% extends "app/layout.html" %}
{% block content %}
<p>You searched for: <strong>{{ query }}</strong></p>
{% if authors%}
<p>Found {{ authors|length }} author{{ authors|pluralize }}.</p>
<ul>
{% for author in authors %}
<li>{{ author.username }}</li>
{% endfor %}
</ul>
{% else %}
<p>No authors matched your search criteria.</p>
{% endif %}
loginparitial.html
{% if user.is_authenticated %}
<form id="logoutForm" action="/logout" method="post" class="navbar-right">
{% csrf_token %}
<ul class="nav navbar-nav navbar-right">
<li><span class="navbar-brand">您好 {{ user.username }}!</span></li>
<li>查询作者</li>
<li> 发布旧书</li>
<li><a href="user_book_detail" >查看我的书籍</a></li>
<li> 登出</li>
</ul>
when l click the '查询作者' in the home page,the error says
Page not found (404)
Request Method: GET
Request URL: http://127.0.0.1:8000/search_form/
Can anyone help me with my problem ? Thanks in advance
It's because of your regex pattern.
/search_form/ is different from your specified ^search_form$. That maps to /search_form (No trailing slash). To make it /search_form/, just use ^search_form/$.
I think you giving wrong routing to templates.
In settings.py you giving templates directory route like this
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [os.path.join(BASE_DIR, 'templates')],
'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',
],
},
},
]
then your templates loaded from main app folder --> templates folder.
You can call templates directly in views.
If you save your templates in another folder in templates folder,then you need to give the template route upto that folder.
For example:
you saved your templates in main app folder -->templates --> app folder.
then you have to give the route upto to app folder.
like this:
def search_form(request):
return render_to_response('app/search_form.html').
Try once..!
I'm planning to add change language dropdown in my admin page.
according to this code and How to extend admin page.
I copy base_site.html and copy it to myapp/templates/admin, the i create a html file named change_language.html and write this code in it:
{% load i18n %}
/ {% trans 'Change language' %}
<form action="/i18n/setlang/" method="post" style="display: inline;">
<div style="display: inline;">
<select name="language" onchange="javascript:form.submit()">
{% for lang in LANGUAGES %}
<option value="{{ lang.0 }}"{% ifequal LANGUAGE_CODE lang.0 %} selected="selected"{% endifequal %}>{{ lang.1 }}</option>
{% endfor %}
</select>
</div>
</form>
I add {% extends 'admin/base_site.html' %} at the top of this file, noting happens.
I add {% extends 'admin/base.html' %} , again noting happens.
All hints and answers says that we should change something name <div id="user-tools"> at line 25 of base.html, But in Django 1.10 it goes to line 31 with a different staff. im kinnda lost because i read many different staff every where and non of them works for me. Dose any know where im doing wrong ?
here is my middlewares :
MIDDLEWARE = [
'django.middleware.security.SecurityMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.locale.LocaleMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
]
And template settings :
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [
os.path.join(BASE_DIR,'templates'),
],
'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',
],
},
},
]
In your templates/admin folder, make sure the file is named base_site.html (otherwise the default base_site will not be overwritten).
Make sure to copy the latest version of the file from the django-repo.
Most important: verify you extend the admins base.html (your base_site.html should start with {% extends "admin/base.html" %})
You may now add your form (for example to the footer):
{% extends "admin/base.html" %}
{% load i18n %}
{% block title %}{{ title }} | {{ site_title|default:_('Django site admin') }}{% endblock %}
{% block branding %}
<h1 id="site-name">{{ site_header|default:_('Django administration') }}</h1>
{% endblock %}
{% block nav-global %}{% endblock %}
{% block footer %}
<div id="footer">
<form action="/i18n/setlang/" method="post">
{% csrf_token %}
<select name="language">
{% for lang in LANGUAGES %}
<option value="{{ lang.0 }}"
{% ifequal lang.0 request.LANGUAGE_CODE %}
selected="yes"
{% endifequal %}
>{{ lang.1 }}</option>
{% endfor %}
</select>
<input type="submit" value="{% trans 'Change language' %}" />
</form>
</div>
{% endblock %}
Refer to the base.html to find a block that seems suitable for your form.
You will also need to add the i18n urls to your url-settings
url(r'^i18n/', include('django.conf.urls.i18n')),
And if you really want to submit your form using javascript you will also need to get and submit the csrf token for the form. The django docs cover this topic quite comprehensive.
In addition to Kim's answer, you should add i18n context processor in your django settings django.template.context_processors.i18n to be able to access LANGUAGES variable in the template.
I guess you are mixing both answers that you've found on the internet. One of them copies and changes a couple of files from the admin template, effectively overriding them in your program's references. The second one extends admin's templates. You should completely implement just one of them.
app.views.py
from django.shortcuts import render
from django.views.generic import View
class RegisterView(View):
def get(self,request):
return render(request, 'register.html' , { 'title' : 'Register Page'} );
register.html
{% extends 'base.html' }
{% block head}
{{title}}
{% endblock}
{% block content}
Register Page
{% endblock }
base.html
Arena Application
{% block head}
Main Page
{% endblock }
</title>
</head>
<body>
<h2> Welcome to arena app </h2>
{% block content }
<p>
this is base page
</p>
{% endblock }
</body>
</html>
**Problem : ** When i visit http://127.0.0.1:8000/account/register , this is what i get :
http://i.stack.imgur.com/Y9Lod.png
My file Structure :
http://i.stack.imgur.com/1KvDP.png
And finally my TEMPLATE_DIRS var in settings.py looks like:
import os
BASE_DIR = os.path.dirname(os.path.dirname(__file__))
TEMPLATE_DIRS = (
os.path.join(BASE_DIR, 'templates'),
)
Django version : 1.7.3
The code is not escaped properly. Should start and end with {% and %}
Example:
{% endblock %}
not
{% endblock}