as the title said i am having a problem using a template in an admin view
here is my worktree
project
|-- project/
|-- myapp/
|-- templates/
|-- admin/
|-- file.html
settings.py
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [os.path.join(BASE_DIR, 'myapp/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',
],
},
},
]
ModelAdmin.py
class ModelAdmin(admin.ModelAdmin):
actions = ['export']
def export(self,request, queryset):
paragraphs = ['first paragraph', 'second paragraph', 'third paragraph']
pdfkit.from_file('file.html', 'out.pdf', paragraphs)
admin.site.register(Model, ModelAdmin)
but i am getting "No such file: file.html" error
pdfkit does not know about your TEMPLATES setting. You need to provide the path relative to your project directory.
pdfkit.from_file('myapp/templates/admin/file.html', 'out.pdf', paragraphs)
However this will treat the file.html as an html file. If it is a Django template that you want to render, you could use render_to_string and from_string.
from django.template.loader import render_to_string
html = render_to_string(request, 'admin/file.html', {...})
pdfkit.from_string(html, 'out.pdf')
Related
I'm new to Django and I want to use my custom login page instead of the ugly looking django login template. How could I make it work? I think I need to edit the urls.py file and override the login template but I don't know how to do it. Thanks
urls.py
from django.urls import path
from django.contrib.auth.views import (
LoginView,
)
urlpatterns = [
path('login/', LoginView.as_view(template_name='login/login.html'), name='login'),
]
settings.py
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',
],
},
},
]
LOGIN_URL = '/login/'
Now make a templates folder in the Base directory( where your manage.py is present) and in that folder make a new folder named login. Inside login folder make login.html
So the directory structure will be like this
-my_project
-templates
-login
-login.html
-manage.py
Now do whatever customization you want to do in login.html. You will get form object in context of the template, which is nothing but your login form.
I'm using django-invitations to manage user invitation.
I can't find where I should put the template files email_invite_message.txt and email_invite_subject.txt
Here they talk about
Override email_invite_message.txt in django-invitations/invitations/templates/invitations/email/.
You can do this by creating the file in the same directory path in your project.
or
Yeah, if this isn't clear, you can create an .html file in your project at {projectroot}/{app}/templates/invitations/email/email_invite_message.html and it will override the default template.
But the first one didn't work for me and I can't figure out what {app} should be.
You can override any third-party application templates by creating replacement files in your project’s templates directory as indicated in the docs here.
In your settings.py file edit the TEMPLATES var, specifically the 'DIRS' key
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
...
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [BASE_DIR + '/core/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',
],
},
},
]
let's suppose you have an app named core, edit the settings as in the previous example and put the new files in this way (for this particular app):
core/
/templates
/invitations
/email
email_invite_message.html
email_invite_subject.html
__init__.py
admin.py
apps.py
models.py
tests.py
I'm using django 3.0.
I use django-admin startproject mysite and created a sample django project.
I don't think the sample project has any models so I commented out "django.contrib.contenttypes" in INSTALLED_APPS in settings.py. I also commented out all middlewares.
I then wrote a simple view
from django.shortcuts import render
def index(request):
return render(request, 'hello.html')
and hello.html is just a blank file.
Once I access the page, django throws exception
Model class django.contrib.contenttypes.models.ContentType doesn't
declare an explicit app_label and isn't in an application in
INSTALLED_APPS.
Can anyone help explain the exception? The sample project doesn't have any model, why do I need django.contrib.contenttypes? Can django websites live without django.contrib.contenttypes?
I have to remove django.contrib.auth.context_processors.auth in TEMPLATES.
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [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',
],
},
},
]
I am creating a website using Django 2.0.7. I need to use a base template that is outside the purview of all applications, but can be inherited and used by templates in my other applications in the project.
I have edited my settings.py file appropriately (as per the documentation), but still when I go the root (i.e. home) page of my site, I get a blank page - can anyone explain why?
I have the following directory structure
myproj
----manage.py
----app1
----templates/
base.html
index.html
----static/
----myproj
__initi__.py
wsgi.py
settings.py
urls.py
views.py # <- I added this
The relevant parts of my settings.ini file looks like this:
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
SITE_ROOT = os.path.dirname(os.path.realpath(__file__))
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': ['{0}/templates/'.format(BASE_DIR),],
'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',
],
},
},
]
views.py:
from django.http import HttpResponse
def index(request):
return HttpResponse()
Why can't django find my index.html ?
Use 'DIRS': [os.path.join(BASE_DIR, "templates")] in your settings, and your view should return a object which knows what template to render, like this one:
from django.shortcuts import render
def index(request):
return render(request, 'index.html')
There's also TemplateResponse(), SimpleTemplateResponse() and other objects you can return, depending on your needs.
I don't get django 1.11 to accept a new base_site.html:
Following docs and some posts i copied the file to:
BASE_DIR/templates/base_site.html and
BASE_DIR/templates/admin/base_site.html
in settings.py:
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [os.path.join(BASE_DIR, 'templates')],
'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',
],
'loaders': (
'django.template.loaders.filesystem.Loader',
'django.template.loaders.app_directories.Loader',
),
},
},
]
I tried that with the file in templates and in templates/admin:
>>> print ( os.path.isfile((os.path.join(settings.BASE_DIR, 'templates', 'admin','base_site.html'))) )
True
>>> print ( os.path.isfile((os.path.join(settings.BASE_DIR, 'templates', 'base_site.html'))) )
True
If you want to override admin files they must exist in admin folder inside templates folder like below
your_project
your_project
|-- your_project/
|-- myapp/
|-- templates/
|-- admin/
|-- base_site.html
here file name base_site.html must be same as the file name in original admin folder.
base_site.html
{% extends "admin/base.html" %}
# you can write changes here
Just had this problem and literally fixed it by installing django-apptemplates and adding it as a loader into settings.py:
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [PROJECT_DIR],
'APP_DIRS': False,
'OPTIONS': {
'loaders': [
# ADD LOADER HERE
'apptemplates.Loader',
'django.template.loaders.filesystem.Loader',
'django.template.loaders.app_directories.Loader',
],
'context_processors': [
'django.template.context_processors.debug',
'django.template.context_processors.request',
'django.contrib.auth.context_processors.auth',
'django.contrib.messages.context_processors.messages',
],
},
},
]
**APP_DIRS will have to be set to False in order for this to work
To override the base_site.html of Django admin, add the custom base_site.html such that it's path is BASE_DIR/templates/admin/base_site.html. This works in Django 1.11 as well.