Disable caching in custom views in Flask Admin - python

I want to disable cahce in Flask-Admin panel, where i displaying some images. For exmaple, i have 1 image in my DB (actually, just uri to an image). If i delete this image, and then upload a new one, the cache will show me deleted image.
To escape this moment, i need disable caching. But, also, i want disable it on all Flask-Admin pages.
{% extends 'admin/master.html' %}
{% block head_meta %}
{{super()}}
<meta http-equiv='cache-control' content='no-cache'>
<meta http-equiv='expires' content='0'>
<meta http-equiv='pragma' content='no-cache'>
{% endblock head_meta %}
I know if i save code above as index.html, it applies only to index admin page panel.
But, as i also said above, i want to disable caching on all pages.

Found some information about it. If you want to edit your CUSTOM view, you should add these variables:
list_template = 'list.html'
create_template = 'create.html'
edit_template = 'edit.html'
Where list.html responsible for list with all your records and so on.
Also, you should create template, where you put all your code to append to existing Flask-Admin. In my case, it looks like this:
{% extends 'admin/model/list.html' %}
{% block head_meta %}
<meta http-equiv="Cache-Control" content="no-cache, no-store, must-revalidate" />
<meta http-equiv="Pragma" content="no-cache" />
<meta http-equiv="Expires" content="0" />
{{ super() }}
{% endblock head_meta %}
above is example of disabling cache on page where all records are show

Related

Django appends white space to views

So I have recently started looking into the Django framework, but it appends some white space in the top of my views even though the layout.html and layout.css is the same for each view.
layout.html
<html>
<head>
<meta charset="utf-8" />
{% load staticfiles %}
<link rel="stylesheet" type="text/css" href="{% static 'layout.css' %}" />
<link href='https://fonts.googleapis.com/css?family=Inconsolata' rel='stylesheet' />
</head>
<body>
<div class="content">
<div class="navbar">
Homepage.
Home
Projects
About
</div>
<div class="text">
{% block stuff %}
{% endblock %}
</div>
</div>
</body>
</html>
This is my views
def index(request):
return render(
request,
'home/index.html',
{
'greetings': "Welcome to my site!",
}
)
def about(request):
return render(
request,
'home/about.html',
{
'greetings': "Welcome to my site!",
}
)
home/index.html and home/about.html both look like
{% extends "layout.html" %}
{% block stuff %}
{{ greetings }}
{% endblock %}
about page pushed down
As seen in the image the view for the about.html page is push down, and I really cannot figure out why.
After inspecting the elements I found that for the about page, the header is added to the body tag.
inspecting the elements
Anyone who can help me out?
This is most likely because of CSS. To find out where the space is coming from, click on inspect element and choose select an element to inspect it. Then slightly move the mouse around the page until you find that space and click on it. You can then find out where the space is coming from in the Styles tab.
Also make sure there are no <br>tags in your html template
The view is certainly not the culprit here, it's just a python function which takes in a request and returns a response. The HTML looks fine too, check your CSS once again(or post it here) or you can always check the source code on your browser and inspect element the page through the Developer tools to check where the whitespace is coming from.

How to get data that was displayed in template with django template language into different method

How can i access data, after i press button, that was displayed in my template from different method in views.py. Short example of what i am trying to achieve:
TEMPLATE: (test.html)
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
{% for each in my_list %}
<p>each</p>
{% endfor %}
<a class="btn" href="url to cal second_method"></a>
</body>
</html>
Views.py:
def first_method(request, template='test.html'):
context = {
'my_list': ["one","two","tree"]
}
return render(
request,
template,
context
)
def second_method(request):
# i want to work with my_list here after pressing <a>
# i want to be able to export it to excel for example
What is the best way to do this.
I believe you could store your queryset in a session and than retrieve it in another view. See Django: Passing a queryset to another view with HttpResponseRedirect for some hints.
I solved this by cashing the query set. See: Django cache

How to extend to 'base.html' template styles in Django

