Dears,
I am new to Flask and HTML. I created a python app that is controlled by a SQL Server database (data and settings)
I needed an admin interface, so I used the below template and redesigned the pages.
https://github.com/app-generator/flask-argon-dashboard
I just need to query database from this Flask app and put the data into a table in this template. I searched a lot with no hope.
Solved by using pyodbc to query data, and edit /app/home/routes.py like the below:
def myfunction
And then:
return render_template("home/" + template, segment=segment, data=myfunction())
and on the HTML files, but {% for roww in data %} and {% endfor %} into
Related
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().
I am trying to use the example of Django tables2 to export xls and csv. this is the part of my html:
{% for format in table.export_formats %}
<a href="{% export_url format %}">
download <code>.{{ format }}</code>
</a>
{% endfor %}
My web page is class view based with filter, I can see my web url with query string attached with '?_export=xls', which is expected. However, when I clicked, no response, out output file created? not sure if anything else to make it work?
My work is almost the same as this example, but no output created when clicked. Any ideas? thanks a lot.
I am using django tables 2 and trying to export my table following the official documentation
In your template:
{% load querystring from django_tables2 %}
Download CSV
Download XLSX
In your view:
from django_tables2.export.export import TableExport
export_format = request.GET.get('_export', None)
if TableExport.is_valid_format(export_format):
table = [[your table object]]
exporter = TableExport(export_format, table)
return exporter.response('File_Name.{}'.format(export_format))
The second line is to check if the url was clicked and the _export flag is included in the request. print request.GET if you want to see what's included in the request and you should see '_export' as a key with the format as the value:
{'_export':'csv'}
So I'm currently learning to use Django, and I'm wondering how to correctly split up parts of the functionality while still displaying it on the main page.
For example, I want the header + navigation, a calendar and recent blog articles on the main index page.
On the view article page I'd for example have the header + nav, the calendar and a single article with a comment section.
Now reading the tutorials, if I understand them correctly, I'd split the functionality in a header app, a calendar app and the blog app itself while glueing it together with a core app.
But what I don't understand is how to render apps/other views in the main app. All the ways I found only specify templates itself or look very hacky, so apparently that doesn't seem to be the common way to go.
So there are multiple views are work here:
1.The views that you want to show
2.The view that will render the page, displaying all of the views (lets call this the 'main view'.
The first step is to import all of the other views / models into the views.py file that the main view resides in.
from blog.models import Post
from calendar.models import Calendar
Now you can edit your mainview to acces this data. For example:
class Mainview(TemplateView):
template_name = 'app/homepage.html'
def get_context_data(self, **kwargs):
#This will only show the latest post
data['posts'] = Post.objects.all().order_by('-id')[:1]
data['calendar'] = Calendar.objects.all()
return data
Now you can acces the data from the other apps in your template using the {{ }} tags, for example - you could do something like this:
{% for post object in post %}
{{ post.title }}
{{ post.content}}
{% endfor %}
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.
I m creating a django site (using django forms) that lets users subscribe their email. How do I get that email into a text file? Currently I have a cron job that runs a python script sending an email to all subscribers every morning. Is this the best way to do this or there is some built-in functionalities in django-forms that I can use?
If I understand you correctly, you have a list of users from their email address saved into the database, and want to save that list to a text file?
Well you could go into ./manage.py shell and type something like this:
# I assumed the email addresses are in User.email; modify as needed to conform to your model.
with open('email.txt','w') as f:
for u in User.objects.all():
f.write(u.email + '\n')
You could also write a management command to do this:
http://docs.djangoproject.com/en/dev/howto/custom-management-commands/
or create a simple view & template that creates a text file with all your email addresses in a list. Though for the sake of your users not being spammed password protect this, and don't make this publicly accessible. Something simple like (that is not private) should work:
urls.py:
from django.views.generic.list_detail import object_list
urlpatterns = patterns('',
(r'^full_emails/$', 'object_list', {'template': 'template/text.txt', 'queryset'=User.objects.all()}
)
template/text.txt:
{% for user in object_list %}
{{ user.email }}
{% endfor %}