Django does not find the Urls.py in the app - python

I've been working with Django for 3 months but I haven't had anything like that until now. my thoughts are by '' at the beginning of the urlspattern to make the home template the main template
main urls.py
from django.contrib import admin
from django.urls import path, include
urlpatterns = [
path('admin/', admin.site.urls),
path('', include('inventory.urls')),
app urls.py
from . import views
from django.urls import path
urlpatterns = [
path('', views.home, name='home'),
path('base/', views.BaseTemplateView.as_view(), name='base'),
# Menu Item URLs
path('menu/', views.MenuItemListView.as_view(), name='menu_item_list'),
path('menu/<int:pk>/', views.MenuItemDetailView.as_view(), name='menu_item_detail'),
views.py
def home(request):
return render(request, 'home.html')
class BaseTemplateView(TemplateView):
template_name = 'base.html'
class MenuItemListView(ListView):
model = MenuItem
template_name = 'menu_item_list.html'
context_object_name = 'menu_items'
class MenuItemDetailView(DetailView):
model = MenuItem
template_name = 'menu_item_detail.html'
settings.py
ROOT_URLCONF = 'djangodelights.urls'
TEMPLATE_DIR = os.path.join(BASE_DIR,"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',
],
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'inventory.apps.InventoryConfig',
I was expecting ,when a start the server e get the home.html template.no matter which path I enter manually, I always get the same message
Using the URLconf defined in djangodelights.urls, Django tried these URL patterns, in this order:
admin/
The current path, home, didn’t match any of these.

Related

The current path, login/, didn’t match any of these

I have one app on my project which named "bookstore".
http://127.0.0.1:8000/login/ is rising an error that the current path, login/, didn't match any of these.
In addition that the urls were working properly the last time I was working on my project and I didn't do any changes to my code.
I'm using Django version 3.2.2
The bookstore/urls.py file
from django.urls import path
from . import views
urlpatterns = [
path('', views.home),
path('login/', views.login),
path('about/', views.about)
]
The main urls.py file
from django.contrib import admin
from django.urls import path, include
urlpatterns = [
path('admin/', admin.site.urls),
path('', include('bookstore.urls'))
]
The views.py file
from django.shortcuts import render
from django.http import HttpResponse
def home(request):
return render(request, "bookstore/dashboard.html")
def login(request):
return render(request, "bookstore/login.html")
def about(request):
return HttpResponse("About Us")
The settings.py file
DEBUG = True
ALLOWED_HOSTS = []
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'bookstore'
]
MIDDLEWARE = [
'django.middleware.security.SecurityMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
]
ROOT_URLCONF = 'blog.urls'
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',
],
},
},
]
WSGI_APPLICATION = 'blog.wsgi.application'

Django is loading template from the wrong app

I have a view under my elearning app named home(), which should load index.html from within the app's directory. Instead it loads an instance of index.html from a different app (symposium/templates/index.html). It should be loading it from (elearning/templates/index.html).
Can someone please explain why this is happening and how to fix it?
# Settings.py
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
# Machina dependencies:
'mptt',
'haystack',
'widget_tweaks',
# Machina apps:
'machina',
'machina.apps.forum',
'machina.apps.forum_conversation',
'machina.apps.forum_conversation.forum_attachments',
'machina.apps.forum_conversation.forum_polls',
'machina.apps.forum_feeds',
'machina.apps.forum_moderation',
'machina.apps.forum_search',
'machina.apps.forum_tracking',
'machina.apps.forum_member',
'machina.apps.forum_permission',
# SCORM apps:
'accounts',
'symposium',
'elearning']
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [os.path.join(BASE_DIR,'templates'), MACHINA_MAIN_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',
'machina.core.context_processors.metadata',
],
},
},
]
# root urls.py
from django.contrib import admin
from django.urls import path, include
from machina import urls as machina_urls
from accounts.views import CreateUser
from django.conf.urls.static import static
from django.conf import settings
urlpatterns = [
path('forum/',include(machina_urls)),
path('admin/', admin.site.urls),
path('createuser',CreateUser.as_view()),
path('symposium/', include('symposium.urls')),
path('elearning/', include('elearning.urls')),
]
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
# elearning app urls.py
from django.urls import include, path
from . import views
app_name = 'elearning'
urlpatterns = [
path('', views.home, name='home'),
]
# app: elearning/views.py
from django.shortcuts import render
# Create your views here.
def home(request):
return render(request,'index.html',{})
# index.html location
elearning templates : ./elearning/templates/index.html
Django searches for a templates folder within each app for the relevant template to render. Looks like the first it found was index.html from within the symposium app.
The convention of directory structure for templates is to put your templates within another folder with the name of the app in order to avoid this exact situation with templates of the same name.
Move your index.html file from
/elearning/templates/index.html
to
/elearning/templates/elearning/index.html
and then change your home view function to
def home(request):
return render(request,'elearning/index.html',{})
and similarly do the same for the location of the index.html file in the symposium app.

