I'm having trouble using a script from a file in App1 in a page in app2.
My project structure looks like this:
I want to use the following file in home/static/scripts/scripts.py
Inside an html file in project/templates/project/s3.html
My scripts.py file looks like this:
import boto3
def listS3():
s3 = boto3.resource('s3')
for bucket in s3.buckets.all():
print(bucket.name)
My s3.html file looks like this:
{% include "home/_header.html" %}
{% block content %}
{% load staticfiles %}
<div class="s3">
</div>
{% endblock content %}
To be frank I'm not even sure how to load that script and how to call it afterwards as this is my first project in django.
Do I use:
{% load script.py %}
Or something like that?
How do I later call function listS3 in the html file?
Like this?
{% listS3() %}
? I appreciate your help.
I think you do not understand the basic Model-View-Template pattern structure of Django.
It might be a good idea to re-study the basic design of Django and take a look at the code.
This is Django's Official Tutorial
https://docs.djangoproject.com/en/1.11/intro/tutorial01/
Python script methods can not be called directly from within a Django template. In Django, you need to pass variables, objects, and Methods to be used in the Template in the form of "Context" in "View".
This is Django's official tutorial for that part.
https://docs.djangoproject.com/en/1.11/intro/tutorial03/#write-views-that-actually-do-something
Related
I am trying to make a blogging website. I know django provides argument templates like
{% include images.html with value=sense %}
The above code directly works in HTML and hence everything works. The images are stored in a backend database and connected to everystory by some logic. The user can use the names of the images and call whenever they need to use it
When I try the above code directly in the backend it doesn't work because I think once something is rendered then it doesn't rerender by django HTML
I wish to paste some form of links in the django story backend. such that when it renders in HTML automatically the page should show pics in the appropriate place. If anyone has any idea how to do this kindly let me know.
So when loading stories in the database the user can put some form of links for images in the database and while rendering all images come in a certain format as specified in the block in the blog.So there can be any number of images and the count is not longer fixed as shown in the pics below where I am trying to render a image called sense from the backend which doesn't work.. whereas it directly works in the frontend.
<p>{{object.story_title}}</p>
<p>{{MEDIA_ROOT}}</p>
<p>{{object.story}}</p>
{% include "blogdescription/image.html" with value=sense %}
Thank you for your time.
with regards
Let me start saying that doing exactly what you want is not possible because Jinja will compile and render {{object.story}} and not its content (the include). It does not seem possible to use nested Jinja syntax to load any resources, includes, extends, urls, etc.
Which explains why when you place the include in the template it works but does not inside your model field.
What seems possible is to load an HTML image with a explicit URL to the resource, lets say, the content inside your text field is:
<div style="display: flex; justify-content: center; align-items: center;">
<img src="/static/myimage.jpg" alt="Object Image">
</div>
Template.html (source):
{% block content %}
{{obj.title}}
<br>
{{obj.body|safe}}
{% endblock %}
Alternatively, it is possible to generate a HTML file to render dynamically based on Object.field. Note that this solution is a heavy load on the server, for every request will generate a dynamic file to be rendered.
Obj field value:
{% extends 'base.html' %}
{% block content %}
{{obj.title}}
<hr>
{% include 'includes/image.html' %}
{% endblock %}
views.py:
def story(request, id):
obj = Story.objects.get(id=id)
f = open(f'templates/generated/dynamic_template.html', 'w+')
f.write(obj.body)
f.seek(0)
return render(request, 'generated/dynamic_file.html', {'obj': obj})
I am trying to create template inheritance using Jinja2. I am using (basic.py) to define app and render "home.html". I created base.html as framework file that I want to inherit in home.html. Problem is that that the code
{% block content %}
{% endblock %}
in my base1.html file is not getting recognized when I run base.py. These lines of code are greyed out in PyCharm pro editor Also, the following code in home.html that I am using to extend base.html is also not working (greyed out):
{% extends [enter image description here][1]base1.html %}
{% block content%}
<h1>Hello, How are you?</h1>
{% endblock %}
I appreciate any help as there don't seem to be many direct answers available either on PyCharm site or anywhere else. Please see attached picture of code files. I do already have installed Flask package in my project.
i found a solution that worked. the solution is to select Jinja2 as the template language for for HTML template file type.enter image description here
I'm using django-pygmentify package in order to highlight my code blocks in my Django templates. The thing is that this package only supports code blocks as input. I have a model field that keeps markdown data. This markdown content might contain code blocks. (using ``` symbol)
Now, how can I highlight its inner code blocks??
Imagine I have a field that only contains source code. Like:
print('Hey..!')
In that case, this one works properly.
{% load pygmentify_tags %}
...
{% pygmentify %}
{{post.code}}
{% endpygmentify %}
Imagine my field contains the following content.
## Hello
This is my first step working with Python.
```python
print('Hey..!')
```
In this case, how can I implement it?? I can render that whole markdown content with {{post.body|markdown|safe}}, but how can I highlight those code blocks?? I also want to give all those code blocks a class name .code-block for better styling. Should I create a custom template tag?
You may use html standard <code> tag like this:
{% load pygmentify_tags %}
...
{% pygmentify %}
<code>
{{post.code}}
</code>
{% endpygmentify %}
This will separate the code section and at the same time will apply pygmentify to it.
I have been developing an app using the very easy-to-pickup Flask system and I have used a jinja template to write out a bunch of links that correspond to pages in Flask that are defined by something like this:
#app.route(/<var1>/<var2>)
...
...
in the test server these links work just fine however when I move from the test server to a server behind a proxy I get a problem where the href links don't take into account the extra directory name inserted by my proxy.
#where a link should read:
server:/myapp/<var1>/<var2>
# it acually puts out:
server:/<var1>/<var2>
my jinja etemplate looks like this but I am wondering if instead of putting a backslash there is a way to put a variable that specifies root directory.
{% block navigation %}
{% for record in db.values() %}
<li>{{record.name}}</li>
{% endfor %}
{% endblock %}
Any help would be greatly appreciated. Thanks Flask team!
Firstly, as #reclosedev said, you can generate URLs using the url_for function (assuming the view function is called myview:
<a href="{{ url_for('myview', var1=db.name, var2=record.name) }}">
Secondly, if you're behind a reverse proxy, wrap the WSGI application with this decorator that updates the request environment so that Flask generates correct URLs.
Maybe you are looking for url_for function?
<li><a href="{{ url_for('view_func_name',
var1=db.name,
var2=record.name) }}">{{record.name}}</a></li>
I’ve written a custom template tag:
def mytag(para):
return something
In my template I am getting a value {{value}}. Now I am using {{value|mytag}} to apply the tag to the value, and it is throwing a syntax error.
Your example looks like a filter. If that's all you want, it's fairly simple. Paul's links to the documentation should provide a fairly clear explanation of how and why to do things. Here's a quick start that should get you up and running though.
Create a folder in your app called "templatetags" with an empty __init__.py file
Create a file to hold your custom tags, we'll say "tags.py" for now.
your tags.py file should look something like this:
from django import template
register = template.Library()
#register.filter
def mytag(para):
return 'something'
then, in your template, you'll first have to load your custom tags, then you can have access to it.
{% load tags %}
My new value is: {{ value|mytag }}
To use template tags in Django, you wrap the name of the tag in {% and %}, like this:
{% mytag %}
If your tag takes a parameter, as yours seems to, you pass the parameter after the name of the tag:
{% mytag value %}
The syntax you were trying to use — {{ value|mytag }} is for template filters, not tags.
Template tags:
http://docs.djangoproject.com/en/1.2/topics/templates/#tags
http://docs.djangoproject.com/en/1.2/howto/custom-template-tags/#writing-custom-template-tags
Template filters:
http://docs.djangoproject.com/en/1.2/topics/templates/#filters
http://docs.djangoproject.com/en/1.2/howto/custom-template-tags/#writing-custom-template-filters