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
Related
The official Jekyll tutorial has an entire section on using a YAML file to define a custom sequence of pages: https://jekyllrb.com/tutorials/navigation/
But it doesn't mention anywhere how one might create previous/next navigation buttons on the pages within that sequence, which is particularly ironic considering that the tutorial itself has them.
I've come up with some Liquid to determine the index of the current page:
{% for item in site.data.encore.docs %}
{% if item.url != page.url %}
{{ item.title }}: {{ forloop.index }}
{% else %}
<strong> This page index: {{ forloop.index }}</strong>
{% assign this_page_index = forloop.index %}
{% break %}
{% endif %}
{% endfor %}
but getting the index of the previous page via {% decrement this_page_index %} always returns -1 for some reason, and something like {% assign previous = this_page_index - 1 %} isn't valid Liquid. Same goes for trying to get the next page with similar methods.
What's the ideal way to accomplish this? I've searched every way I can think of and not found anything.
You can find the code for Jekyll's own navigation on their tutorials page by sifting through their GitHub repo until you get to their section_nav_tutorials.html, but it appears the way to do it is very close to what you have.
Liquid doesn't respect you doing math directly, you have to use a filter. For you, you'd use {% assign previous = this_page_index | minus: 1 %}.
I am wondering how to go about creating a dynamic menu in Jekyll that automatically populates the navigation with nav items whenever a new page is created. I read an article that touched a bit on the subject, but it was geared toward just sub-nav items. Has anyone had any experience in doing something like this?
Thanks!
It is a bad idea to do this automatically. However, it is very easy to achieve. Here is the code:
<ul>
{% for item in site.pages %}
<li {% if page.url contains item.url %}class="active"{% endif %}>
{{ item.title }}
</li>
{% endfor %}
</ul>
Source: https://jekyllcodex.org/without-plugin/simple-menu/
To define the order of appearence, you might want to add a front matter variable called 'order' to the pages and add a different pagenumber to this variable on each page. The code should sort the pages before looping over them. That looks like this:
{% assign sitepages = site.pages | sort: order %}
{% for item in site.pages %}
...
{% endfor %}
Happy coding!
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 %}
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 %}
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.