Accessing custom directory under static directory in Django - django

I am learning Django.
I know how to add use {% static "abc/xyz" %} in templates.
But now I want to use something similar in views.py file.
For example project->my_app->static->resources->abc.json is the file I need to access in the views.py of my_app.
When I tried f = open('resources/abc.json') it didn't work.
How do I do it correctly?

Related

Django: how to display images from local resource in Chrome other than {% static %} tag

In case I'm:
at a very early stage of developing;
using Chrome, which doesn't allow images from local resource, and
storing my images in a local /static folder,
what could be the best workaround for displaying images, other than through the {% static %} tag?
I have been using {% load static %}...{% static variable/path %} in my templates, but now I'm trying to display images in the admin page, which requires a good deal of overriding, and admin templates aren't easy to find where exactly to override.
Lately I tried format_html, as in:
from django.contrib import admin
(...)
#admin.display(description='Foto')
def show_foto(self):
return format_html(
'<img src=https://media.(...)>'
)
Which works fine as long as the image resource isn't local.
Also, Django tags don't seem to work inside format_html().

Can't use django variable in include tag

I'm trying to include a .html using
{% include "paintings/experiments/points/{{substance.name}}.html" %}
This however leads to the error TemplateDoesNotExist.
If I hardcode the name of the .html file, it does work.
{% include "paintings/experiments/points/fabric.html" %}
And, in fact, I can use {{substance.name}} inside the included html, were it does indeed get substituted for fabric. Why can I not use a django variable when using an include tag?
I doit with the add templateTag.
{% include "paintings/experiments/points/"|add:substance.name %}
Notice that substance.name should have .html . I'm using this approach to use dynamic Templates. So in a context_processor I set the variable value and use it normally, like this:
{% include ""|add:paginationTemplatePath with page=page_obj %}
In this case, I change the paginationTemplatePath given certain conditions on the context_processor.
I'm exposing this example in order to enrich the answer for other cases, as use include with variable page.
include template tag was designed to accept either string or variable. If you try to use the above, it's just going to be string. But you can manipulate strings with template filters and tags.
You can create custom template tag that creates variable and then use that newly created variable in the include tag. If you check the documentation on Custom template tags and filter you'll see how they work and what are the requirements for them to work.
First you must create a directory inside your app named templatetags
myapp/
__init__.py
models.py
templatetags/
__init__.py
custom_tags.py
views.py
Below is a possible code to create variable for template path:
from django import template
register = template.Library()
#register.simple_tag
def build_template_path(base, name):
return base.format(name)
base in your case would be "paintings/experiments/points/{}.html" and name would be source.name.
Now in the template you first have to load these custom tags with:
{% load custom_tags %}
and then you use this template tag in the template:
{% for source in sources %}
{% build_template_path "paintings/experiments/points/{}.html" source.name as template_path %}
{% include template_path %}
{% endfor %}
With build_template_path you create custom variable template_path which you then use in the include tag.
Overall, template tags are very powerful. You can create or do pretty much anything with them, while filters are a bit more limited, but you could have done that with filters as well. Maybe something like this:
#register.filter
def replace_value(value, name):
return value.replace('**', name)
{% include "paintings/experiments/points/**.html"|replace_value:source.name %}

How to put multiple models on the same page?

I'm doing a website in html and base (where all pages extend) I want
to put a session of social network icons. As this session is base on
html it should be displayed on all pages of the website.
I do not want
to put this session in a static html, I want to do in django using
models. This is already done.
Question: Do I have to put the session of social network icons on each view, or can I make a separate view and all others extend this view?
How can I do this?
Try using an inclusion tag. You can create a function for doing all of the work to create the sessions and then associate that with a particular block of HTML.
templatetags/session.py
#register.inclusion_tag('includes/session_box.html')
def output_session_box(...):
...
return { .. }
The associated template file, includes/session_box.html, would have the HTML like any template.
And then your base.html would have:
{% load session %}
{% output_session_box ... %}
Use RequestContext and a context_processor to inject template variables into every view using RequestContext.
It's as simple as a python function accepting request as an arg, and returning a dictionary to be passed into your template.
https://docs.djangoproject.com/en/dev/ref/templates/api/#django.template.RequestContext
def my_processor(request):
return {'foo': 'bar'}
TEMPLATE_CONTEXT_PROCESSORS = (
# add path to your context processor here.
)
I generally have a per-project processor for the basics... It's exactly how django adds {{ user }} or {{ STATIC_URL }} to every template.

overriding default templates of django-allauth

