Debug School

rakesh kumar
rakesh kumar

Posted on

How to get current route or url to apply if else condition on header tag in django template file

To apply an if-elseif condition in a Django template to check the current route or URL, you can use the request.path or request.resolver_match.view_name attribute in combination with the and template tags. Here's an example:

{% if request.path == '/current-route/' %}
    <p>This is the current route.</p>
{% elif request.resolver_match.view_name == 'app:view_name' %}
    <p>This is a specific view.</p>
{% else %}
    <p>This is a different route or view.</p>
{% endif %}
Enter fullscreen mode Exit fullscreen mode

In the above code, we have two conditions. The first condition checks if the current route is by comparing the request.path attribute. If it matches, it displays the corresponding message. The second condition checks if the current view matches a specific view by comparing the request.resolver_match.view_name attribute. If it matches, it displays a different message. If none of the conditions match, the else block is executed and displays a default message.

You can customize the conditions and messages according to your specific routes and views. By using the and template tags, you can apply different conditions based on the current route or URL in your Django template

Practical Example

myproject/
    myapp/
        urls.py
        views.py
    templates/
        index.html
Enter fullscreen mode Exit fullscreen mode

In your urls.py file inside the myapp app, you define the URL patterns as follows:

from django.urls import path
from myapp import views

app_name = 'myapp'

urlpatterns = [
    path('', views.index, name='index'),
    path('about/', views.about, name='about'),
]
Enter fullscreen mode Exit fullscreen mode

In your views.py file inside the myapp app, you define the corresponding views:

from django.shortcuts import render

def index(request):
    return render(request, 'index.html')

def about(request):
    return render(request, 'about.html')
Enter fullscreen mode Exit fullscreen mode

In your index.html template, you can apply the if-elseif condition to check the current route and view names:

{% if request.path == '/' %}
    <p>This is the home page.</p>
{% elif request.resolver_match.view_name == 'myapp:index' %}
    <p>This is the index view.</p>
{% else %}
    <p>This is a different route or view.</p>
{% endif %}
Enter fullscreen mode Exit fullscreen mode

In this example, if the current route is the root URL '/', it will display "This is the home page." If the current view matches the 'myapp:index' view name, it will display "This is the index view." For any other route or view, it will display "This is a different route or view."

Practical Example

In your urls.py file inside the myapp app, you define the URL patterns as follows:

  from django.urls import path,include 
  from loginsignup import views 
 path('question_analysis',views.question_analysis,name='question_analysis'),
    path('grammer_correction',views.grammer_correction,name='grammer_correction'),
Enter fullscreen mode Exit fullscreen mode

In your views.py file inside the myapp app, you define the corresponding views:

from django.shortcuts import render

def question_analysis(request):
    return render(request, 'question_analysis.html')

def grammer_correction(request):
    return render(request, 'question_analysis.html')
Enter fullscreen mode Exit fullscreen mode

In your index.html template, you can apply the if-elseif condition to check the current route and view names:

<html>
<head>
    <title>ChatGPT</title>
</head>
<body>
    {% if request.path == '/question_analysis' %}
    <h1>question analysis api using model</h1>
    {% elif request.path == '/grammer_correction' %}
    <h1>grammer correction api using model</h1>
    {% else %}
    <h1> correction api using model</h1>
{% endif %}
    <form method="post">
        {% csrf_token %}
        {{ form.as_p }}
        <input type="submit" value="Send">
    </form>
    {% if generated_message %}
    <h2>Generated Message:</h2>
    <p>{{ generated_message }}</p>

    {% else %}
    <h1>Generated Message will display after data is saved into database:</h1>
    {% endif %}
</body>
Enter fullscreen mode Exit fullscreen mode

In this example, if the current route is the root URL '/', it will display "This is the home page." If the current view matches the 'myapp:index' view name, it will display "This is the index view." For any other route or view, it will display "This is a different route or view."

Output

Image description

Image description

Top comments (0)