I work with templates in my application. I have the main part of the website styled in base.html, being the files that always will be the same (header, menu, footer...) properly coded in 'base.html' and also with the styles linked to it (in a link rel="stylesheet").
When I try to use the base template as it is, a base template, it works well while it lets me add content between de {% block content %} and also shows the 'permanent' parts (menu, header etc), but there have no style (CSS) on it. How could I also extend to these stylesheet to load the CSS styles??
Help would be appreciated, thank you.
EDIT 2: Here's my base.html head content:
<head>
<meta charset="utf-8">
{% load staticfiles %}
<title>{% block title %}Index{% endblock %}</title>
{% block style_base %}
<link href="{% static 'css/styles.css' %}" rel="stylesheet">
{% endblock %}
<meta name="description" content="{% block description %}{% endblock %}">
<link href='https://fonts.googleapis.com/css?family=Roboto+Mono:400,700,300' rel='stylesheet' type='text/css'>
<script src="static/myapp/jquery.min.js" type="text/javascript"></script>
<script src="static/myapp/main.js" type="text/javascript"></script>
</head>
This works on base.html, it gets the correct styles. However, when trying to get the same styles in the common part with another template, it doesn't gets the styles. The template code starts like this:
{% extends "base.html" %}
{% load staticfiles %}
{% load i18n %}
{% include "base.html" %}
{% block title %}{% trans "Main index" %}{% endblock %}
{% block content1 %}
It gets all the correct HTML from base.html but unstyled. I also try delete the 'include' tag or changing its position but there's no result. What can be wrong? Thank you.
Also, the console tells me this:
Not Found: /list/static/myapp/styles.css
[25/Mar/2016 01:03:03] "GET /list/static/myapp/styles.css HTTP/1.1" 404 3414
When I refresh the page (list is the page where there is the template I wanna get the styles from base) it keeps telling me this. List is not a directory in my project, but the /static/myapp/styles.css path is correct. What happens?
You have to call the CSS files within the base.html, so when you extend the base.hml in your other pages, the CSS will be called.
For me, I usually do this:
I have Head.html I call all Javascripts and CSS files I am using in the website inside it like this:
for CSS:
<link rel="stylesheet" type="text/css" href="/static/styles/example.css"/>
for javascript:
<script type="text/javascript" src="/static/js/example.js"></script>
then, in each page in the website, after the title tag I include this Head.html like this:
{% include "Head.html" %}
When you do this in every page, the CSS and javascripts files will be seen in all the pages and callable.
Also, the main urls.py file should be like this: "the answer is from here"
urlpatterns = [ # ... the rest of your URLconf goes here ...
] + static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
On top of your html file add this:
{% extends "base.html" %}
It should work, maybe the css path is wrong, or try to delete your {%block style_base%}

django crispy form button not showing

