Separate front-end in django - python

I want to divide my app to front-end and back-end in Django project. I have books app inside my project directory, so i placed JS files and index.html to myproject/books/front-end/ dir. Now i just need to render index.html in view, but for that it has to be in myproject/books/templates/. So i tried to use render_to_response('../front-end/index.html') dance, but it just renders 'no file' error.
P.S. Basic idea is to handle all template work by Handlebars and use RESTful API to communicate with server database with Tastypie. Maybe it is bad idea or i'm doing something wrong.

Consider adding the /front-end/ folder in your TEMPLATES['DIRS'][] list in your settings.py. Something like
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [
os.path.join(BASE_DIR, 'books/front-end/'),
],
'APP_DIRS': True,
'OPTIONS': {
'context_processors': [
#the usual stuff
],
},
},
]
In my project with Django 1.8 I have something similar.
In views.py:
def desktop_view(request):
return render(request, 'index.html', {})

Related

render_to_string gives error whereas render works

I am trying to render a response back to an Ajaz request. The view is reached in the python code, but if I try and render the response to string using render_to_string then I get an error stating:
django.template.exceptions.TemplateDoesNotExist:
If I run the render method with the same parameters then I don't get an error. The code for each looks like:
html = render_to_string(request, 'planner/viewconnections.html', { 'routes' : routelist })
render(request, 'planner/viewconnections.html', { 'routes' : routelist })
Obviously the first one is what I want to run so I can obtain the raw html back to the AJAX success function.
My templates configuration in settings.py looks like:
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',
],
},
},
]
I have tried adding the template directory as below, but I still get the same error.
ROJECT_ROOT = os.path.abspath(os.path.join(os.path.dirname((__file__)),".."))
# Other settings...
TEMPLATE_DIRS = (
os.path.join(PROJECT_ROOT, "planner/templates"),
)
Can someone help? My search on this problem let me to add the TEMPLATE_DIRS in settings, but that didn't work. I don't understand why one render method can pick up the template, but the other doesn't.
in django render_to_string request paramter is not required
syntax
render_to_string(template_name, context=None, request=None, using=None)
html = render_to_string('planner/viewconnections.html', { 'routes' : routelist })
refer this https://docs.djangoproject.com/en/2.1/topics/templates/#django.template.loader.render_to_string

Can't load a template in Django - 'TemplateDoesNotExist at /home/' error

I'm learning Django from 'Mastering Django: Core' book and now I'm
stucked in this third chapter of the book which describes about the Django template.
The problem I'm facing is I can't load a template from a specific directory because it gives me this "TemplateDoesNotExist at /home/" error.
Here is my project directory :
mywebsite/
mywebapp/
...
...
views.py
...
temp/
template.html
Here is TEMPLATES list from settings.py :
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': ['/home/temp'],
'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',
],
},
},
]
And finally here's my view :
from django.template.loader import get_template
def home(request):
t = get_template("template.html")
c = Context({'heading':'Welcome to MyWebsite.',
'name':'Arya Stark','age':19})
return HttpResponse(t.render(c))
Note: The template I'm using in this view is in the temp directory.
So, can you please explain me why would that error happen?
You've set DIRS to "/home/temp", but as you've clearly shown in your directory structure, your templates are in "mywebsite/temp". You'll probably need to use the full path there, or at least os.path.join(BASE_DIR, 'temp').
First of all, check if in your settings.py BASE_DIR is declared, if it is,
then check,
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
Also, in your DIRS in TEMPLATES,
'DIRS' : [BASE_DIR + '/temp/',]
This should do the trick,
Also, django as default checks for the sub-directory templates in your app-directory and project-directory as well, when you APP_DIRS = True.

How can I have a template folder outside my Django project

I'm working on an Open Source Django app and created some design for it. Now a customer wants to use it's own, copyrighted, design. After reading the Django docs I created a separate, private, GIT repository that I want to use for the new design. I got it almost working by adding 2 items to the settings; an extra entry to look for templates in a folder "funder_custom_templates" and an extra entry to look for static files in the same location. This is how I configured TEMPLATES and STATICFILES_DIR:
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [
os.path.join(PROJECT_DIR, '..', '..', 'funder_custom_templates'),
os.path.join(PROJECT_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',
'fundraiser.context_processors.cart',
],
},
},
]
STATICFILES_DIRS = [
os.path.join(PROJECT_DIR, '..', '..', 'funder_custom_templates','static'),
os.path.join(PROJECT_DIR, 'static'),
]
This works for overriding the base design located in the PROJECT_DIR/templates/base.html, when I create funder_customer_templates/base.html and for all the static files as expected. But I also want to override app specific template files like blog/templates/blog/blog_index_page.html
I tried to put these files in the root of funder_custom_templates and I tried to mimic the app folders structure in funder_custom_templates but that doesn't load the app specific templates. Is there a way to solve this?
Example project files, with the folder structure, located at: https://github.com/acidjunk/funder/tree/develop/
Since you are using the app_directories.Loader class to load templates (specified by setting 'APP_DIRS': True,), then for app specfic templates Django will iterate over your INSTALLED_APPS setting looking for a specific template file. The important thing here is that it does so in order.
So if you want to override blog/templates/blog/blog_index_page.html then you will need to have a custom_blog/templates/blog/blog_index_page.html inside an application that comes before blog.
I recommend wrapping up all custom static resources in their own django application and python package. This way you can simply install the package from its private repo and add it to the list of installed apps to override any static content and templates.
See Django's docs for more details on template loading: https://docs.djangoproject.com/en/1.9/ref/templates/api/#django.template.loaders.app_directories.Loader

Django Admin Panel

I am trying to set up a new blog. I want to keep all my project templates folder in the same folder as where my settings.py is. To do this I did the following...
[...]
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [os.path.join(BASE_DIR, "templates/")],
'APP_DIRS': False,
'OPTIONS': {
'context_processors': [
[...]
But now, my admin panel layout doesn't work. How can I circumvent the above solution when using the admin panel? I get the error
Exception Type: TemplateDoesNotExist
Exception Value: admin/login.html
By disabling APP_DIRS you're forcing Django to look for your templates in the templates folder of your basedir regardless of where the app specifies them. This will break any plugins and also prevents you from namespacing templates. Its generally a bad idea.
DIRS is a list, so you can specify multiple locations if desperately want to hold the templates in a different locationand maintain access to teh admin

Errno22 while setting up django (trying to use get_template)

I am using windows, new to Django, trying to learn it.
Right now I am just trying to set up a template, views, url - and load the template in my browser. Should be simple.
I have a project folder. An app folder. A templates folder. My templates folder contains page.html which is an html file that just says "hi".
my urls is properly configured to point to my views
I have 2 views.
def frontpage(request):
fp = open('C:/EclipseWorkspace64/Project/templates/page.html')
page = Template(fp.read())
return HttpResponse(page)
def otherpage(request):
page = get_template('page.html')
return HttpResponse(page)
The first view loads my template fine when I go to the URL.
The second view does not. It gives me: [Errno 22] Invalid argument: 'C:\EclipseWorkspace64\Project\:\page.html'
This is templates in my settings:
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': 'C:/EclipseWorkspace64/Project/templates/',
'APP_DIRS': False,
'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 don't understand why I get this error. What am I doing wrong? Why does the first view load but the second one doesn't? I have followed instructions in the django book on how to set up my settings file to a tee.

Categories

Resources