Error: Template Does Not Exist - python

I have two apps in one project the main apps call pages contain Home and About page the second apps is contact page.
Here is my contact/urls.py in contact apps:
from .views import ContactFormView
urlpatterns = patterns('',
# URL pattern for the ContactView
url(regex=r'^contact/$', view=ContactFormView.as_view(), name='contact'),
)
the main apps pages/urls.py :
from django.conf.urls import patterns, url
from .views import AboutPageView, HomePageView
urlpatterns = patterns('',
url(regex=r'^$', view=HomePageView.as_view(), name='home'),
url(regex=r'^about/$', view=AboutPageView.as_view(), name='about'),
)
in main apps pages/views.py :
from vanilla import TemplateView
class HomePageView(TemplateView):
template_name = "pages/home.jade"
class AboutPageView(TemplateView):
template_name = "pages/about.jade"
class ContactFormView(TemplateView):
template_name = "contact/email_form.jade"
I get this error say django.template.base.TemplateDoesNotExist
TemplateDoesNotExist: /contact/email_form.jade
I am not sure why, the setting file is OK I can open the main page with the contact page in the menu nav when I click contact I get the error above. Any helps appreciate
Thanks

You should have the email_form.jade file inside /templates/contact. Or place it in /templates/pages and change the line to template_name = "pages/email_form.jade".

Check to make sure you don't have the any of the parent directories the template is in marked as static in app.yaml.
App Engine stores and serves static files separately from application files. Static files are not available in the application's file system.
If you do either find a separate directory to place static files or you can optionally use
application_readable: True in the configuration block for static files which allows the filesystem to read them.
See here for more details

Related

Why is my urls.py file not being detected?

I just created a new app for my django project and have created a urls.py, models.py, views.py, and serializers.py. However, my project is not detecting the URLs in the urls.py file. If I go to a different app in my project and edit the urls.py file, the system detects it. However, it will not detect it in the new one. Here is the urls.py file:
from django.conf.urls import url
from . import views
urlpatterns = [
url(r'^knorket-kaitongo-integration/$', views.KnorketKaitongoIntegration.as_view(),
name='knorket_kaitongo_integration'),
]
Does anyone know why this wouldn't be detected? It's not showing up on the API screen either.
Django does not autodetect any files
You need to install them using the installed_apps list and register URLs.
In the Root application, you should find a urls.py file, in that file you need to register your URLs.
You need to import application URLs in Root urls.py and add them in something like this.
urlpatterns = [
path('^', include(urls))
]

What is the url.py file that opens home using wagtail?

I'm starting using Wagtail + Django.
Generally, in Django you set the urls path in a urls.py file.
However, I cannot find the url.py that says to my app, when users visits main domain (local http://127.0.0.1:8000/) show home_page.html view.
I'm following the get_started tutorial.
And used this command to generate the basic apps:
wagtail start elim
This generated: a) elim, b) home, c) search apps.
Only elim app contains a urls.py, but it doesn't set a path for home:
from django.conf import settings
from django.conf.urls import include, url
from django.contrib import admin
from wagtail.admin import urls as wagtailadmin_urls
from wagtail.core import urls as wagtail_urls
from wagtail.documents import urls as wagtaildocs_urls
from search import views as search_views
urlpatterns = [
url(r'^django-admin/', admin.site.urls),
url(r'^admin/', include(wagtailadmin_urls)),
url(r'^documents/', include(wagtaildocs_urls)),
url(r'^search/$', search_views.search, name='search'),
]
This is my project structure:
So how does going to "/" open the home page???
The home dir is served by wagtail internal mechanism. Scroll to the end of the file elim/urls.py to find this :
urlpatterns = urlpatterns + [
# For anything not caught by a more specific rule above, hand over to
# Wagtail's page serving mechanism. This should be the last pattern in
# the list:
url(r"", include(wagtail_urls)),
# Alternatively, if you want Wagtail pages to be served from a subpath
# of your site, rather than the site root:
# url(r"^pages/", include(wagtail_urls)),
]
So, continue to read the tutorial, I'm sure you will soon or later discover the Page models and everything provided by wagtail.
Try adding a view function in your app's views.py file that renders home_page.html when the user goes to "/".
in views.py write this:
def home_page(response):
return render(response, "home/home_page.html")
then map this to ur urs.py file
url_patterns = [
path("", views.home_page)
]
then add this url to your url conf

GET http://localhost:8000/add/ Page not found

