Request URL: Category 404 - python

I have, Page not found (404) when accessing /category/Django
urls.py
from .views import (
PostListViewHome,
CategoryView,
)
from . import views
urlpatterns = [
path("", PostListViewHome.as_view(), name="Blog-home"),
path("category/<str:cats>/", views.CategoryView, name="category"),
]
views.py
def CategoryView(request, cats):
category_posts = Post.objects.filter(category=cats)
return render(request, 'Blog/category.html', {'cats':cats, 'category_posts':category_posts})
urls.py in the MPIE02
from django.contrib import admin
from django.contrib.auth import views as auth_views
from django.urls import path, include
from django.views.generic.base import TemplateView
from users import views as user_views
from django.conf import settings
from django.conf.urls.static import static
urlpatterns = [
path('admin/', admin.site.urls),
path('logout/', auth_views.LogoutView.as_view(template_name='users/logout.html'), name='Logout'),
path('', include ('Blog.urls')),
]
if settings.DEBUG:
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
if i try to accessing /category/ it showin me just 404

You are getting this error because in your settings file, there is no any path added of templates.
Add path in settings file:
import os
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [os.path.join(BASE_DIR,'appname','templates',)],
'APP_DIRS': True,
'OPTIONS': {
'context_processors': [
'django.template.context_processors.debug',
'django.template.context_processors.request',
'django.contrib.auth.context_processors.auth',
'django.contrib.messages.context_processors.messages',
],
},
},
]

Related

Django: Template does not exist

Please can you help me understand what I have done wrong here, I am sure it's a very simple problem but, I am new to django and especially the latest version...
I believe and as far to my research that I have routed my views to the best of my knowledge, please may you help identify my issue, I am getting an error:
"Page Not Found"
url.py from my app
from django.urls import path
from . import views
urlpatterns = [
path('', views.index, name='index')
]
views from my app
from django.shortcuts import render
from requests import request
# Create your views here.
def index(request):
return render(request, 'landing_page/index.html')
url from my project folder
from django.contrib import admin
from django.urls import path, include
urlpatterns = [
path('/', include('comp.urls')),
path('admin/', admin.site.urls),
]
urls from my project
and then here is my project and template folders, I have attached an image for your ease of helping me:
In settings.py file, just add this:
os.path.join(BASE_DIR, 'templates')
EX:
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [os.path.join(BASE_DIR, 'templates')], #Removed landing_page from here
'APP_DIRS': True,
'OPTIONS': {
'context_processors': [
'django.template.context_processors.debug',
'django.template.context_processors.request',
'django.contrib.auth.context_processors.auth',
'django.contrib.messages.context_processors.messages',
],
#Removed libraries from here
},
},
]

TemplateDoesNotExist at / customer/index.html Request Method

enter image description hereenter image description herei get the error of Template does not exist # [[e](https://i.stack.imgur.com/gSKFa.png)](https://i.stack.imgur.com/795HN.png)
this is my urls
when i run i get the error emplateDoesNotExist at / customer/index.html Request Method
from django.contrib import admin
from django.urls import path
from django.conf import settings
from django.conf.urls.static import static
from customer.views import Index, About
urlpatterns = [
path('admin/', admin.site.urls),
path('', Index.as_view(), name='index'),
path('about/', About.as_view(), name='about'),
] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
Your Templates settings in settings.py should look like this
TEMPLATES = [
{
"BACKEND": "django.template.backends.django.DjangoTemplates",
"DIRS": ["templates"],
"APP_DIRS": True,
"OPTIONS": {
"context_processors": [
"django.template.context_processors.debug",
"django.template.context_processors.request",
"django.contrib.auth.context_processors.auth",
"django.contrib.messages.context_processors.messages",
],
},
},
]
And your urls.py in your deliver1 folder should look like this
from django.contrib import admin
from django.urls import path, include
urlpatterns = [
path("admin/", admin.site.urls),
path("", include("the-name-of-your-app.urls")),
]
I don't have a lot of experience with class-based views. However you can try this to see if works for you
def index(request):
return render(request, "customer/index.html", {})
Your urls.py in your customer folder will look like this
from django.urls import path
from . import views
urlpatterns = [
path("", views.index, name="index"),
]
so any error you encouter, let me know

Template Issue of Views.py

