Cant render HTML file on Django - python

Django 4.0.3 and Python 3.10.2
I cant render a html file on my project. What am i missing? Main parts of code below.
Settings.py at INSTALLED_APPS:
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'app1',
]
Settings.py at TEMPLATES:
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'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',
],
},
# 'DIRS': [r'project1\app1\templates\app1'],
'DIRS': [os.path.join(BASE_DIR, 'templates')],
},
]
Project-Urls:
from django.contrib import admin
from django.urls import include, path
urlpatterns = [
path('admin/', admin.site.urls),
path('', include('app1.urls')), # main will be the name of your app
]
App1-Urls:
from django.urls import path
from . import views
urlpatterns = [
path('', views.simple_function, name='simple_function'),
]
Views:
def simple_function(request):
print("Print executes correctly, but the render doesn't")
return render(request, r'project1\app1\templates\app1\home.html')
Html file path: app1/templates/app1/home.html
Github of this project
Had to post this bunch of information to clarify.

instead of
return render(request, r'project1\app1\templates\app1\home.html')
try to write
return render(request, 'app1/home.html')

Related

I am getting Template Does Not Exist at / when I'm running my server in Django

When I run server I get Template Does Not Exist at / for my index.html page. Same problem occurs for other templates as well.
I've tried the solutions for previously asked similar questions like checking the template directory and installed apps in settings.py file but nothing seems working.
Also, when I create a new project folder at some other location and copy all the code there, it usually works.
This is my folder tree:
I'm currently learning Django and new at stack overflow. Any help would be appreciated?
Here is my urls.py code for project folder:
from django.contrib import admin
from django.urls import path
from django.conf.urls import url,include
from basic_app import views
urlpatterns = [
url(r'^$',views.index,name='index'),
path('admin/', admin.site.urls),
url(r'basic_app/',include('basic_app.urls')),
]
Here is my urls.py file under basic_app:
from django.conf.urls import url
from basic_app import views
app_name= 'basic_app'
urlpatterns=[
url(r'^register/$',views.register,name='register')
]
Here is my views.py file code:
from django.shortcuts import render
from basic_app.forms import UserForm, UserProfileInfoForm
def index(request):
return render(request,'basic_app/index.html')
def register(request):
registered= False
if request.method=='POST':
user_form= UserForm(data= request.POST)
profile_form= UserProfileInfoForm(data= request.POST)
if user_form.is_valid() and profile_form.is_valid():
user= user_form.save()
user.set_password(user.password)
user.save()
profile= profile_form.save(commit=False)
profile.user=user
if 'profile_pic' in request.FILES:
profile.profile_pic= request.FILES['profile_pics']
profile.save()
registered= True
else:
print(user_form.errors ,profile_form.errors)
else:
user_form= UserForm()
profile_form= UserProfileInfoForm()
return render(request,'basic_app/registration.html',{'user_form':user_form,'profile_form':profile_form,
'registered':registered})
Here is my settings.py file code for templates:
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
TEMPLATE_DIR=os.path.join(BASE_DIR,'templates')
STATIC_DIR=os.path.join(BASE_DIR,'static')
MEDIA_DIR=os.path.join(BASE_DIR,'media')
This is my Installed app list:
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'basic_app',
]
Also this:
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',
],
},
},
]
I think in admin just do 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 view should be something like this
return render(request,'basic_app/registration.html',{'user_form':user_form,'profile_form':profile_form,
'registered':registered})
hope this helps
In your settings.py check the directory pointed by TEMPLATE_DIR so that you can configure where exactly it is ponting to.
print(TEMPLATE_DIR)
Then in views.py:
def index(request):
return render(request,'folder/my_html_file.html')
So django renders the template from.
TEMPLATE_DIR + "folder/my_html_file.html"
If you are using the default configuration settings, the templates are supposed to be in learning_users/basic_app/templates/basic_app/ and accessed like the following:
return render(request, `basic_app/index.html`, {})
where index.html is your template name.
The directory structure here may seem a little redundant at first but you can also change it using the settings.py configuration file.
In your directory, the templates folder is placed directly inside the project folder (i.e. learning_users). Unless you have modified the configuration files to change the templates' path, this would result in an Error.
but perhaps you can do this sure it works in your settings.py file add this line to the installed apps
'basic_app.apps.Basic_appConfig'

Using iframe in Django, keep getting 'TemplateDoesNotExist'

