Django Template: remove the buttons when session starts - html

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 %}

Related

Django: How to use views&forms of an app in main templates?

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?

Selecting multiple items on an actionable list bootstrap, flask-wtf

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.

How to display specific navbar when logged in?

I have created 2 navbars, one for when user is logged in and one for when they are not. I need to display the correct navbar dependending upon if the user is logged in or not. I know i need to check in Django with something like this,
{% if user.is_authenticated %}
{% return navbarLogged %}
{% else %}
{% return navbarsignup %}
But I'm confused on what I should include in my return and where exactly to put this in my code.
Should I add this to my base.html? Or, can i just create a function in my models and then call the function at the beginning of base.html?
{% if user.is_authenticated %}
<nav> navbar to show when authenticated </nav>
{% else %}
<nav> navbar to show when user is not authenticated </nav>
{% endif %}
and you can use it on your base html if the navbar is used in all pages

Jekyll IF statement for 'IF index (home) and NOT paged'?

As you may know Jekyll uses Liquid tags, and the Liquid templating engine has support for If / Else / Unless statements. Does anyone know how to show a specific content only on the 'homepage` (& NOT paged)?
Got it!
UPDATE: This doesn't work, if you don't yet have enough posts for pagination. That is, it only works if you have at least page1 and page2.
As you may already know, Jekyll supports pagination. So, to target just the Index/Home page (and specifically only the first page, i.e. page1 and NOT page2, page3 ...), you can use this:
{% if paginator.next_page == 2 %}
<div id="welcome">Hello, welcome to my blog!</div>
{% endif %}
{% if paginator.next_page == 2 %} tells Jekyll to check if the next page of the pagination is page2 (i.e. the current page is page1) and show the specified content.
This works best of all:
{% if paginator.previous_page %}
{% else %}
<div id="welcome">Hello, welcome to my blog!</div>
{% endif %}
Untested, but {% if paginator.previous_page == 0 %} might work as well.
The best way to to is :
{% if paginator.page==1 %}
// Do something here
{% endif %}

Menu navigation with Django

I have the view:
def about(request):
return render(request, 'about.html', {'page_about_exist':1,})
In html I have this code:
<li {% if page_about_exist %} class="active" {% endif %}> About</li>
So, If I go to the About-page, in menu it has class active, and I can see it visually.
Is there any other way without the dictionary? Because I have in url
url(r'^accounts/login/$', 'django.contrib.auth.views.login', name='login',),
and my way with dictionary doesnt look great there. Thanks.
You should do this kind of thing with template inheritance. For example, your base.html might include a navigation list with:
<li{% block products %}{% endblock %}><a href...>Products</a></li>
<li{% block about %}{% endblock %}><a href...>About</a></li>
Then in your about template (assuming it inherits from base) you have:
{% block about %} class="active"{% endblock %}
This will render as pure html, using the class you have defined for active pages. Because it uses simple template inheritance you can also get really fine control with this.
I use my own template tag called ifnav.
http://pypi.python.org/pypi/django-ifnav-templatetag
https://bitbucket.org/bikeshedder/django-ifnav-templatetag
The usage is very simple. Just add it to your INSTALLED_APPS and make sure the request context processor is activated.
Afterwards you can write this:
<li {% ifnav "^/about/" %} class="active" {% endifnav %}> About</li>
For current projects I use Django CMS which takes care of rendering the navigation.