I am getting 'TemplateDoesNotExist' error using Django Version 2.2.3 - django

I'm learning Django and building a page. I get the TemplateDoesNotExist error. I don't know how to fix this, But I don't know which parts of the code to show.
TemplateDoesNotExist at /
Leaning_logs/base.html
Request Method: GET
Request URL: http://localhost:8000/
Django Version: 2.2.3
Exception Type: TemplateDoesNotExist
Exception Value:
Leaning_logs/base.html
Exception Location: D:\learning-note\ll_env\lib\site-packages\django\template\backends\django.py in reraise, line 84
Python Executable: D:\learning-note\ll_env\Scripts\python.exe
Python Version: 3.7.3
Python Path:
['D:\\learning-note',
'C:\\Users\\qingting_bailihua\\AppData\\Local\\Programs\\Python\\Python37\\python37.zip',
'C:\\Users\\qingting_bailihua\\AppData\\Local\\Programs\\Python\\Python37\\DLLs',
'C:\\Users\\qingting_bailihua\\AppData\\Local\\Programs\\Python\\Python37\\lib',
'C:\\Users\\qingting_bailihua\\AppData\\Local\\Programs\\Python\\Python37',
'D:\\learning-note\\ll_env',
'D:\\learning-note\\ll_env\\lib\\site-packages']
Server time: Thu, 1 Aug 2019 10:45:39 +0000

myapp/views.py
class ContractsView(TemplateView):
def get(self, request):
return render(request, 'yourPage.html')
In the urls.py in your project folder add the url to your view.py class from application folder.
project/urls.py
urlpatterns = [
.
path('contractList/', ContractsView.as_view(), name='contractsList'),
.
]
If I did not manage to help you. Here you will find more answers:
Django TemplateDoesNotExist?

Related

AttributeError at /BRMapp/view-books/

I am developing Book Record Management system as my first project on django but after performing all my functionalities I got a problem in my views.py file in a function name view-book in a line(books=models.Book.objects.all()).Although i have made a class name 'Book' in models.py and i also have imported models in my views.py file with a line(form django.db import models).Although it is not working.Kindly help
Error showing like this:::
module 'django.db.models' has no attribute 'Book'
Request Method: GET
Request URL: http://localhost:8000/BRMapp/view-books/
Django Version: 3.2.5
Exception Type: AttributeError
Exception Value:
module 'django.db.models' has no attribute 'Book'
Exception Location: G:\Django projects\firstproject\BRMapp\views.py, line 41, in viewBooks
Python Executable: C:\Users\amann\AppData\Local\Programs\Python\Python39\python.exe
Python Version: 3.9.2
Python Path:
['G:\\Django projects\\firstproject',
'C:\\Users\\amann\\AppData\\Local\\Programs\\Python\\Python39\\python39.zip',
'C:\\Users\\amann\\AppData\\Local\\Programs\\Python\\Python39\\DLLs',
'C:\\Users\\amann\\AppData\\Local\\Programs\\Python\\Python39\\lib',
'C:\\Users\\amann\\AppData\\Local\\Programs\\Python\\Python39',
'C:\\Users\\amann\\AppData\\Roaming\\Python\\Python39\\site-packages',
'C:\\Users\\amann\\AppData\\Local\\Programs\\Python\\Python39\\lib\\site-packages']
Server time: Thu, 12 Aug 2021 10:19:08 +0000
Based on the exception models points to the models submodule of the django.db module, but there is no Book defined in that module.
Likely the Book originates from elsewhere, from an appname.models module.
You thus import this with:
from appname.models import Book
# …
def viewBooks(request):
# …
# ↓ no models.
books = Book.objects.all()
# …

Reverse for 'xyz' not found. 'chat' is not a valid view function or pattern name. At redirect('xyz')

