Django templates folders - django

I'm experimenting with Django, and figuring out how to set urls.py, and how the URLs work.
I've configured urls.py in the root of the project, to directs to my blog and admin.
But now I want to add a page to my home, so at localhost:8000.
So I've added to following code to the urls.py in the root of the project:
from django.views.generic.simple import direct_to_template
urlpatterns = patterns('',
(r"^$", direct_to_template, {"template": "base.html"}),
)
The problem is that it searches for the template in blog/templates/...
Instead of the templates folder in my root. Which contains the base.html.
Full urls.py:
from django.conf.urls import patterns, include, url
from django.views.generic.simple import direct_to_template
# Uncomment the next two lines to enable the admin:
from django.contrib import admin
admin.autodiscover()
urlpatterns = patterns('',
(r"^$", direct_to_template, {"template": "base.html"}),
url(r'^blog/', include('hellodjango.blog.urls')),
url(r'^admin/doc/', include('django.contrib.admindocs.urls')),
url(r'^admin/', include(admin.site.urls)),
(r'^tinymce/', include('tinymce.urls')),
)
Am I overlooking something?

Did you set TEMPLATE_DIRS in your settings.py? Check and make sure it is set up correctly with absolute paths. This is how I make sure it is properly set:
settings.py
PROJECT_ROOT = os.path.abspath(os.path.dirname(__file__))
TEMPLATE_DIRS = (
# Put strings here, like "/home/html/django_templates" or "C:/www/django/templates".
# Always use forward slashes, even on Windows.
# Don't forget to use absolute paths, not relative paths.
os.path.join(PROJECT_ROOT, 'templates').replace('\\','/'),
)
# List of callables that know how to import templates from various sources.
TEMPLATE_LOADERS = (
'django.template.loaders.filesystem.Loader',
'django.template.loaders.app_directories.Loader',
# 'django.template.loaders.eggs.Loader',
)
This way, I have a templates folder in my project root that is used for non-app templates and each app has a templates/appname folder inside the app itself.
If you want to use a template from the root template folder, you just give the name of the template like 'base.html' and if you want to use an app template, you use 'appname/base.html'
Folder structure:
project/
appname/
templates/
appname/ <-- another folder with app name so 'appname/base.html' is from here
base.html
views.py
...
templates/ <-- root template folder so 'base.html' is from here
base.html
settings.py
views.py
...

from django.conf.urls import patterns, include, url
# Uncomment the next two lines to enable the admin:
from django.contrib import admin
admin.autodiscover()
urlpatterns = patterns('',
url(r'^blog/', include('hellodjango.blog.urls')),
url(r'^admin/doc/', include('django.contrib.admindocs.urls')),
url(r'^admin/', include(admin.site.urls)),
url(r'^tinymce/', include('tinymce.urls')),
)
urlpatterns += patterns(
'django.views.generic.simple',
(r'^', 'direct_to_template', {"template": "base.html"}),
)

I would re-organize the urls as such:
urlpatterns = patterns('',
(r'^admin/doc/', include('django.contrib.admindocs.urls')),
(r'^admin/', include(admin.site.urls)),
(r'^tinymce/', include('tinymce.urls')),
(r'^blog/', include('hellodjango.blog.urls')),
(r'^$', direct_to_template, {"template": "base.html"}),
)
Patterns are matched by their specificity, so I tend to put the more specific patterns first. Otherwise you might see some unexpected behavior. Give that a try, and if it's still loading a template from your blog on a request to /, we'll dig deeper.

I think it depends what you want your home page to be. If its simply a page with links off to other parts of your site then catherine's answer is a nice clean way.
If you want the root of your site to be your blog for example I would do this:
urlpatterns = patterns('',
# Django Admin
url(r'^admin/', include(admin.site.urls)),
# Tiny MCE Urls
url(r'^tinymce/', include('tinymce.urls')),
# Other App
url(r'^other/', include('projectname.other.urls', namespace='other')),
# Blog App
url(r'^', include('projectname.blog.urls', namespace='blog')),
)
Also don't forget to name space your url includes: https://docs.djangoproject.com/en/dev/topics/http/urls/#url-namespaces

