I am trying to add a delete button to a searchable list of emails like
this
I was wondering if anyone knows a way to highlight multiple items on a list like this and then send a list of the names to python when you hit the delete button? I can only find ways to do this with radios. I am sorry if this is a very basic question! I am just new to WTF forms and I cannot find anything on them relating to bootstrap lists.
Here is the code in question:
<div class="list-group-flush">
<form class="form-inline" action="{{ url_for('home') }}" method ="POST">
{% if data %}
{% for x in data %}
<a class="list-group-item list-group-item-action">{{ x }}</a>
{% endfor %}
{% endif %}
</form>
</div>
Thank you in advance!
If you are not using javascript (JS) you will struggle. JS allows you to react to a user's search entry and dynamically update the page using information that is fetched via a flask api route that will also search the db, and return info in JSON format.
If you insist on doing this without JS, then when you click search a Flask route will find the relevant emails and display them in a new render_template, everytime the user clicks search a new page is rendered which is often not optimal. I would then use a MultiCheckbox:
from wtforms import SelectMultipleField, widgets
class MultiCheckboxField(SelectMultipleField):
widget = widgets.ListWidget(prefix_label=False)
option_widget = widgets.CheckboxInput()
def Form(*search_options):
class TempForm(FlaskForm):
pass
found_emails = some_db_query_method(*search_options)
setattr(TempForm, 'emails', MultiCheckboxField('List of queried emails',
choices=[(x[0], x[1]) for x in found_emails])
return TempForm()
You can configure the CSS to control the look and feel of your checkboxes, as part of the multicheckbox
<div class="list-group-flush">
<form class="form-inline" action="{{ url_for('home') }}" method="POST">
{% if data %}
<select multiple id="mySelect">
{% for x in data %}
<option class="list-group-item list-group-item-action">{{ x }}</option>
{% endfor %}
</select>
{% endif %}
</form>
</div>
Try using a select form element with the ability to select multiple items. You can then access the values via form values.
Related
I'm using edge repo for quick starting a django project. The repo has an app called "accounts" which handles signin up, logging in, etc with crispy forms. However i'd like to allow users to sign up from the home.html which isn't located in accounts/templates.
I have tried adding the following code to home.html:
{% if not user.is_authenticated %}
{% include "signup.html" %}
{% include "auth_base.html" %}
{% block form %}
{% crispy form %}
<div class="form-message">
<p>
Already signed up? Log in.
</p>
</div>
{% endblock form %}
{% endif %}
However since the main views.py can't locate views/forms in account app i'm faced with the following error:
django.template.base.VariableDoesNotExist: Failed lookup for key [form] in [{'True': True, 'False': False, 'None': None}, {}, {}, {'view': <my_env.views.HomePage object at 0x7f0ed11cec88>}]
My project files are the same as here with only "project_name" being different.
I expected to be able to use the signup form in home.html. Can anyone point me the right way?
I am building a tool in django. It allows myself, and others, to make entries (imagine a journal) for various 'topics'. When the topic is clicked a page opens ('topic' page) that shows the entries. I am trying to limit the length of that text to 200 characters such that if someone wants to see the whole entry, they need to click on that entry to open it up to its specific page.
I have tried putting a maxlength="200" in a few different places. But nothing worked. The only thing I did that kind of worked was to create a 'textarea' with a maxlength="200" but that was visually odd and unappealing. I know there has to be some way to limit the length in the existing code, without creating a new area and putting the entry in there.
Below is the code for the 'topic.html' page. I am thinking that should be enough. If you need the views.py or models.py, let me know.
{% extends "AARs/base.html" %}
{% block header %}
<h2>{{ topic }}</h2>
{% endblock header %}
{% block content %}
<p>
Add new action
</p>
{% for action in actions %}
<div class="panel panel-default">
<div class="panel-heading">
<small>
{{ action.date_added|date:'M d, Y H:i' }}
</small>
<small> Read or Edit Action</small>
</div>
<div class="panel-body">
<small>
<input type="text" name="action" maxlength="10">
{{ action.text|linebreaks }}
</small>
</div>
</div>
{% empty %}
No actions have been added yet.
{% endfor %}
{% endblock content %}
You might be looking for the truncation filters in Django's template language:
truncatechars
truncatechars_html
truncatewords
truncatewords_html
As an example, quoting from the docs:
{{ value|truncatewords:2 }}
If value is "Joel is a slug", the output will be "Joel is …".
I am currently looking at developing a "static" website, few pages only. However, by design, I can tell there is going to be repetitive layouts/patterns. I am thinking doing a data-oriented approach, with my HTMLs being as reusable as possible. Here is an example:
index.html:
<div>
{% include organisms/topBanner.html
tp-title=site.data.home.topbanner.title
tp-select-blurb=site.data.home.topbanner.select.blurb
button-text=site.data.generic.buttons.getstarted
button-link=site.data.generic.links.gosomewhere
%}
</div>
then my organisms/topBanner.html:
<div class="tb">
<h1>
{{ include.tp-title }}
</h1>
<div>
<h2>{{ include.tp-select-blurb }}</h2>
<div>
{% include atoms/button.html
%}
</div>
</div>
</div>
finally my atoms/button.html:
<a class="button" href="{{ include.button-link }}">{{ include.button-text }}</a>
I have multiple JSON file under _data that basically hold the texts. An example for the button would be a _data/generic/buttons.json:
{
"getstarted": "GET STARTED",
"completesurvey": "COMPLETE THE SURVEY"
}
or links.json:
{
"gosomewhere": "/go-somwhere",
"surveypage": "/survey"
}
So this means you need to pass all your data from the top level include of the organism so every bits in it would have its data. That way the example of that button is that the HTML is defined only once and the data is bound to it. And for a second button to be in the topBanner you could do something like this:
index.html:
<div>
{% include organisms/topBanner.html
tp-title=site.data.home.topbanner.title
tp-select-blurb=site.data.home.topbanner.select.blurb
b-getstarted-text=site.data.generic.buttons.getstarted
b-getstarted-link=site.data.generic.links.gosomewhere
b-survey-text=site.data.generic.buttons.completesurvey
b-survey-link=site.data.generic.links.surveypage
%}
</div>
and in the topBanner.html, you rebind the data to the dedicated button:
<div class="tb">
<h1>
{{ include.tp-title }}
</h1>
<div>
<h2>{{ include.tp-select-blurb }}</h2>
<div id="getstarted">
{% include atoms/button.html
button-text=include.b-getstarted-text
button-link=include.b-getstarted-link
%}
</div>
<div id="survey">
{% include atoms/button.html
button-text=include.b-survey-text
button-link=include.b-survey-link
%}
</div>
</div>
</div>
This approach means everything is data driven, there is no repetition/'copy/paste' of HTML, it all works through includes and you can apply atomic design pattern (http://patternlab.io/).
Wanna change the text of the button from 'GET STARTED' to 'LET'S START'? Go to the data/generic/buttons.json and change it there. The whole website now has the text changed.
The drawback is the fact that all the data has to trickle down from top level. Readability might be bad.
First use of Jekyll for me, and waned to have your opinion on this. What is good practice for static website dev like this? Is it easier to have a buttonGetStarted.html that includes a more generic button.html, and pass the data to button.html from buttonGetStarted.html? Like:
buttonGetStarted.html:
{% include atoms/button.html
button.text=site.data.generic.buttons.getstarted
button.text=site.data.generic.links.gosomewhere
%}
and then include buttonGetStarted every time I need it on the page? But then if I need a new button for the survey, I need to create another html buttonSurvey.html and so on... Sure on the code you see an {% include buttonSurvey.html %} which is easy to read and understandable straight away what this button is about. So this:
{% include button.html button.text=site.data.generic.buttons.getstarted %}
with only one file button for all the buttons, or
{% include buttonGetStarted.html %}
with creation of a new HTML file everytime I need a new button?
Thanks
F.
Disclaimer : As this question is primarily opinion-based (see SO help on this), I've voted to close it.
However, I can give my two cents. Quote are from Atomic Design Methodology.
Atom
[...] elements that can’t be broken down any further without ceasing to be functional
atom/buttons.html
<a class="button" href="{{ include.datas.button-link }}">
{{ include.dats.button-text }}
</a>
Molecule
[...] molecules are relatively simple groups of UI elements functioning together as a unit.
Here the question is : "do we need datas from organism / page for our molecule to work ?"
Yes : Datas will be passed by the parent organism. molecule/buttonGetStarded.html looks like (Note : this molecule is Homonuclear, but is functionnal.)
{% include button.html datas=include.buttonDatas %}
No : Datas will be set from inside the molecule (imaginary data structure)
{% include button.html datas=site.data.buttonDatas.getStarted %}
So in your case, I think that organism/topBanner.html can be composed like this (simplified for readability) :
{{ include.tp-title }}
<h2>{{ include.tp-select-blurb }}</h2>
<div id="getstarted"> {% include molecules/buttonGetStarted.html %}</div>
<div id="survey"> {% include molecules/buttonSurvey.html %}</div>
As I guess that your data files can be used for Internationalization (I18n) purpose. The molecule language doesn't need to be passed all the way down. It can be guessed by the molecule itself.
{% if page.language == nil %}
// if no language variable in page's front matter
// we default to site language set in _config.yml
{% assign language = site.language %}
{% else %}
// language variable present in front matter
{% assign language = page.language %}
{% endif %}
// get datas depending on guessed language
{% assign datas = site.data[language] %}
// this can even be more atomic with
{% assign datas = site.data[language]['buttonSurvey'] %}
// include the atom with correct language datas
{% include atom/button.html datas=datas %}
Note that this logic can even be factorized.
I am trying to make a Login Authentication in Django. I have made sign in and sign up buttons in the upper navbar.
Now What I need to achieve is when I sign in to the application the redirection would take place and at that time session is checked and if the session has started then the sign in and sign up button disappears and the User ABC button comes at its place.
I am trying to do this with my code snipped here it is.
{% if request.session.loggedin %}
<li><a data-toggle="modal" href="#"><b>Hello Chitrank</b></a></li>
{% else %}
<li><a data-toggle="modal" href="#signup"><b>Sign Up</b></a></li>
<li><a data-toggle="modal" href="#signin"><b>Sign In</b></a></li>
{% endif %}
Please suggest me what to do , Am i using a wrong way to check the session or if there is some other way to do this then the solution is welcome.
{% if user.is_authenticated %}
is what you are looking for. https://docs.djangoproject.com/en/dev/ref/templates/api/#django-contrib-auth-context-processors-auth
also allows you to do this
<span>Welcome back {{ user.username }}!</span>
There is another solution on this, based on the logged in user (requires that you use django build in auth system).
You may access request.user.is_authenticated in your template and differentiate on its state (True is loggend in).
You may check whether you're logged in in the corresponding view and pass variable current_user to your template, then check:
{% if current_user %}
Hello, {{current_user.name}}
{% else %}
{# ... Display signin and signup buttons #}
{% endif %}
<span id='question'>{{QuestionText}}</span>
<br>
<form action={{address}}>
{% for each in AnswerQuery %}
<span>{{each.answer}}</span><input type='radio' name="answer">
<span>Votes:{{each.answercount}}</span>
<br>
{% endfor %}
Hey, I am using django template for google app engine project. what is above is a snippet of my codes. But I am wondering if there is a way that I can choose to display the html partially. like if under some circumstances, I dont want the users to see the QuestionText, so I just close it up somehow while still displaying the rest of it. I know I can render different templates which are modified accordingly,but would that be too trivial that I have to render a bunch of different templates. Would be that nice if I can just change a part of the template and reuse it. I guess the magic is from the django part, the client side,but I dont know how. Any help? Thank you
If you'd like to hide the content and they show it with JavaScript (client-side) based on some user action, this will work:
<span id="question" style="display: none;">{{QuestionText}}</span>
<script type="text/javascript">
// This requires jQuery
$('document').ready(function(){
if(some_foo_bar == true){
$('#question').show();
}
});
</script>
If you want to just not show the portion of content at all (ie, don't even output it to the browser), this will work:
{% if some_variable_set_in_django_view_context %}
<span id='question'>{{QuestionText}}</span>
{% endif %}
<br>
<form action={{address}}>
{% for each in AnswerQuery %}
<span>{{each.answer}}</span><input type='radio' name="answer">
<span>Votes:{{each.answercount}}</span>
<br>
{% endfor %}