Django Template Not Displaying Variables Correclty - python

I have a django page which is making an API call then displaying the results through a template. This has been working well but I must have changed something and now half the variables are not displaying correctly. Usually when there is an error calling a variable the template would just display a blank field. However, in my situation the webpage is displaying the code which is calling the variable. Here is what I have:
My HTML Code Looks like this:
<h6>Page</h6>
<ul class="list-inline mx-auto row">
<li class="list-group-item col-md-12">ASN: {{ context.upload.urlscan.page.asn }}</li>
</ul>
<ul class="list-inline mx-auto row">
<li class="list-group-item col-md-12">ASN Name: {{ context.upload.urlscan.page.asnname }}</li>
</ul>
<ul class="list-inline mx-auto row">
<li class="list-group-item col-md-12">IP: {{ context.upload.urlscan.page.ip }}</li>
</ul>
From this code ONLY the first and third line are displaying correctly - These lines are displaying the variables that they are assigned to.
The middle value Labeled 'ASN Name' is displaying the code back. On the webpage it looks like this:
{{ context.upload.urlscan.page.asnname }}
I have verified that the data is coming in correctly and this is happening in several places in my code. Not sure why some variables are working and some are not.
I am using Django 2.1 and Python 3.6 on RHEL 7.x
EDIT
Per the request here is my context creation and render portion
context['type'] = 'url'
context['upload']['cs_response'] = cs_response
context['upload']['cs_domain'] = cs_resp_dom
context['upload']['ppdom_rep'] = ppdom_rep
context['upload']['ppdom_ids'] = ppdom_ids
context['upload']['cs_test'] = ioc
context['upload']['urlscan'] = scan_res
context['js'] = json.dumps(context, indent=4)
return render(request, 'iocs/upload.html', {'context': context})

So this was a pain but I finally figured it out. I wanted to style a div and I did it in a kinda lazy way. My django template was extended from a 'base' template so right under the {% block content %} tag I added a <style> tag.
This worked and styled the div correctly, however for some reason it broke half my variables on the page. I HAVE NO IDEA WHY! When debugging I commented this entire <style> block out and my code was still broken. I had to completely delete the <style> block for it to work again.
Once the <style> block was removed all variables began working...

Related

How do I assign an ID to a div using Django?