I've been working on this application using my localhost:8000 for a while now and everything had been working smoothly. However now that I have tried to add a new url: /add/. For some reason it doesn't recognise the URL. I believe maybe there's something wrong with how I've wrote my code but I haven't quite found it. Any help would be great!
To provide context I started of in my urls.py file where I created a new path: path('add', views.ProjectCreateView.as_view(), name='add'),
I then moved over to my views.py file and imported the CreateView as so:
from django.views.generic import CreateView.
From there I then created a class for the view:
class ProjectCreateView(CreateView):
model = Project
template_name = 'budget/add-project.html'
fields = ('name', 'budget')
Following this I then created another file within my budget folder nested in my templates folder. The file name add-project.html. I don't think there's anything wrong with this file but just to guarantee this is how I linked my html file:
{% extends 'budget/base.html' %}
{% load static %}
{% block content %}
This is the exact message I get when I run http://localhost:8000/add/
"No Project matches the given query."
ULRS.PY in main working folder:
from django.contrib import admin
from django.urls import path
from . import views
urlpatterns = [
path('', views.project_list, name='list'),
path('add', views.ProjectCreateView.as_view(), name='add'),
path('<slug:project_slug>/', views.project_detail, name='detail')
]
URLS.PY in subfolder:
from django.urls import path, include
from django.contrib import admin
urlpatterns = [
path('admin/', admin.site.urls),
path('',include('budget.urls'))
]
When you request /add/ with the trailing slash, this does not match the add path but does match the <slug:project_slug>/ path, so it tries your project_detail view with project_slug set to "add".

how to move the template?

please tell me how to move the view (view.py) in the directory "views".
Now the structure of my folders and files is as follows:
proj1(catalog)
views.py(file)
manage.py(file)
proj1(catalog)
wsgi.py(file)
urls.py(file)
settings.py(file)
__init__.py(file)
views(catalog)
urls.py following content:
from django.conf.urls import patterns, include, url
import views
urlpatterns = patterns('',
url('^$', views.hello),
url('^datetime$', views.current_datetime),
url('^dt$', views.current_datetime),
url('^dt/(\d{0,2})$', views.current_datetime2),
)
I need to file located in the directory view.py proj1/proj1 /.
wherein static pages is still made ​​available to the browser.
the problem is that I do not know how to change the code in the files: urls.py, settings.py
There is no need to change views, just do in settings.py
TEMPLATE_DIRS = (
'/path/to/proj1/proj1/views/', # or another path with templates
'django/contrib/admin/templates/'
)

"admin" Subdirectory Triggering (unintentially) Django Admin Pages

I am having trouble with some custom admin pages triggering the Django admin site instead of displaying my custom pages.
My urls.py follows:
urlpatterns = patterns('',
# ... trimmed ...
# Admin pages
url(r'^admin/doc/', include('django.contrib.admindocs.urls')),
url(r'^admin/', include(admin.site.urls)),
# Lobby Visitor Log
url(r'^visitorLog', include('lobbyVisitorLog.urls')),
)
In my lobbyVisitorLog app I have the following directory structure, leading to "admin" pages
lobbyVisitorLog
- templates
- admin
And my lobbyVisitorLog/urls.py follows:
urlpatterns = patterns('visitorLog.views',
url(r'^/$', views.home, name='homeView'),
url(r'^/search', views.search, name='searchView'),
url(r'^/submit', views.submit, name='submitView'),
url(r'^/admin/$', views.adminView, name='adminView'),
url(r'^/admin/import/$', views.adminImportView, name='adminImportView'),
url(r'^/(?P<guest_type>\w+)$', views.logEntry, name='logEntryView'),
)
The views.py for the admin index page looks like this:
def adminView(request):
return render(request, 'admin/index.html', {}, context_instance=RequestContext(request))
When I go to "mysite/visitorLog/admin/" I get the Django admin site with the following message: “You don't have permission to edit anything.”
However, if I change my "admin" directory to "utils" (or whatever else, other then "admin") and update my views.py accordingly, everything appears as expected! This is okay, I can deal with my directory being called "utils" but it will annoy me... just enough.
What is happening that is causing the Django admin page to load instead of my custom pages?
By default django first checks each of the paths you have in TEMPLATE_DIRS for 'admin/index.html'. If it doesn't find it there, it starts searching in the templates directory for each app in the INSTALLED_APPS setting.
If 'django.contrib.admin' is listed first in INSTALLED_APPS, it will use the identically named 'admin/index.html' template from the django.contrib.admin app.
Moving 'django.contrib.admin' to the last position in INSTALLED_APPS should allow it to find 'admin/index.html' in your lobbyVisitorLog app first, but this will break the admin site by cause it to use 'admin/index.html' from your app, lobbyVisitorLog.
A good way to solve this is to always have a sub-directory named after your app within your app's templates directory. For example:
lobbyVisitorLog
- templates
- lobbyVisitorLog
- admin
- index.html
and then update your view's template path:
def adminView(request):
return render(request, 'lobbyVisitorLog/admin/index.html', {}, context_instance=RequestContext(request))
You can find more on how Django loads templates here

Categories