I used this social registration/signup library django allauth for a project of mine. How do i customize the default templates and forms to give a better look and feel?
Assuming you have set a project level templates directory using the TEMPLATE_DIRS setting like:
TEMPLATE_DIRS = [
os.path.join(PROJECT_DIR, 'templates'),
]
You should be able to copy all of the folders shown here into that directory and edit them as you need. Most of the templates seem to be filling a {% block content %} block, so it's probably easiest if your site_base.html template defines that block somewhere.
If you haven't set TEMPLATE_DIRS, you can do the same thing, but copy the template folders into the templates directory of one of your apps. I prefer to set TEMPLATE_DIRS and keep the main site templates like base.html there, since they don't really belong to a particular app, but that's really just a preference; the template loader should find them either way.
In your views:
from allauth.account.views import SignupView, LoginView
class MySignupView(SignupView):
template_name = 'my_signup.html'
class MyLoginView(LoginView):
template_name = 'my_login.html'
Do pay attention to the examples and docs for structuring your own templates.
Watch this piece in the example templates though:
<form id="signup_form" method="post" action="{% url 'account_signup' %}">
I had to remove the URL link to make it work properly in my own template:
<form id="signup_form" method="post" action="">'
the latest version of all-auth on github has its templates outside, however the one on Pypi is not, all you need to do is clone the repo in your project directory and override the templates. As simple as that.
Use the same logic as overriding admin templates.
Have a look at the example application; it has a templates folder that indicates the layout of the necessary templates
All of these are good suggestions.
Most of these ought to work.
The only thing that worked for me, though, was to include my own templates in "project/app/templates/account/" and make sure that "django-allauth" gets listed in INSTALLED_APPS after my own app.
I discovered this here.
To customize django-allauth after installing it, copy it from site-packages and paste it in your project apps directory. In this way the default allauth app and its templates being used will be those in your project's allauth app. Then if you want to modify signup.html of socialaccount then go to apps\allauth\templates\socialaccount\signup.html and modify it by editting inside 'block content' tag:
{% block content %}
// your customized html for signup form
{% endblock %}
Hope this will help you.

Add links to navigation based on INSTALLED_APPS

I have a project that has a number of apps. These apps translate into modules that perform different functions for the end user.
Now each deployment of the project may have certain apps enabled or disabled.
What I'd like to achieve is a navigation list of links that only displays links for apps that are in INSTALLED_APPS.
For example deployment 1 has App1 and App2 listed in INSTALLED_APPS so would have a navigation something like this:
Link to App1's view
Link to App2's view
And deployment 2 has App2 and App3 installed so should show a navigation something like this:
Link to App2's view
Link to App3's view
Without having a navigation defined in a base template and editing it for each deployment, I can't see a way of doing this. Even by using { block.super }, this seems to not allow 2 apps to be installed as each child template would append to the parent.
This is strictly from the top of my head, so it may not be the "best" or most appropriate way.
First, if you're going to rely on INSTALLED_APPS you should actually implement something along the lines of:
MY_INSTALLED_APPS = (
'app1',
'app2',
)
INSTALLED_APPS = (
# other installed apps
) + MY_INSTALLED_APPS
Then, instead of having to figure out which of the items in INSTALLED_APPS are yours and which are third-party, you just use MY_INSTALLED_APPS instead for things like building your menus.
Second, in Django, apps aren't tied to views in any meaningful way. There's no concept of a "default" view, and you can't simply link to an app. However, you can some what achieve this idea through the use of namespaces and a convention for view naming, specifically, all your apps will at least have a urlpattern named "index", for example.
Then, in your urls.py you create urlpatterns like:
url(r'^app1/', include('app1.urls', namespace='app1', app_name='app1'),
url(r'^app2/', include('app2.urls', namespace='app2', app_name='app2'),
# etc
In each app, you create a urls.py that has at least one urlpattern:
url(r'^$', some_view, name='index'),
This means that the going to /app1/ in your browser would then load some_view and you can reference this view in your code with a name like: app1:index.
The tricky part is using this in your templates. Django 1.5 will add the ability to use context variables in the {% url %) tag for the view name. As of Django 1.3, you can use this behavior as well via {% load url from future %}. However, even that only gets you part-way to what you need.
In your template, you'll need to loop through the values of MY_INSTALLED_APPS and construct the links. The following should work in Django 1.3-1.4 via {% load url from future %} or Django 1.5:
{% for app in apps %}
{{ app }}
{% endfor %}
The other way to accomplish this is with a template filter, which is your only option in Django <1.3, and may still be preferable in later versions. Something like:
#register.filter
def default_url_for_app(app):
return reverse(app+':index')
And, in your template:
{% for app in apps %}
{{ app }}
{% endfor %}
You can simple import your settings.py file and iterate over the INSTALLED_APPS list.
from django.conf import settings
for installed_app in settings.INSTALLED_APPS:
But I'm also in doubt if this is a smart decision to implement your application as such.
What you really want here is some kind of app registration system. That is, for each app that's loaded (ie included in INSTALLED_APPS), register its homepage with the list of links.
Rather than doing all this automatically, I would follow the example of the admin application and get each app to call a register function on first load. You could do this in the models.py, for example, since you know that will be imported by Django on startup. The register function would take the app name and the url and build a dictionary which could then be passed to each template via a context processor.