Skip to content
Advertisement

Set iframe url to access pages in navigation bar in Django

I am new to Django framework. I am trying to use html pages in templates and get the interfaces. I got all of the pages one by one. Now I want them appear in an iframe. Here is my html code in homeAdmin.html page.

<body>
    <div class="main-nav">
        <div class="main-nav-ul">
            <a href="{% url 'welcomeAdmin' %}" target="frame1"><span class="fa fa-home fa-lg"></span> Home</a>
            <a href="{% url 'register' %}" target="frame1"><span class="fa fa-user-plus fa-lg"></span> Register</a>
            <a href="{% url 'company' %}" target="frame1"><span class="fa fa-building fa-lg"></span> Company</a>
            <a href="{% url 'supplier' %}" target="frame1"><span class="fa fa-taxi fa-lg"></span> Supplier</a>
            <a href="{% url 'category' %}" target="frame1"><span class="fa fa-mouse-pointer fa-lg"></span> Category</a>
            <a href="{% url 'role' %}" target="frame1"><span class="fa fa-id-badge fa-lg"></span> Role</a>
        </div>
        <div class="target">
            <iframe src="{% url 'welcomeAdmin' %}" name="frame1" id="frame1" style="width: 100%;height: 540px; float: right;"></iframe>
        </div>
    </div>
</body>

And here is the views.py code that I have written for this problem.

``` from django.shortcuts import render
from django.http import HttpResponse

def welcomeAdmin(request):
    return render(request, 'newapp/welcomeAdmin.html')
def category(request):
    return render(request, 'newapp/category.html')
def company(request):
    return render(request, 'newapp/company.html')
def register(request):
    return render(request, 'newapp/register.html')
def role(request):
    return render(request, 'newapp/role.html')
def supplier(request):
    return render(request, 'newapp/supplier.html')
def homeAdmin(request):
    return render(request, 'newapp/homeAdmin.html') ```

I am lack of knowledge about what to do next in which file, like in urls.py. Here is the tried code, but it is not working.

``` from django.urls import path
from . import views
from django.views.generic,base import TemplateView

urlpatterns = [
    url(r'^welcomeAdmin/', TemplateView.as_view(template_name='welcomeAdmin.html'), name="welcomeAdmin"),
    path('', views.homeAdmin, name='homeAdmin'),
    path('welcomeAdmin', views.welcomeAdmin, name='welcomeAdmin'),
    path('category', views.category, name='category'),
    path('company', views.company, name='company'),
    path('register', views.register, name='register'),
    path('role', views.role, name='role'),
    path('supplier', views.supplier, name='supplier'),   
] ```

All the html files are in the same place. I would be very grateful if anyone could help me with this. Thank you.

Advertisement

Answer

I got a solution from the things I have found so far. In Django framework, they have restricted the iframe options because it might call security threats. So if you want to include iframes, add these parts in settings.py file

X_FRAME_OPTIONS = 'SAMEORIGIN'

And in views.py file, add following codes.

In the beginning:

from django.views.decorators.clickjacking import xframe_options_deny
from django.views.decorators.clickjacking import xframe_options_sameorigin

In the relevant def function:

@xframe_options_sameorigin
def home(request):
    return render(request, 'newapp/home.html')

Now you can see home page with iframes. Thank you.

User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement