Reputation: 13
When I write code like this without .html
in the end of about/
:
from django.urls import path
from . import views
urlpatterns = [
path('', views.index, name='index'),
path('about/', views.about, name='about'),
path('contact/', views.contact, name='contact'),
path('categories/', views.categories, name='categories'),
path('docAdd/', views.docAdd, name='docAdd')
]
I get errors when click on about in the navbar of my webpage, error in that case is :
" The current path, about.html, didn't match any of these." But when I change about/ to about.html/ then I can click on about in navbar and page opens, but when I switch from about to contact in navbar I get error like: "The current path, about.html/contact.html, didn't match any of these."
This is the code with .html
suffix:
from django.urls import path
from . import views
urlpatterns = [
path('', views.index, name='index'),
path('about.html/', views.about, name='about'),
path('contact.html/', views.contact, name='contact'),
path('categories.html/', views.categories, name='categories'),
path('docAdd.html/', views.docAdd, name='docAdd')
]
views.py code:
from django.shortcuts import render
from . models import Document
def index(request):
return render(request, 'index.html')
def about(request):
return render(request, 'about.html')
def contact(request):
return render(request, 'contact.html')
def categories(request):
return render(request, 'categories.html')
def docAdd(request):
return render(request, 'docAdd.html')
This is the error when I click on about in navbar two times, when I have .html
suffix with about/ in urls.py:
Page not found (404)
Request Method: GET
Request URL: http://localhost:8000/about.html/about.html
Using the URLconf defined in thesisProject.urls, Django tried these URL patterns, in this order:
admin/
[name='index']
about.html/ [name='about']
contact.html/ [name='contact']
categories.html/ [name='categories']
docAdd.html/ [name='docAdd']
^media/(?P<path>.*)$
The current path, about.html/about.html, didn't match any of these.
This is the error when I click on about in navbar one time, when I have dont have .html
suffix with about/ in urls.py:
Page not found (404)
Request Method: GET
Request URL: http://localhost:8000/about.html/about.html
Using the URLconf defined in thesisProject.urls, Django tried these URL patterns, in this order:
admin/
[name='index']
about.html/ [name='about']
contact.html/ [name='contact']
categories.html/ [name='categories']
docAdd.html/ [name='docAdd']
^media/(?P<path>.*)$
The current path, about.html, didn't match any of these.
Code for navbar in index.html:
<div class="collapse navbar-collapse" id="ftco-nav">
<ul class="navbar-nav ml-auto">
<li class="nav-item active"><a href="index.html" class="nav-link">Home</a></li>
<li class="nav-item"><a href="about.html" class="nav-link">About</a></li>
<li class="nav-item"><a href="categories.html" class="nav-link">Categories</a></li>
<li class="nav-item"><a href="docAdd.html" class="nav-link">Add New Documents</a></li>
<li class="nav-item cta"><a href="contact.html" class="nav-link"><span>Get in touch</span></a></li>
</ul>
</div>
</div>
</nav>
<!-- END nav -->
I can't seem to solve this, please help.
Upvotes: 1
Views: 1606
Reputation: 226
The reason is that in the html u probably haven’t put a slash before the nav It should be like this
<a href=“/about/”>
This will go to about.html rather than add /about.html to the end of the url.
So on the nav bar it should be:
<li class="nav-item"><a href="/about/" class="nav-link">About</a></li>
change your nav bar to this:
<div class="collapse navbar-collapse" id="ftco-nav">
<ul class="navbar-nav ml-auto">
<li class="nav-item active"><a href="/index/" class="nav-link">Home</a></li>
<li class="nav-item"><a href="/about/" class="nav-link">About</a></li>
<li class="nav-item"><a href="/categories/" class="nav-link">Categories</a></li>
<li class="nav-item"><a href="/docAdd/" class="nav-link">Add New Documents</a></li>
<li class="nav-item cta"><a href="/contact/" class="nav-link"><span>Get in touch</span></a></li>
</ul>
</div>
</div>
urls.py should look like:
from django.contrib import admin
from django.urls import path
from main import views
urlpatterns = [
path('admin/', admin.site.urls),
path('', views.index, name='index'),
path('about/', views.about, name='about'),
path('contact/', views.contact, name='contact'),
path('categories/', views.categories, name='categories'),
path('docAdd/', views.docAdd, name='docAdd')
]
I hope this helps please comment if you don’t understand what I have put.
Upvotes: 2
Reputation: 142
Firstly, you should not include .html
in your urls file.
And when you put your link in the <a>
tag, you should use a django url-reversion template tag like this:
{% url 'about' %}
<li class="nav-item"><a href="{% url 'about' %}">About</a></li>
Also ensure you have a templates file setup in your app directory.
Upvotes: 1