I am getting an error while using the path in Django.
this is the code where the redirect function is called in views.py
def addmsg(request):
c = request.POST['content']
new_item = messageItem(content = c)
new_item.save()
bot_msg(c)
return redirect('chatapp:chat')
urls.py
app_name = 'chatapp'
urlpatterns = [
path('chat', views.chatbot, name='chat'),
path('addmsg',views.addmsg, name='addmsg'),
]
Django error:
NoReverseMatch at /addmsg
Reverse for 'chat' not found. 'chat' is not a valid view function or pattern name.
Request Method: POST
Request URL: http://localhost:8000/addmsg
Django Version: 2.2.17
Exception Type: NoReverseMatch
Exception Value:
Reverse for 'chat' not found. 'chat' is not a valid view function or pattern name.
Exception Location: C:\Users\xyz\Documents\Python\hotel\hotel2\env\lib\site-packages\django\urls\resolvers.py in _reverse_with_prefix, line 673
Python Executable: C:\Users\xyz\Documents\Python\hotel\hotel2\env\Scripts\python.exe
Python Version: 3.9.1
Python Path:
['C:\\Users\\xyz\\Documents\\Python\\hotel\\hotel2',
'C:\\Users\\xyz\\AppData\\Local\\Programs\\Python\\Python39\\python39.zip',
'C:\\Users\\xyz\\AppData\\Local\\Programs\\Python\\Python39\\DLLs',
'C:\\Users\\xyz\\AppData\\Local\\Programs\\Python\\Python39\\lib',
'C:\\Users\\xyz\\AppData\\Local\\Programs\\Python\\Python39',
'C:\\Users\\xyz\\Documents\\Python\\hotel\\hotel2\\env',
'C:\\Users\\xyz\\Documents\\Python\\hotel\\hotel2\\env\\lib\\site-packages']
Server time: Mon, 1 Feb 2021 18:26:38 +0000
I have also registered the app name in settings.py
Change the redirect('chatapp:chat') to redirect('chat').
Explanation:
The redirect function would search for the name of the URL in urls.py and in your case it's
path('chat', views.chatbot, name='chat'),