you can refer the my solution
Django==3.2.5
https://stackoverflow.com/a/68420097/10852018

Related

Django website loads with empty root urls.py file

I know it's feels like elementary, and yet I can't come up with a clean solution based on doc only.
I have the following project structure (I omit files like models.py, forms.py for the purpose of keeping the question concise)
hello_world
hellow_world
urls.py
app_2
urls.py
app_3
urls.py
manage.py
urls.py
settings.py
As you see, my goal is to have a separate urls.py file for each app, and then assemble them into root urls.py (depicted at the same level as settings.py in the list above). The problem is that my root urls.py is EMPTY (!!!) now, and the site still loads the home page !!! What am I doing wrong ???
See the details below:
settings.py:
ROOT_URLCONF = 'urls'
hellow_world urls.py:
urlpatterns = [
url(r'^$', views.home , name = 'home'),
]
root urls.py - empty !
manage.py:
if __name__ == "__main__":
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "settings")
from django.core.management import execute_from_command_line
execute_from_command_line(sys.argv)
Use include() to include more urls:
# your main urls.py file
from django.conf.urls import include, url
urlpatterns = [
url(r'^myapp1/', include('myapp1.urls')),
url(r'^myapp2/', include('myapp2.urls')),
]
And:
# myapp1/urls.py
from django.conf.urls import url
from . import views
app_name = 'myapp1'
urlpatterns = [
url(r'^$', views.IndexView.as_view(), name='index'),
url(r'^(?P<pk>\d+)/$', views.DetailView.as_view(), name='detail'),
#...
]

Python-Django : ImportError at / : No module named urls

I have created Djnago project in eclipse. Unfortunately, i am facing issue when i run the project
ImportError at /
No module named urls
Here Error Page
http://dpaste.com/1499981/
Eclipse Project http://i1008.photobucket.com/albums/af204/shoaibshah01/Untitled_zps84f95b4f.jpg
urls.py Content
from django.conf.urls import patterns, include, url
from django.contrib import admin
admin.autodiscover()
urlpatterns = patterns('',
# Examples:
# url(r'^$', 'TestApp.views.home', name='home'),
# url(r'^blog/', include('blog.urls')),
url(r'^admin/', include(admin.site.urls)),
)
Try converting admin.site.urls to string url(r'^admin/', include('admin.site.urls'))
Most likely this is because your TestApp is not referenced in INSTALLED_APPS so Django have no idea it should process it.
Try to change your ROOT_URLCONF to 'urls' value in settings.py
ROOT_URLCONF = 'urls'
The link you provided for the error log is giving 404.
Perhaps you can try with the below code for admin in URLs.py
from django.contrib import admin
from django.urls import path
urlpatterns = [
path('admin/', admin.site.urls),
]
Everything is looking correct in settings.py.
If the above changes are not working, please share error page again.

Django: Can't get homepage to display correctly TemplateDoesNotExist at /