I am new to Django. So I am trying to add a HTML template to the views.py but it is showing an error, it would be great if you would help me.
The full Error that I am seeing:
TemplateDoesNotExist at /
platform/home.html
Request Method: GET
Request URL: http://127.0.0.1:8000/
Django Version: 3.0.8
Exception Type: TemplateDoesNotExist
Exception Value:
platform/home.html
Exception Location: C:\Users\Adeel\AppData\Local\Programs\Python\Python38-32\lib\site-packages\django\template\loader.py in get_template, line 19
Python Executable: C:\Users\Adeel\AppData\Local\Programs\Python\Python38-32\python.exe
Python Version: 3.8.3
Python Path:
['C:\\Users\\Adeel\\Google Drive\\Visual Studio\\Personal Portfolio\\Portfolio',
'C:\\Users\\Adeel\\AppData\\Local\\Programs\\Python\\Python38-32\\python38.zip',
'C:\\Users\\Adeel\\AppData\\Local\\Programs\\Python\\Python38-32\\DLLs',
'C:\\Users\\Adeel\\AppData\\Local\\Programs\\Python\\Python38-32\\lib',
'C:\\Users\\Adeel\\AppData\\Local\\Programs\\Python\\Python38-32',
'C:\\Users\\Adeel\\AppData\\Roaming\\Python\\Python38\\site-packages',
'C:\\Users\\Adeel\\AppData\\Local\\Programs\\Python\\Python38-32\\lib\\site-packages',
'C:\\Users\\Adeel\\AppData\\Local\\Programs\\Python\\Python38-32\\lib\\site-packages\\win32',
'C:\\Users\\Adeel\\AppData\\Local\\Programs\\Python\\Python38-32\\lib\\site-packages\\win32\\lib',
'C:\\Users\\Adeel\\AppData\\Local\\Programs\\Python\\Python38-32\\lib\\site-packages\\Pythonwin']
Server time: Tue, 13 Oct 2020 06:14:44 +0000
views.py:
from django.shortcuts import render
def home(request):
return render(request, 'platform/home.html')
urls.py:
from django.contrib import admin
from django.urls import path
from django.conf.urls.static import static
from django.conf import settings
from Platform import views
urlpatterns = [
path('admin/', admin.site.urls),
path('', views.home, name='Home'),
]
settings.py:
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [],
'APP_DIRS': True,
'OPTIONS': {
'context_processors': [
'django.template.context_processors.debug',
'django.template.context_processors.request',
'django.contrib.auth.context_processors.auth',
'django.contrib.messages.context_processors.messages',
],
},
},
]
My HTML template that i am trying to add:
<h1>This is my project</h1>
Add the template to your directories
TEMPLATES = [
{
...
'DIRS': [
os.path.join(BASE_DIR, 'your/way/to/templates'),
],
...
]
After that
def home(request):
return render(request, 'home.html')
Hope I didn't miss anything
EDIT
You should have a separate folder with templates
and in urls.py
from . import views

(django)TemplateDoesNotExist at /

urls.py
from django.urls import path, include
urlpatterns = [
path('', include('calc.urls')),
path('admin/', admin.site.urls),
]
views.py
from django.shortcuts import render
def home(request):
return render(request, 'tmpl/home.html')
settings.py
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [os.path.join(BASE_DIR,'tmpl')],
'APP_DIRS': True,
'OPTIONS': {
'context_processors': [
'django.template.context_processors.debug',
'django.template.context_processors.request',
'django.contrib.auth.context_processors.auth',
'django.contrib.messages.context_processors.messages',
],
},
},
calc/urls.py
from django.urls import path
from . import views
urlpatterns = [
path('', views.home, name='home')
]
folder structure:
It looks like your post is mostly code; please add some more details.
It looks like your post is mostly code; please add some more details.
in your settings.py inside TEMPLATES do:
'DIRS': [os.path.join(BASE_DIR, 'top', 'tmpl')]
and in views.py
return render(request, 'home.html')

Trying to set up urls.py to direct to index.html

As the question implies, I am trying to set up my urls.py file to point towards my index.html file.
Here is the structure of my project:
-->mysiteX
---->.idea
---->mysite
------->migrations
__init__
admin.py
apps.py
models.py
test.py
views.py
---->mysiteX
-------->templates
index
---->css
---->fonts
---->js
---->vendors
__init__
settings
urls
wsgi
----->venv
db.sqlite3
manage
This is what my urls.py file looks like
from django.conf.urls import url
from django.contrib import admin
urlpatterns = [
url(r'^index/', admin.site.urls),
]
my views.py file
from __future__ import unicode_literals
def index_file(request):
return render(request, "index.html")
settings.py:
import os, sys
abspath = lambda *p: os.path.abspath(os.path.join(*p))
PROJECT_ROOT = abspath(os.path.dirname(__file__))
sys.path.insert(0, PROJECT_ROOT)
TEMPLATE_DIRS = (
abspath(PROJECT_ROOT, 'templates'),
)
When I run manage.py I get this page
Update after making changes to my urls.py file to this extent:
from django.conf.urls import url
from django.contrib import admin
from gradientboost import views
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^/$',views.index_file,name='index')
]
This is what I am getting
Second Update
changed my settings.py file according to an answer given
import os, sys
# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
TEMPLATE_DIR = os.path.join(BASE_DIR, 'mysiteX/templates')
...
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [TEMPLATE_DIR],
'APP_DIRS': True,
'OPTIONS': {
'context_processors': [
'django.template.context_processors.debug',
'django.template.context_processors.request',
'django.contrib.auth.context_processors.auth',
'django.contrib.messages.context_processors.messages',
],
},
},
]
However still getting this error
You Need To Change Your Urls.py
Because You Are Changing Admin Url Admin Url Is For Admin Login And For Website Control!
and if you add index/ than you need to type in your brower like that domainname.com/index
So if you Want Your index.html show on website home page try my code:
Try This If You browsing this link 127.0.0.1:8000
from django.conf.urls import url
from django.contrib import admin
from mysite import views
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^$', views.index_file, name='YOUR NAME')
]
And if you trying this link 127.0.0.1:8000/index Try This:
from django.conf.urls import url
from django.contrib import admin
from mysite import views
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^index/$', views.index_file, name='YOUR NAME')
]
Also Add This In Your Settings.py
import os
# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
TEMPLATE_DIR = os.path.join(BASE_DIR,'mysiteX/templates')
And Also Add This In your Settings.py
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [TEMPLATE_DIR,],
'APP_DIRS': True,
'OPTIONS': {
'context_processors': [
'django.template.context_processors.debug',
'django.template.context_processors.request',
'django.contrib.auth.context_processors.auth',
'django.contrib.messages.context_processors.messages',
],
},
},
]
create a html template index.html and your url should be redirected the view.
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^index/', your_view, name = 'index'),
]
You don't reference templates from your urls.py. That is for matching routes up to view functions, and it is the view's job to tell Django to render a template, if appropriate. You already have a simple view that renders index.html, you just need to reference it in your urlconf.
If you want your index.html rendered at the root of your site, then you need to add this line to your urls.py (EDIT: fixed the route):
url(r'^$', index_file)
You will also need from mysite.views import index_file at the top of your urlconf.

Categories