Issue
I made basic HTML/CSS files, that I'm trying to run through Django, but every time I run it and try to switch pages, I get this error:
Page not found (404)
Request Method: GET
Request URL: http://127.0.0.1:8000/about.html
Using the URLconf defined in test.urls, Django tried these URL patterns, in this order:
admin/
[name='main']
add [name='about']
The current path, about.html, didn’t match any of these.
Here's what that my .urls file looks like:
from django.contrib import admin
from django.urls import path
from . import views
urlpatterns = [
path('admin/', admin.site.urls),
path('add',views.about,name='main.html'),
path('add',views.about,name='about.html')
]
Here's my .views file looks like:
from django.shortcuts import render
def main(request):
return render(request,'main.html')
def about(request):
return render(request, 'about.html')
Lastly Here's the section of my Settings file that I modified to find the file:
'DIRS': [os.path.join(BASE_DIR,'templates')],
'APP_DIRS': True,
Is there something else I'm supposed to do to make this work?
Solution
so the problem is here you should add the name of the URL of the page as an example http://127.0.0.1:8000/home
PS - in urls.py should write it like this home/
from django.contrib import admin
from django.urls import path
from . import views
urlpatterns = [
path('admin/', admin.site.urls),
path('Here/main.html/',views.main,name='main.html'),
path('here/about.html/',views.about ,name='about.html')
]
UPDATE 05/01/2022
Follow these steps to get the same result
Run-on your terminal
django-admin startproject Tutoanddjango-admin startapp tutoApplike you're did in this project.Go to
settings.pyand add your app toINSTALLED_APPSCreate a folder on your TutoApp and name it
templatesand create the following fileindex.html,about.html.Go to
settings.pyimport osand on TEMPLATES change'DIRS': []with'DIRS': [os.path.join(BASE_DIR),'templates']You need to add some functions to your
views.py
from django.shortcuts import render
def homepage(request):
return render(request, 'index.html')
def about(request):
return render(request, 'about.html')
- In /tuto/urls.py
from django.contrib import admin
from django.urls import path, include
urlpatterns = [
path('admin/', admin.site.urls),
path('',include('tutoApp.urls'))
]
- Back to you're app and create a file name
urls.py
from . import views
from django.urls import path
urlpatterns = [
path('home/', views.homepage),
path('about/', views.about)
]
add some HTML to these pages to recognize which page you're on.
- Run
python3 manage.py runserverand go to Home Page About page
Answered By - Yassin Amjad
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.