I would like to make a header that is dynamic depending if a user is logged in, I have a custom login to active directory. logged in a parameter in a cookie, and I use the .get in python to fetch it.
I keep looking and not finding how to do this. Any help would be appreciated.
I always get index.html no matter what, and I'm sure I have the cookie.
{% if logged == 1 %}
{% extends "logged-index.html" %}
{% else %}
{% extends "index.html" %}
{% endif %}
If you want a dynamic header/navbar or footer; couldn't you just put if/else statements into your main template that you extend from, so that different links and things show depending on whether the user is logged in or not. This is what I did on my web app:
<ul class="navbar-nav">
{% if current_user.is_authenticated %}
<li class="nav-item">
<a class="nav-link" href="{{ url_for('account') }}">Account</a>
</li>
<li class="nav-item">
<a class="nav-link" href="{{ url_for('logout') }}">Logout {{current_user.username}}?</a>
</li>
{% else %}
<li class="nav-item">
<a class="nav-link" href="{{ url_for('logIn') }}">Login</a>
</li>
<li class="nav-item">
<a class="nav-link" href="{{ url_for('signUp') }}">Sign Up</a>
</li>
{% endif %}
Perhaps I do not understand what you are after, but:
I think that it would be the same logic as this dynamic title logic:
<head>
{% if logged == 1 %}
<title>{{ username }} - is logged in</title>
{% else %}
<title>Welcome to My Website!</title>
{% endif %}
</head>
I borrowed the concept from:
https://blog.miguelgrinberg.com/post/the-flask-mega-tutorial-part-ii-templates
Related
I’m trying to create a next and previous button on a single(detail) page. I don’t understand how to make this happen. These are my codes
views.py
def playsong(request, id,slug, tag_slug=None):
beat = Number_beat.objects.get(created_beats=id)
album = Album.objects.get(album_id= id)
albu = Album_beats.objects.filter(album_name= id)
bea = Beats.objects.all()
nextbeat = Number_beat.objects.filter(id__gt = beat.id).order_by(‘id’).first() #.values(‘id’)[:1]
lastbeat = Number_beat.objects.filter(id__lt= beat.id).order_by(‘id’).last()
tag = None
if tag_slug:
tag = get_object_or_404(Tag,slug=tag_slug)
beat=beat.filter(tags__in=[tag])
context={
'beat':beat,
'nextbeat':nextbeat,
'lastbeat':lastbeat,
'album':album,
'albu':albu, }
return render(request, 'music/playsong.html', context)
html
{% if lastbeat %}
<li class="page-item">
<a class="page-link" href="{% url 'playsong' lastbeat %}?{{ request.GET.urlencode }}" id="pred">{{ lastbeat }}</a>
</li>
{% else %}
<li class="page-item">
<a class="page-link" href="{% url 'index' %}" id="pred">home</a>
</li>
{% endif %}
{% if nextbeat %}
<li class="page-item">
<a class="page-link" href="{% url 'playsong' %}?next={{ nextbeat|urlencode }}" id="predj">{{nextbeat}}</a>
</li>
{% else %}
<li class="page-item">
<a class="page-link" href="{% url 'index' %}" id="pred">home</a>
</li>
{% endif %}
please can anyone help on can I do
after writing the view for my pagination in django, the button works fine, meaning that they load new pagesz but the problem is that all the posts still remains in all the new pages and that is not what's expected.
views.py
def ElementLists(request):
vectors = Vectors.objects.filter(status="published").order_by("?")
paginator = Paginator(vectors, 6)
page_number = request.GET.get('page')
vector_paginator = paginator.get_page(page_number)
elementlist.html
<li class="page-item">
{% if vector_paginator.has_previous %}
<a class="page-link" href="?page={{vector_paginator.previous_page_number}}" arialabel="Previous">
<span class="ti-arrow-left">Previous</span>
<span class="sr-only">Previous</span>
</a>
{% endif %}
</li>
<li class="page-item">
{% if vector_paginator.has_next %}
<a class="page-link" href="?page={{vector_paginator.next_page_number}}" aria-label="Next">
<span class="ti-arrow-right">Load More</span>
<span class="sr-only">Next</span>
</a>
{% endif %}
</li>
I later got a fix for it
when looping thorught your posts in template, it should now be this
before
{% for post in posts %}
...code here
{% endfor %}
Now
{% for post in posts_paginator %}
...code here
{% endfor %}
I want to show log out button when user is loged in and login button when user is loged out.But the buttons show as many times as I have users. How can I fix it?
--html code
{% for user in users %}
{% if user.user_id == session['user_id'] %}
<li class="nav-item">
<a class="nav-link js-scroll-trigger" href="/logout">Logout </a>
</li>
{% elif user.user_id != session['user_id'] %}
<li class="nav-item">
<a class="nav-link js-scroll-trigger" href="/login_register">Login </a>
</li>
{% endif %}
{% endfor %}
--server.py code
#app.route('/')
def landing():
mysql = connectToMySQL('comfort_zone')
query = "Select user_id from users"
users = mysql.query_db(query)
return render_template('index.html',users=users)
You just need to check if the user in session is present in users, then show a logout button, otherwise show a logout button. Fixed code:
index.html:
{% if logged_in %}
<li class="nav-item">
<a class="nav-link js-scroll-trigger" href="/logout">Logout </a>
</li>
{% else %}
<li class="nav-item">
<a class="nav-link js-scroll-trigger" href="/login_register">Login </a>
</li>
{% endif %}
server.py
#app.route('/')
def landing():
mysql = connectToMySQL('comfort_zone')
query = "Select user_id from users"
users = mysql.query_db(query)
logged_in = session['user_id'] in (user.user_id for user in users)
return render_template('index.html', logged_in=logged_in)
Depending on how session['user_id'] is set, I would do it like this:
{% if session['user_id'] %}
<li class="nav-item">
<a class="nav-link js-scroll-trigger" href="/logout">Logout </a>
</li>
{% else %}
<li class="nav-item">
<a class="nav-link js-scroll-trigger" href="/login_register">Login </a>
</li>
{% endif %}
Basically, if the user_id is set in the session (i.e. somebody is logged in), show the logout button.
i made a header for my site project and it has some sections...
i want to change the section color name on header using jinja but i can't
the template already rendered without error but section name doesn't change
and in pycharm this message shows up :Tag start is not closed
<ul class='menu'>
<li {% if section == "dashboard" %}class="selected"{% endif %}>
My dashboard
</li>
<li {% if section == "images" %}class="selected"{% endif %}>
Images
</li>
<li {% if section == "people" %}class="selected"{% endif %}>
People
</li>
</ul>
try this
{% if section == "dashboard" %}
<li class="selected">
{% else %}
<li>
{% endif %}
<ul class='menu'>
<li class="selected">
{% if section == "dashboard" %}
My dashboard
{% elif section == "images" %}
Images
{% elif section == "people" %}
People
{% endif %}
</li>
</ul>
Code:
class MyObjects(LoginRequiredMixin, ListView):
model = AllObjects
template_name = "my_objects.html"
paginate_by = 10
def get_queryset(self, *args, **kwargs):
queryset = super(MyObjects, self).get_queryset()
return queryset.filter(added_by=self.request.user).order_by('-last_modified')
I have a view that lists user's addings to a table. The problem is pagination is not working properly. I think it is because each time django renders the page, it filters the queryset again, causing only first 10 items to be listed. What can be done to handle this problem? Or should i not use generic view for doing such thing?
Template:
<table>
{% for obj in object_list %}
<tr>
<td>
{{ obj.name}}
</td>
</tr>
{% endfor %}
</table>
<nav aria-label="Page navigation">
<ul class="pagination">
<li class="page-item {% if not page_obj.has_previous %}disabled{%endif%}">
<a class="page-link" {% if page_obj.has_previous %} href="?sayfa={{ page_obj.previous_page_number }}" {% endif %} aria-label="Previous">
<span aria-hidden="true">«</span>
</a>
</li>
{% for page in page_obj.paginator.page_range %} {% if page == page_obj.number %}
<li class="page-item active"><a class="page-link" href="#">{{ page }}</a></li>
{% else %}
<li class="page-item"><a class="page-link" href="?sayfa={{ page }}">{{ page }}</a></li>
{% endif %} {% endfor %}
<li class="page-item {% if not page_obj.has_next %}disabled{%endif%}">
<a class="page-link" {% if page_obj.has_next %}href="?sayfa={{ page_obj.next_page_number }}" {% endif %} aria-label="Next">
<span aria-hidden="true">»</span>
</a>
</li>
</ul>
</nav>
I found my mistake. In my template, i used the link "?sayfa={{ page }}". ("sayfa" means page in Turkish, so it's localized version) It's a leftover from my previous pagination which i made with function based view. One way to fix is changing all "sayfa"'s to "page" but since this is not great for localization purposes adding page_kwarg = "sayfa" to my view fixed the problem. Thanks for the answers!