Django Sending Nav Pill Selection - python

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.

Related

send information in database from html page to another one

I have a flask html page that retrieve applicants information from the database sqlalchemy, I want to click on a name of applicant from the first page and another page open with specific information for this applicant.
I sent a query for the first page and it works like I want, but I struggled in the second page, I did not know how to send that one record
this is the code of the first page, applicant-report page is the second page
{% for report in reports %}
<div class="rec_box">
<a class="text" href="/applicant-report">
{% for a in applicant %}
{% if a.phone == report.applicant_phone %}
{{a.name}}
{% endif %}
{% endfor %}
</a>
<p class="rec_small">O: {{(report.op*100)}}%</p>
<p class="rec_small">C: {{report.co*100}}%</p>
<p class="rec_small">E: {{report.ex*100}}%</p>
<p class="rec_small">A: {{report.ag*100}}%</p>
<p class="rec_small">N: {{report.ne*100}}%</p>
</div>
{% endfor %}
Pass parameters between pages is usually using the URL, since this method is showed to the end user, I suggest not include any passwords or something like that
In the link you will redirect the user ends in something like
<a class="text" href="/applicant-report?phone={{a.phone}}">
Where the a.phone is the value of your variable. To read this url in the other page, you use something like
applicant_phone = request.args.get('phone')
applicant = session.query(Applicant).filter_by(phone=applicant_phone).first()
If you want to add another parameter to the url you concat using & between them, like
<a class="text" href="/applicant-report?phone={{a.phone}}&{{a.contact}}">
And so on.
Hope you find this usefull

Django Template Not Displaying Variables Correclty

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...

How do i show an active link in a django navigation bar dropdown list?

I have a navbar menu with a list of links which i want to show the active link when the user is on the page, so far i have managed to do this with links that dont have dropdowns like this.
But I cannot seem to get it right with the dropdown links in such a way that if the user is on a page on the dropdown link the parent link on the navbar gets highlighted.like shown below
Any help would be greatly appreciated.
If you define your URLs with names like this:
url('', 'home_view', name='home'),
url('posts/', 'posts_view', name='blog'),
url('contact/', 'contact_view', name='contact'),
You can use these names in the template to make the if statements work:
{% with request.resolver_match.url_name as url_name %}
<ul id="menu">
<li class="{% if url_name == 'home' %}active{% endif %}">Home</li>
<li class="{% if url_name == 'blog' %}active{% endif %}">Posts</li>
<li class="{% if url_name == 'contact' %}active{% endif %}">Contact</li>
</ul>
{% endwith %}
This saves you from problems with duplication in url paths.
You can do this by passing a variable in context dictionary views.
Example:
context['faculties']=True
and then in html
{% if faculties %}active{% endif %}
For every view function you can set a variable to which you want to make active.
There are two ways: either manage your CSS to highlight the parent item if any of its children are "active" or (at the expense of some maintenance overhead and loss of DRY) test for membership in the template:
<li class="dropdown {% if url_name in "neo_app:business,neo_app:IT,neo_app:hospitality" %}
active
{% endif %}>Faculties</li>
You can do this all on the front end very easily without passing anything from the backend based on the URL.
If your url for example is "/faculties", you can do:
{% if '/faculties' in url %} active {% endif %}

Django apps that add content to the same page

I have written a Django website with a simple home page containing a list of the following menu items:
Home
Users
Contact
I am now working on an optional Django app for this website. When installed, I would like it to add an extra menu item to the home page called "Extras".
What is the best way to achieve this sort of thing in Django?
I have considered adding something like this to my settings.py file:
MENU_ITEMS = [
'Home',
'Users',
'Contact',
]
Then inside my app's __init__.py, I would do something like this:
from django.conf import settings
settings.MENU_ITEMS.append('Extras')
And the views.py file would then pass this MENU_ITEMS list to the home page template. Is this the correct approach or is there a better way?
A tip i would give you from experience: don't mess with settings in runtime if possible, those variables are global and changing them may cause some nasty bugs. I'm not sure but with that code you may end up with something like:
MENU_ITEMS = [
'Home',
'Users',
'Contact',
'Extras',
'Extras',
'Extras',
'Extras',
]
Again, I'm not sure of that but you don't need to mess with your settings to achieve your purpose.
A way more clean approach is let your view manage your view. If all you want is a menu, create a base template and put the menu there and then extend from it. If you want to add anything in the future you will need to change that piece only (You can add conditions there if you want to render some parts depending on user or something).
For more details refer to the django docs.
For example:
<div class="header navbar">
<div class="container">
<a class="navbar-brand" href="/">{% endraw %}{{ cookiecutter.project_name }}{% raw %}</a>
<ul class="nav navbar-nav">
<li class="active">Home</li>
<li>About</li>
{% if request.user.is_authenticated %}
<li>{% trans "My Profile" %}</li>
<li>{% trans "Logout" %}</li>
{% else %}
<li>{% trans "Sign Up" %}</li>
<li>{% trans "Log In" %}</li>
{% endif %}
</ul>
</div>
</div>
{% block content %}
{% endblock content %}
If this is defined in your base template, the pages that extend it will show it by default, overriding only the section where the block content is located. If you want to add another link to your navbar just do something like:
<li class="active">My new item</li>
You define 'my_other_url' as usual in your urls.py.
This is the right approach to do what i think you are trying to do. You let the templates handle how your site looks, don't mess views (as in mvc) with your controller.
If you want to make the extra item optional you just have to send a flag or something to the template and use:
{% if flag %}
<li class="active">My new item</li>
{% endif %}

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