Dharmendra
Dharmendra

Reputation: 15

How to link html files together using Django?

i am using django 3.0 with python 3.8. i am new to coding. below is my project hierarchy.i want to connect my "home" page to next html "teoco" page by clicking on continue button for which i have below html code

myproject.urls-

from django.contrib import admin
from django.urls import path,include

urlpatterns = [
    path('', include('training_review.urls')),
    path('teoco', include('training_review.urls')),
    path('admin/', admin.site.urls),
]

home.urls-

from django.urls import path

from . import views
urlpatterns = [
    path('',views.home, name='home'),
    path('teoco',views.teoco, name='teoco')
]

home.views-

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


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

Upvotes: 0

Views: 7741

Answers (2)

AzyCrw4282
AzyCrw4282

Reputation: 7754

You can use Django's built-in URL tags. See here

Returns an absolute path reference (a URL without the domain name) matching a given view and optional parameters. Any special characters in the resulting path will be encoded using iri_to_uri().

This is a way to output links without violating the DRY principle by having to hard-code URLs in your templates:

{% url 'some-url-name' v1 v2 %}

So in your button have this set to the href attribute, for example:

<input class="btn btn-success" type="button" value="New Line" onclick="location.href="{% url 'addrow' %}""  />

This above code will navigate to the views.py as defined in your urls and you can render any html page as you like in that method.

Upvotes: 1

Vahid Kharazi
Vahid Kharazi

Reputation: 6033

you can create an HTML file like this:

home.html

<html>
<a href="/teaco" > Go to teaco page</a> or
<a href="{% url 'teoco' %}"> Go to teaco page </a>

</html>

Upvotes: 0

Related Questions