from django.urls import ( # noqa ModuleNotFoundError: No module named 'django.urls' [duplicate]

I'm using django==1.8, rest_framework=3.7.7, python==2.7.12
urls.py
urlpatterns += [
url(r'^api/core/', include('core.urls')),
]
core/urls.py
urlpatterns=[
url(r'^/users/', core_view.userlist),
]
views.py
class UserList(generics.ListAPIView):
queryset = User.objects.all()
serializer_class = UserSerializer
userlist = UserList.as_view()
When I'm navagating to: http://localhost:8000/api/core/users I'm getting the following error:
ImportError at /api/core/users
No module named urls
Request Method: GET
Request URL: http://localhost:8000/api/core/users
Django Version: 1.8
Exception Type: ImportError
Exception Value:
No module named urls
Exception Location: /usr/local/lib/python2.7/dist-packages/rest_framework/compat.py in <module>, line 26
Python Executable: /usr/bin/python
Python Version: 2.7.12
what is wrong in configuration?
Django Rest Framework dropped support for Django 1.8 in version 3.7.
You should install an earlier version of rest framework, or upgrade Django (note that Django 1.8 reaches end-of-life in April 2018)
Just Make sure you include core app in your INSTALLED_APPS settings, and that you have init.py in your core directory folder.
Look here for more information.

Using django name 'HttpResponse' is not defined

So brand new to Python, trying to get past this django error:
NameError at /hello/
name 'HttpResponse' is not defined
Request Method:
GET
Request URL:
http://localhost:61892/hello/
Django Version:
1.11.7
Exception Type:
NameError
Exception Value:
name 'HttpResponse' is not defined
Exception Location:
c:\users\mblaylock\source\repos\mysite\mysite\mysite\views.py in hello, line 6
Python Executable:
c:\users\mblaylock\source\repos\mysite\mysite\env\Scripts\python.exe
Python Version:
3.6.2
Python Path:
['c:\\users\\mblaylock\\source\\repos\\mysite\\mysite',
'c:\\users\\mblaylock\\source\\repos\\mysite\\mysite',
'c:\\users\\mblaylock\\source\\repos\\mysite\\mysite\\env\\Scripts\\python36.zip',
'C:\\Program Files\\Python36\\DLLs',
'C:\\Program Files\\Python36\\lib',
'C:\\Program Files\\Python36',
'c:\\users\\mblaylock\\source\\repos\\mysite\\mysite\\env',
'c:\\users\\mblaylock\\source\\repos\\mysite\\mysite\\env\\lib\\site-packages']
Server time:
Mon, 13 Nov 2017 18:09:39 +0000
My code is:
class views(object):
"""description of class"""
from django.http import HttpResponse
def hello(request):
return HttpResponse("Hello world")
Using Visual Studio for my IDE on Windows 10, if that matters.
Thanks in advance!
There's no reason to have that class views line there. Remove it.
Best approach in this situation is use single method to handle this request(you don't need any class based views here) which shouldn't be inside any class:
from django.http import HttpResponse
def hello(request):
return HttpResponse("Hello world")
If you want to use class based views you need to remember that
when you are importing something in class body it can be inaccessible in method. You should insert import at the top of python file or directly in method.

django templatedoesnotexist current_date

I'm going through the Django Book. I'm in the middle of Chapter 4. I can't render my current_datetime.html template view. What am I doing wrong? It looks like the template loaders are not finding the right directory. http://www.djangobook.com/en/2.0/chapter04/
I have created my templates directory in my project folder and current_datetime.html with in the directory.
Setting.py
import os.path
TEMPLATE_DIRS = (
os.path.join(os.path.dirname(__file__), 'templates').replace('\\','/'),
# I've also tried: # '/Users/macuser/Python_Projects/django_test1/templates',
)
views.py
from django.shortcuts import render_to_response
import datetime
def current_datetime(request):
now = datetime.datetime.now()
return render_to_response('current_datetime.html', {'current_date': now})
urls.py
from django.conf.urls import patterns, include, url
from django_test1.views import hello, my_homepage_view, current_datetime, hours_ahead
urlpatterns = patterns('',
(r'^$', my_homepage_view),
(r'^hello/$', hello),
(r'^time/$', current_datetime),
(r'^time/plus/(\d{1,2})/$', hours_ahead),)
The error:
TemplateDoesNotExist at /time/
current_datetime.html
Request Method: GET
Request URL:
Django Version: 1.4
Exception Type: TemplateDoesNotExist
Exception Value:
current_datetime.html
Exception Location: /Library/Python/2.7/site-packages/django/template/loader.py in find_template, line 138
Python Executable: /usr/bin/python
Python Version: 2.7.1
Python Path:
['/Users/macuser/Python_Projects/django_test1',
'/Library/Python/2.7/site-packages/distribute-0.6.27-py2.7.egg',
'/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python27.zip',
'/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7',
'/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-darwin',
'/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-mac',
'/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-mac/lib-scriptpackages',
'/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python',
'/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-tk',
'/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-old',
'/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-dynload',
'/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/PyObjC',
'/Library/Python/2.7/site-packages']
Server time: Mon, 23 Jul 2012 20:40:35 -0500
Template-loader postmortem
Django tried loading these templates, in this order:
Using loader django.template.loaders.filesystem.Loader:
Using loader django.template.loaders.app_directories.Loader:
/Library/Python/2.7/site-packages/django/contrib/auth/templates/current_datetime.html (File does not exist)
You havn't checked render_to_Response format
https://docs.djangoproject.com/en/dev/topics/http/shortcuts/#render-to-response
Go through it, You need to add context_instance=RequestContext(request) to the response
This was a silly mistake. I did not create an actual app: python manage.py startapp "appName". I put "assert False" in the settings file and realized django was not even using my settings.py file.