well I'm working on a new Django project and am having a real hard time getting the index.html page to display correctly. Any advice on what to change to get it to display correctly?
I'm getting the error
TemplateDoesNotExist at /
index.html
my settings for each file are below.
myprojectname/myprojectname/settings.py
import os
PROJECT_ROOT = os.path.dirname(os.path.abspath(__file__))
STATIC_ROOT = os.path.join(PROJECT_ROOT, 'templates/static')
TEMPLATE_DIRS = (
PROJECT_ROOT + '../templates'
)
myprojectname/myprojectname/urls.py
from django.conf.urls import patterns, include, url
# Uncomment the next two lines to enable the admin:
from django.contrib import admin
admin.autodiscover()
urlpatterns = patterns('',
url(r'^', 'apps.views.index'),
url(r'^admin/', include(admin.site.urls)),
)
myprojectname/apps/views.py
from django.shortcuts import render_to_response
def index(request):
return render_to_response('index.html', locals())
I'm new to Django too so don't hate! :) I'll gladly admit I'm a noob...
Set up your template directories:
PROJECT_DIR = os.path.dirname(__file__)
TEMPLATE_DIRS = (
os.path.join(PROJECT_DIR, 'templates'),
)
Your folder structure should be similar to:
project_name/
project_name/
settings.py
templates/
index.html
The folder structure is especially important!
And define your template loaders if you haven't already:
TEMPLATE_LOADERS = (
('django.template.loaders.cached.Loader', (
'django.template.loaders.filesystem.Loader',
'django.template.loaders.app_directories.Loader',
)),
)
I've wrapped the standard loaders with cached.Loader, which funnily enough just caches pre-compiled templates.
Also, while I'm here, fix your root URL:
urlpatterns = patterns('',
url(r'^$', 'apps.views.index'),
Note the extra $ terminating the end of the regex, otherwise this first URL will match every single URL, and none of the others will get a chance to match.
Try this..
TEMPLATE_DIRS = (
PROJECT_ROOT + '../templates/'/
)

Adding django-directupload to Django project

I was looking to try django-directupload in one of my Django projects, but I'm new to Django and having some trouble with installation. I'm following the installation instructions in the README. I installed django-directupload using sudo pip install django-directupload.
Here is my urls.py, which I don't think is right:
from django.conf.urls.defaults import patterns, include, url
from testproject import settings
import directupload
from django.contrib import admin
directupload.admin.patch_admin()
admin.autodiscover()
urlpatterns = patterns('testproject.database.views',
url(r'^(\d+)/$', 'test_view', name="test_page"),
url(r'^admin/doc/', include('django.contrib.admindocs.urls')),
url(r'^admin/', include(admin.site.urls)),
url(r'^directupload/', include(directupload.urls))
)
if settings.DEBUG:
urlpatterns += patterns('',
url(r'^media/(?P<path>.*)$', 'django.views.static.serve', {
'document_root': settings.MEDIA_ROOT,
}),
)
Edit:
So I've gained some insight into overriding admin templates (Thanks ilvar for the link). I copied and pasted the contrib/admin/templates/admin/change_form.html file into the project templates directory in the /admin/nameofmyapp/ subdirectory, and added {% load directupload_tags %}{% directupload_head %} below the rest of the load tags. When I go to the Django admin I get the exception: 'module' object has no attribute 'admin' on line 6 of urls.py.
https://docs.djangoproject.com/en/1.4/ref/contrib/admin/#overriding-admin-templates
urls.py should look like this:
from django.conf.urls.defaults import patterns, include, url
from testproject import settings
from directupload.admin import patch_admin
from django.contrib import admin
patch_admin()
admin.autodiscover()
urlpatterns = patterns('testproject.database.views',
url(r'^(\d+)/$', 'test_view', name="test_page"),
url(r'^admin/doc/', include('django.contrib.admindocs.urls')),
url(r'^admin/', include(admin.site.urls)),
url(r'^directupload/', include(directupload.urls))
)
if settings.DEBUG:
urlpatterns += patterns('',
url(r'^media/(?P<path>.*)$', 'django.views.static.serve', {
'document_root': settings.MEDIA_ROOT,
}),
)
My imports were incorrect.

how to include urlpatterns in django?

In my project I have one app which have its own urls.py like this
urlpatterns = patterns('',
(r'^(?P<language>\w+)/$', 'MainSite.views.home_page'),)
(above file is in my application )
I am trying include this file in main(project's) urls.py
like this :
from django.contrib import admin
admin.autodiscover()
urlpatterns = patterns('',
(r'', include('myproject.MainSite.urls')),
url(r'^admin/', include(admin.site.urls)),
)
if settings.DEBUG :
urlpatterns += patterns('',
(r'^media/(?P<path>.*)$', 'django.views.static.serve',
{'document_root': settings.MEDIA_ROOT}),
)
but after this I can able to call the MainSite's (app's) view but my admin url is not working
I tried
urlpatterns = patterns('',
(r'^$', include('myproject.MainSite.urls')),
url(r'^admin/', include(admin.site.urls)),
)
but after this this makes admin work but my app's view won't get called,
how do I solve this.
You're including your views at the root level. Since it comes before the urlpattern for the admin, the first urlpattern catches everything, so nothing is ever passed to the admin views.
The simplest fix is to simply reverse the order:
urlpatterns = patterns('',
url(r'^admin/', include(admin.site.urls)),
(r'', include('myproject.MainSite.urls')),
)
Then, your views will only catch anything the admin doesn't.