0

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')
1
  • you can directly use {% url 'teoco' %} in href Commented Feb 24, 2020 at 20:10

2 Answers 2

1

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.

Sign up to request clarification or add additional context in comments.

5 Comments

thanks for explaining do well. i tried putting below code but still showing ";" expected in the end of line..please help <input type="button" value="Continue" onclick="window.location.href='{ % url 'teoco' % }';">
Can you try changing it to <input type="button" value="Continue" onclick="window.location.href="{% url 'teoco' %}"" />.
thanks a lot, it worked..now i want to do the reverse of it also..i mean i want to add abutton in my second "teoco" page something like "go to the home page". how do i add one more address to the base.htm and how to call it from "teoco" page.below is my base.html </head> <body> <a href="{% url 'teoco' %}"> <input type="button" value="Continue" ></a> {% block content %} {% endblock %} </body> </html>
Great. If this has worked then please accept the answer( see here) to state that the initial question has been resolved.
If you want to do the reverse of it then it would follow the same logic. Firstly, a button with the URL tag defined, and then in the urls.py redirect to a method that will render the html page(in this case, the homepage) . If you are new to Django do consider reading this. I found this really helpful when I first came across this problem
0

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>

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.