I am using iFrame with Django 2.0.13. I keep getting TemplateDoesNotExist error, and I don't see what I'm missing.
I've looked at other answers here on StackOverFlow and I seem to be doing everything.
settings.py
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
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',
],
},
},
]
urls.py
from django.contrib import admin
from django.urls import path, include
from django.views.generic import TemplateView
from NatureApp import views
urlpatterns = [
path('', views.index, name="index"),
path('NatureApp/', include('NatureApp.urls')),
path('admin/', admin.site.urls),
path(r'Map2.html', TemplateView.as_view(template_name="Map2.html"), name='Map2'),
]
views.py
from django.shortcuts import render
from django.http import HttpResponse
# Create your views here.
# def index(request):
# return HttpResponse("Hello World!")
def index(request):
my_dict = {'insert_me':"Hello I am from views.py!"}
return render(request,'NatureApp/index.html',context=my_dict)
Map2.html should being showing. I see index.html fine, but inside the IFrame I see the TemplateDoesNotExist message.
I'm new to Django, I'm trying to include all code needed for troubleshooting.
Try creating Map2 view in views.py. There could be problem with TemplateView.as_view()

Django Social Auth issue with Coinbase

Im using https://github.com/python-social-auth/social-app-django
to make it easy to add new oauths to my django app.
I have added it to my settings files and filled in the keys.
urls.py
urlpatterns = [
path('', include('home.urls')),
path('admin/', admin.site.urls),
path('auth/', include('social_django.urls', namespace='social')),
]
settings.py
SOCIAL_AUTH_COINBASE_KEY = os.environ.get('COINBASE_ID')
SOCIAL_AUTH_COINBASE_SECRET = os.environ.get('COINBASE_SECRET')
AUTHENTICATION_BACKENDS = (
'social_core.backends.open_id.OpenIdAuth', # for Google authentication
'social_core.backends.google.GoogleOpenId', # for Google authentication
'social_core.backends.google.GoogleOAuth2', # for Google authentication
'social_core.backends.coinbase.CoinbaseOAuth2', # for Coinbase authentication
'django.contrib.auth.backends.ModelBackend',
)
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',
'social_django.context_processors.backends', # <- Here
'social_django.context_processors.login_redirect', # <- Here
],
},
},
]
INSTALLED_APPS = [
'user.apps.UserConfig',
'django.contrib.humanize',
'social_django',
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
]
However when I see the oauth screen of coinbase, I press next, I get redirected to my redirect uri,
Then it gives me an error
HTTPError at /auth/complete/coinbase/
403 Client Error: Forbidden for url: https://api.coinbase.com/v2/user
I have been trying to fix this issue for hours now but I cant seem to find anything on this

django-admin-tools 0.8 after installing and configuring shows error 404 while trying to open admin page

I had installed and configured django-admin-tools 0.8 following the documentation. Django version is 1.11.4, using virtualenv in project.
File settings.py:
INSTALLED_APPS = [
'admin_tools',
'admin_tools.theming',
'admin_tools.menu',
'admin_tools.dashboard',
'django.contrib.auth',
'django.contrib.sites',
'django.contrib.admin',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'atelierapp',
]
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',
'django.core.context_processors.request',
],
'loaders': [
'django.template.loaders.filesystem.Loader',
'django.template.loaders.app_directories.Loader',
'admin_tools.template_loaders.Loader',
]
},
},
]
File urls.py:
urlpatterns = [
#url(r'^admin/', admin.site.urls),
url(r'^admin_tools/', include('admin_tools.urls')),
]
But when I try to enter the admin panel, I get a 404 error.
Screenshot
What is the problem I'm not seeing?
Thank you for your time.
According to the documentation
Prerequisite
In order to use django-admin-tools you obviously need to have configured your Django admin site. If you didn’t, please refer to the relevant django documentation.
However I noticed in your urls that you have:
#url(r'^admin/', admin.site.urls),
Have you tried removing the # tag?

Templates Doesnot exist at /accounts/login

I am using django-registraion for authentication of users. I did install django-registration via pip and added in the settings.py file. However it is still giving me the Templtate doesnot exist error. Here's the error:
TemplateDoesNotExist at /accounts/login/
registration/login.html
Request Method: GET
Request URL: http://127.0.0.1:8000/accounts/login/?next=/
Django Version: 1.11.3
Exception Type: TemplateDoesNotExist
Exception Value: registration/login.html
Exception Location: C:\Python34\lib\site- packages\django\template\loader.py in select_template, line 53
Here's the code:
from django.conf.urls import url
from django.contrib import admin
from django.contrib.auth.views import login, logout
from chat.views import index
from django.conf.urls import include
urlpatterns = [
url('^', include('django.contrib.auth.urls')),
url(r'^admin/', admin.site.urls),
url(r'^$',index, name='homepage'), # The start point for index view
url(r'^accounts/login/$', login, name='login'), # The base django login view
url(r'^accounts/logout/$', logout, name='logout'), # The base django logout view
in the settings.py file:
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.humanize',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'chat',
'registration',
]
This is the structure of my django project.
it seems the templates folder is at the root directory, so you need to change your settings to this
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [
BASE_DIR + '/templates/',
],
'APP_DIRS': True,
'OPTIONS': {
'context_processors': [
# Insert your TEMPLATE_CONTEXT_PROCESSORS here or use this
# list of context processors
],
'debug': True
},
},
]

Categories

Resources