I'm using mezzanine, based on the django framework of Python and I'm trying to add a method for my blog posts. In the models.py I have added an incremental counter to be used as an id reference.
`
def get_new_id():
n = 100
i = 1
for i in range(n):
if i ==100:
break
print (i) ##just checking if it actually did what I asked
return i
This way I want to be able to show the newest post on my homepage without having to change it manually.
I'd probably still need to reverse the count so the last blogpost becomes number 1, but regardless, the issue is not that.
I understand the code might already be too little for what I want it to do but even this simple loop does not print i in the console, let alone place itself into the meta tag. I have added the reference to the method in the correct html page (or so I believe)
from blog_post_list.html
{% for blog_post in blog_posts.object_list %}
<div id="{{ blog_post.get_new_id }}" class="post-preview">
<a href="{{ blog_post.get_absolute_url }}">
<h2 class="post-title">
{{ blog_post.title }}
</h2>
</a>
{% if settings.BLOG_USE_FEATURED_IMAGE and blog_post.featured_image %}
{% block blog_post_list_post_featured_image %}
<a href="{{ blog_post.get_absolute_url }}">
<img class="img-thumbnail" src="{{ MEDIA_URL }}{% thumbnail blog_post.featured_image 730 474 %}">
I am wondering if it is just the method that is poorly written or if I'm doing something else wrong entirely. If I can assign an ID to every blogpost I could use it anywhere on my website which would be awesome. I'm a novice still so I appreciate any help that you can muster. For the moment, when I run this code, the ID is NOT implemented in the html, rather the entire method is just ignored. the id meta tag of the div remains empty. However ,from the same page (blog_post_list.html) other methods are invoked and do work properly in the same fashion so the link is there.
Thanks in advance
If you would like to give an id to each item rendered in the for loop, and you would like your id to increment you can simply add a Django for loop counter. In that case this would give a unique id for each item.
{% for blog_post in blog_posts.object_list %}
<div id="{{ forloop.counter }}" class="post-preview">
<a href="{{ blog_post.get_absolute_url }}">
<h2 class="post-title">
{{ blog_post.title }}
</h2>
</a>
{% if settings.BLOG_USE_FEATURED_IMAGE and blog_post.featured_image %}
{% block blog_post_list_post_featured_image %}
<a href="{{ blog_post.get_absolute_url }}">
<img class="img-thumbnail" src="{{ MEDIA_URL }}{% thumbnail blog_post.featured_image 730 474 %}">
...
Also, FYI: if there are 101 items in your queryset and you give id=1 for the first one and 101st one, id will not be unique. In HTML you should give unique ids to elements.

Django Sending Nav Pill Selection

I have two pills here:
I just want to be able to click on them and have a variable be sent to my 'views' code to tell me which one is selected.
Here is the html:
<div class="col-md-2" id = "charts_tables">
<ul class="nav nav-pills nav-stacked" id = charts_tables>
<li {% if type_display == 'charts' %} class="active"{% endif %}></i> Charts</li>
<li {% if type_display == 'tables' %} class="active"{% endif %}></i> Tables</li>
</ul>
Ideally, when pushed I'd like to set the "type_display" variable to the name of the selected pill and pass that variable to my view file. I tried to make a similar post, but the only answer I receieved was to use radio buttons. For this project it has to be nav pills.
Here's the top of my view:
def analysis_view(request, action=None):
print(request.GET.get("radio-option"))
I've been trying to find documentation for the past two days and nothing. I don't even see a nav-pills widget type in any docs. Any help would be greatly appreciated.

Calling Jinja block from included file

I am building a little web application in Python 3.5, using Flask 0.11.1 and Flask-Admin 1.4.2.
Instead of using Flask-Admin as an administrative interface (only used by administrators of the website), I want it to be used by all the users of the website (to take profit of its awesome CRUD generation with forms using Select2 JS lib, inline forms, list views with filtering, etc.).
With that in mind, I want to override the list view template to be more user-friendly. For that, I am doing the following.
First, in my view I override list_template:
from flask_admin.contrib.sqla import ModelView
class FriendView(ModelView):
list_template = 'friend/list.html'
...
Then, I write the friend/list.html file by extending admin/model/list.html and overriding the block model_list_table defined in it:
{% extends 'admin/model/list.html' %}
{% block model_list_table %}
{% for row in data %}
<div class="col-xs-6 col-sm-4 col-md-3">
{% include 'friend/panel.html' %}
</div>
{% endfor %}
{% endblock %}
The panel.html file represents a Bootstrap panel illustrating each Friend:
<div class="panel panel-success">
<div class="panel-heading">
This friend is named {{ row.name }}.
</div>
<div class="panel-body">
{% block list_row_actions_column scoped %}
{{ super() }}
{% endblock %}
</div>
<div class="panel-footer">
...
</div>
</div>
Here is the problem: as you can see, I use in panel.html the block list_row_actions_column, which is a Jinja block defined in admin/model/list.html.
Of course, this raises the following error:
jinja2.exceptions.UndefinedError: there is no parent block called 'list_row_actions_column'.
because the file panel.html is not extending admin/model/list.html.
I have tried to define panel as a Jinja macro and to import it in friend/list.html, this raises the same error...
I know that I could simply not separate the panel in another file, however my friend/list.html is pretty big, and I think it is a good thing to do it that way, for sake of readibility and decoupling.
Does someone have an idea about how I can proceed?
Thanks,
Edouard
just make an html file and write single block of code in html file then include that file into that particular location where you want to need that code. that is the best method without use of any #block or #micro and for every block of code use a new html file and use this file as a function by including it on that place.

How to use django base extension along with angularjs ng-include?

I'm using ng-include like this
HTML
<div ng-app="myapp" ng-controller="myctrl" ng-include="/static/html/main.html"></div>
main.html
{% extends "app\base.html" %}
{% block content %}
<div ng-controller="myctrl1">
some content
</div>
{% endblock %}
base.html
<div ng-controller="myapp2">some content </div>
but this is not working, it is showing an errorr "SyntaxError: Unexpected token % at Object.parse (native)"
Please suggest me a better way to implement this.
I don't quite know what you're trying to do here. Angular templates and Django ones are quite different, even if they superficially share the same {{ var }} syntax. You can't inherit an Angular template from a Django one, or vice versa.
But there's no reason to want to do that anyway. Angular is already loading the template as a partial, which means it will be inserted into the div in base.html. There's no need for inheritance here at all.

django template conditional html

I'm not sure if the title is technically correct (sorry, I'm new to python+django)
I have a template page that displays whether an application is running or stopped depends on its status. For example:
If app is running I want to display:
<div class="lt">
<a class="play" title="App running">
<span class="text_play">Running</span>
</a>
</div>
<div class="rt">
<input type="submit" onclick="stop_app()" value="stop" class="stop">
</div>
If the application is not running then show this instead:
<div class="lt">
<input type="submit" onclick="star_app()" value="start" class="play">
</div>
<div class="rt">
<a class="stop" title="Application is not running">
<span class="test_stop">Not Running</span>
</a>
</div>
This is kind of stripped down simplified html but my point is how can I avoid repeating myself?
The template is passed a dictionary of applications that it iterates over to display all the applications and their status (running/stopped). So currently I'm iterating over the dict twice, one for "stopped" apps and one for the "running" apps.
Hope it's clear
Thanks in advance
EDIT: This is what I have tried so far:
{% if application.service.status|lower == "enabled" %}
<div>...display running HTML...</div>
{% else %}
<div>...display the non-runing HTML..</div>
{% endif %}
I just want to know if I'm doing the right thing (DRY?)
What you proposed is pretty DRY.
{% if application.service.status|lower == "enabled" %}
<div>...display running HTML...</div>
{% else %}
<div>...display the non-runing HTML..</div>
{% endif %
Keep in mind you'll rely on the return render(request... for determining the html Django needs construct.
Your proposed solution will choose one or the other. I.e. if your non-running HTML needs to switch to running HTML you won't have access to it without another render.
To be more clear and concise, django templates will construct the appropriate HTML leaving out the alternative options or "conditions".
If you learn a bit of jQuery for example you can have elements of the page switch the currently displayed html. Expanding this to ajax will allow you to get status updates from the server and vice versa.

Categories

Resources