Template file not found Django

I am trying to make a home page of a new website via Django.
My app name is 'blog', home page is home.html
I still receive the error template does not exist when I go to http://127.0.0.1:8000/blog/home/
I made sure I added 'blog' to my templates in settings.py and that I added the folder templates in the main directory as well as through blog/templates/blog/home.html
myproject/blog/views.py
from django.shortcuts import render
from django.http import HttpResponse
def home(request):
return render(request, 'blog/home.html')
myproject/blog/urls.py
from django.urls import path
from . import views
urlpatterns = [
path('home/', views.home, name='home'),
]
myproject/settings.py
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'blog',
]
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [
os.path.join(BASE_DIR, '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',
],
myproject/urls.py
from django.contrib import admin
from django.urls import path, include
urlpatterns = [
path('admin/', admin.site.urls),
path('blog/', include('blog.urls')),
]
Do you see anything in my code which causes a problem? I receive the message in blog/views.py that "Template file 'blog' not found" on the line
return render(request, 'blog/home.html')
You are doing it wrong. You need to read the Django documentation carefully and try to understand whatever you read and implement the same step by step.
The url you have to hit is
http://127.0.0.1:8000/blog/home/
home.html will be rendered at this url.
You don't put html page name in the url
I have been looking for answers too and I tried everything but this worked for me on my windows machine. Adding 'r' before "templates" in os.path.join(BASE_DIR, 'templates') to look like this os.path.join(BASE_DIR, r'templates') resolved the error issue. Also my templates directory was in my project root along side the parent app and sub-apps.

ModuleNotFoundError: No module named 'users' --showing in django

My directory
MLG
>.vscode
>blog
>users
-db.sqlite3
-manage.py
This is users views.py
from django.shortcuts import render
from django.contrib.auth.forms import UserCreationForm
def register(request):
form = UserCreationForm()
return render(request, 'users/regsiter.html', {'form': form})
This is users apps.py
from django.apps import AppConfig
class UsersConfig(AppConfig):
name = 'users'
This is mlg settings.py
INSTALLED_APPS = [
'blog.apps.BlogConfig',
'users.apps.UsersConfig',
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
]
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',
],
},
},
]
This is mlg urls.py
from django.contrib import admin
from django.urls import path, include
from users import views as user_views
urlpatterns = [
path('admin/', admin.site.urls),
path('register/', user_views.register, name='register'),
path('', include('blog.urls')),
]
What i am doing wrong please help...
P.S. - I have shown templates just as i am trying to run register.html file .. cant able to run it bcz of above error ..

I want to load a template from the templates folder but I get an error that says the included URLconf does not appear to have any patterns in it

I am trying to load a template in my templates/pages folder and get the error: django.core.exceptions.ImproperlyConfigured: The included URLconf '<module 'pages.urls' from 'D:\\django\\pages\\pages\\urls.py'>' does not appear to have any patterns in it. If you see valid patterns in the file then the issue is probably caused by a circular import.
I have tried putting the templates folder in both the project and the app directory but still get the same error.
In my settings.py I have:
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [os.path.join(BASE_DIR, '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:
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'pages',
]
my urls.py file in the root project folder named pages_project looks like:
from django.contrib import admin
from django.urls import path, include
urlpatterns = [
path('admin/', admin.site.urls),
path('', include('pages.urls')),
]
and my urls.py in my app folder named pages looks like:
from django.urls import path
from . import views
path('', views.HomePageView.as_view(), name='home')
my views.py looks like:
from django.shortcuts import render
from django.views.generic import TemplateView
class HomePageView(TemplateView):
template_name= 'home.html'
I have a template file named home.html in the path pages/templates/pages/home.html and looks like:
<h1>Homepage</h1>
This doesn't have anything to do with templates.
As the error says, the included URLconf doesn't have any patterns in it. As you can see from the main urls.py, you need to define a list named urlpatterns which contains your patterns. So your pages urls.py should be:
urlpatterns = [
path('', views.HomePageView.as_view(), name='home')
]

Categories