I am sure that I'm being very dense, but have spent days trying to work out what I've done wrong.
I have
installed django-crispy-forms (using pip)
added 'CRISPY_TEMPLATE_PACK' to my settings
added 'crispy_forms' to my applications
done a 'collectstatic' from the downloaded tar file (since templates and CSS aren't included in the pip)
added '{% crispy_forms_tags %} to my form html file
imported from crispy_forms.helper and crispy_forms.layout to my forms.py
I'm using Bootstrap 3, which I'm coding directly rather than using bootstrap-toolkit
The Bootstrap renders fine on all my pages and so do the form fields, but the buttons won't appear.
My html is as follows:
{% extends 'base.html' %}
{% load crispy_forms_tags %}
<html>
<title>Add Guest</title>
<div class='container'>
{% block col1 %}
<div class='row'>
<div class="span3">
{% endblock %}
{% block col2 %}
<div class="span9">
<ul>
{% crispy form %}
</ul>
</div><!--/span9-->
{% endblock %}
</div><!--row-->
</div><!--/container-->
</html>
If I change the crispy tag to {% crispy MyForm MyForm.helper %} I get 'VariableDoesNotExist at' plus a continuous of many (real) languages. Rendered as above the form fields appear but not the button.
My forms.py looks like this:
from django.forms import ModelForm
from django import forms
from crispy_forms.helper import FormHelper
from crispy_forms.layout import Layout, Fieldset, ButtonHolder, Submit
from .models import Author
class NewAuthorForm(forms.ModelForm):
class Meta:
model = Author
# If you pass FormHelper constructor a form instance
# It builds a default layout with all its fields
helper = FormHelper()
helper.form_method = 'post'
helper.add_input(Submit('save', 'save', css_class = 'btn-primary'))
I know that there's some redundancy here in terms of imports, but figure that isn't the problem.
Lastly, my base.html is:
<!DOCTYPE html>
<html>
<head>
<title>Bootstrap 101 Template</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<!-- Bootstrap -->
<link href="{{ STATIC_URL }}css/bootstrap.min.css" rel="stylesheet">
<!-- note that there’s also blue.uni-form.css and dark.uni-form.css available if you want to try chan-->
<link rel="stylesheet" href="{{ STATIC_URL }}crispy_forms/static/uni_form/uni-form.css" type="text/css" />
<link rel="stylesheet" href="{{ STATIC_URL }}crispy_forms/static/uni_form/default.uni-form.css" type="text/css" />
<!-- uni-form JS library, optional -->
<script src="{{ STATIC_URL }}crispy_forms/static/uni_form/uni-form.jquery.js" type="text/javascript"></script>
<!-- HTML5 Shim and Respond.js IE8 support of HTML5 elements and media queries -->
<!-- WARNING: Respond.js doesn't work if you view the page via file:// -->
<!--[if lt IE 9]>
<script src="https://oss.maxcdn.com/libs/html5shiv/3.7.0/html5shiv.js"></script>
<script src="https://oss.maxcdn.com/libs/respond.js/1.3.0/respond.min.js"></script>
<![endif]-->
</head>
<body>
<div class='container'>
{% block col1 %}
<div class='row'>
<div class="span3">
{% endblock %}
{% block col2 %}
<div class="span9">
</div><!--/span9-->
{% endblock %}
</div><!--row-->
</div><!--/container-->
<!-- jQuery (necessary for Bootstrap's JavaScript plugins) -->
<script src="https://code.jquery.com/jquery.js"></script>
<!-- Include all compiled plugins (below), or include individual files as needed -->
<script src="{{ STATIC_URL }}js/bootstrap.min.js"></script>
</body>
</html>
I've put the crispy form templates in my templates folder, which is at the same level as manage.py. As I say, all the bootstrap renders fine.
I'm stumped. Any ideas gratefully received
I have absolutely no idea why, but somehow removing
from __future__ import absolute_import
from views fixed the issue for me and now my buttons are showing up.
P.S Found this question, googling for a solution and decided to signup and hopefully help the next one with such a problem :).
It's an old post, but if I made it here on my searches, it's likely someone else will.
I'm using Django 1.11.9 and django-crispy-forms 1.7.0. You're on the right track, you actually want to add the 'submit' button in init for the class, not within Meta.
See below:
from django.forms import ModelForm
from django import forms
from crispy_forms.helper import FormHelper
from crispy_forms.layout import Layout, Fieldset, ButtonHolder, Submit
from .models import Author
class NewAuthorForm(forms.ModelForm):
class Meta:
model = Author
def __init__(self, *args, **kwargs):
super(NewAuthorForm, self).__init__(*args, **kwargs)
# If you pass FormHelper constructor a form instance
# It builds a default layout with all its fields
self.helper = FormHelper()
self.helper.form_method = 'post'
self.helper.add_input(Submit('save', 'save', css_class = 'btn-primary'))
http://django-crispy-forms.readthedocs.io/en/latest/crispy_tag_forms.html#fundamentals

Python images display Django

Since my last question here: Python images display
I understood that from all the answers I got the glob.glob could be the only one in the direction I need.
However where I am stuck right now is here:
I can create a list with all the filenames in my media directory by using glob.glob:
all = glob.glob("/Path_to_MEDIA/*/*.jpg")
But how can I use that and create a VERY SIMPLE image display with one next button that calls files in my MEDIA_ROOT and displays them.
What I know is:
I have a Template which looks something like the default directory index:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<meta http-equiv="Content-type" content="text/html; charset=utf-8" />
<meta http-equiv="Content-Language" content="en-us" />
<meta name="robots" content="NONE,NOARCHIVE" />
<title>Index of {{ directory }}</title>
</head>
<body>
<h1>Index of {{ directory }}</h1>
<ul>
{% ifnotequal directory "/" %}
<li>../</li>
{% endifnotequal %}
{% for f in file_list %}
<li>{{ f }}</li>
{% endfor %}
</ul>
</body>
</html>
I need to create a def in my views that feeds the list from glob.glob to this or similar template.
What I dont know:
How does this def in the view have to look like?
And here:
What do I have to write to display one image, sound in a browser?
What do I have to write to display a LIST of images, sounds?
Thanks for the time!
Make a direct-to-template url with extra-context in urls.py:
from django.views.generic.simple import direct_to_template
...
url(r'^whatever', direct_to_template,
{ 'template':'foo.html', 'extra_context': {'files':myfiles} }
name='whatever' ),
Where myfiles above is a list/tuple of your files. However, make sure to format your file list in terms of MEDIA_URL instead of based on MEDIA_PATH. For example:
myfiles = [ 'relative/path/foo.jpg',
'http://static.mysite.com/absolute/path/bar.jpg' ]
Though, obviously generated from the filesystem in your case, not a hardcoded list. And you could do the work in a view rather than using direct-to-template -- just make sure to put the files key/value into your context:
def myview( request ... ):
context = RequestContext(request)
context[files]=myfiles
return render_to_respone( ..., context_instance=context )
Then, in your template foo.html:
{% for file in files %}
<img src='YOUR_MEDIA_URL_HERE/{{ file }}' />
{% endfor %}

Categories