TemplateDoesNotExist at /hobbies/ - python

This question is the sequel of my previous question -
Reverse for 'hobbieswithCSS.html' not found. 'hobbieswithCSS.html' is not a valid view function or pattern name (solved)
I have moved from one error (in the title of previous question) to another, (in the title of this question) but hopefully this one will not be that much difficult to solve.
I have this anchor tag on my homepage -
My Hobbies
I have this view in my views.py file -
def hobbieswithCSS(request):
return render(request,'hobbieswithCSS.html')
And these are my urlpatterns -
urlpatterns = [
url(r'^$', views.index, name='index'),
url(r'^admin/', admin.site.urls),
url(r'^basic_app/',include('basic_app.urls')),
url(r'^logout/$',views.user_logout,name='logout'),
url(r'^hobbies/$', views.hobbieswithCSS, name='hobbieswithCSS'),
]

these will solve it for sure.
1.if your saved your templates in a separate templates by app name in templates directory you have to put the name of the sub-folder before the name of the template like this hobbies/hobbieswithCSS.html .
2. set the root of the template directory in your settings.py for project and templates.
BASE_DIR = Path(__file__).resolve().parent.parent
TEMPLATE_DIRS = (
os.path.join(SETTINGS_PATH, 'templates'),
)
also check this out Django TemplateDoesNotExist?

Change your views.py like this:
def hobbieswithCSS(request):
return render(request,'basic_app/hobbieswithCSS.html')

Related

django The current path,didn't match any of these

i want to try another apps in django but i got problem when access another apps.
Page not found
tree:
main:
search:
index.html
scrape.html
preprocessing:
index.html
the scenario like this. from scrape.html i want to access index.html in preprocessing but got an error path not found. I've done to add apps in settings.py
main url.py
urlpatterns = [
path('admin/', admin.site.urls),
path('', include('search.urls')),
path('preprocessing/', include('preprocessing.urls')),
]
search url.py
urlpatterns = [
path('', views.index,),
path('scrape/',views.scrape,),
]
slice of scrape.html:
Preprocessing
preprocessing url.py
path('', views.index),
let me know where did I go wrong, thx for your help
Your anchor tag is written as:
<a href = "/preprocessing" ...>
The problem here is that your url pattern is like 'preprocessing/', notice that it ends in a trailing slash while your anchors url doesn't. Furthermore in your settings you set APPEND_SLASH = False.
Normally when Django finds a url not ending in a trailing slash it appends a slash to it and redirects the user to this new url. But you have stopped this behaviour by setting APPEND_SLASH = False. As the first step I would advice you to change this back to APPEND_SLASH = True.
Next you should always name your urls and use those names to refer to the urls. So your urlpatterns should be:
main url.py
urlpatterns = [
path('admin/', admin.site.urls),
path('', include('search.urls')),
path('preprocessing/', include('preprocessing.urls')),
]
search url.py
urlpatterns = [
path('', views.index, name='index'),
path('scrape/',views.scrape, name='scrape'),
]
preprocessing url.py
path('', views.index, name='preprocessing'),
Now in your templates you would simply use the url template tag:
Preprocessing

Can't view thumbnail created in django admin

I am trying to create my own thumbnails within the Django admin and I believe they are being properly pointed to. However I cannot view the thumbnail. This is what I see:
When I click on the thumbnail, I get redirected to this URL and all I see is an empty white page.
http://localhost:8000/media/media/Screen_Shot_2016-04-08_at_12.55.25_PM.png
It is the correct URL but I do not see anything within the page.
Here is what code I have:
Within my settings.py
All files will go to cherngloong/uploads and /media/ will be the public front facing representation of the MEDIA_ROOT
MEDIA_ROOT = os.path.join(BASE_DIR, 'uploads')
MEDIA_URL = '/media/'
Within cherngloong/cherngloong/urls.py I appended to the list to serve static files:
urlpatterns = [
url(r'^admin', include(admin.site.urls)),
url(r'', TemplateView.as_view(template_name="index.html"), name="index"),
url(r'^api/', include('api.urls'))
] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
My cherngloong/api/urls.py:
urlpatterns = [
url(r'ig$', most_recent)
]
cherngloon/api/models.py:
class Media(models.Model):
...
...
file = models.FileField(upload_to="media/", default=None)
cherngloong/api/admin.py:
class MediaAdmin(admin.ModelAdmin):
search_fields = ["name", "file"]
list_display = ("name", "media_type", "url", "album", "display", "thumbnail")
def display(self, media_obj):
return '%s' % (media_obj.file.url, media_obj.file.name)
def thumbnail(self, media_obj):
location = media_obj.file.url
thumbnail_html = "<img border=\"0\" alt=\"\" src=\"{1}\" height=\"80\" />".format(location, location)
return thumbnail_html
thumbnail.allow_tags = True
display.allow_tags = True
Project structure:
I can't see any inconsistency on the code you have. I just created a project on GitHub with the same structure, and code you posted here, and everything worked like a charm, you can test it yourself, see the code here: https://github.com/vladir/cherngloong. I used the last version of Django, I know you are using a version below Django 1.9 because you are still using allow_tags, but even so your code seems good for me. Perhaps my code can help you to find what is happening.
I found why it is happening. Remove this line:
url(r'', TemplateView.as_view(template_name="index.html"), name="index"),
from your cherngloong/cherngloong/urls.py, and it will render ok.
It seems as this URL is too open, and it interferes with the thumbnails URL.
It could work for you instead:
url(r'^$', TemplateView.as_view(template_name="index.html"), name="index"),
I updated my code on the repo, so that you can test that it is working.

Very strange behavior of Django template loader [duplicate]

I am not able to render any html pages in Django 1.7. My 'index.html' is in 'project/seatalloc/templates/index.html' and my view.py in project/seatalloc/views.py looks like:
def index(request):
return render(request, 'index.html', dirs=('templates',))
project/project/settings.py has templates dirs set:
TEMPLATE_DIRS = (
'/Users/Palak/Desktop/academics/sem3/cs251/lab11/project/seatalloc/templates',
)
urls.py:
urlpatterns = patterns('',
url(r'^seatalloc/', include('seatalloc.urls')),
url(r'^admin/', include(admin.site.urls)),
)
I have tried to follow the documentation strictly, yet can't figure out if Django detects the file, why am I getting TemplateDoesNotExist at /seatalloc/ error. I am new to Django, could someone please help.
If - as in your case - you get a TemplateDoesNotExist error and the debug page states "File exists" next to the template in question this usually (always?) means this template refers to another template that can't be found.
In your case, index.html contains a statement ({% extends %}, {% include %}, ... ) referring to another template Django cannot find. Unfortunately, as of Django 1.8.3, the debug page always names the base template, not the one Django can't find.
Try like this,
import os
BASE_DIR = os.path.dirname(os.path.dirname(__file__))
TEMPLATE_DIRS = (
os.path.join(BASE_DIR, 'templates/'),
)
First of all don't use static path (fixed path) in template dirs in settings.py, use:
BASE_DIR = os.path.dirname(os.path.dirname(__file__))
TEMPLATE_DIRS = (
BASE_DIR +'/Templates',
)
And template directory should be in project directory i.e in which manage.py file exists.
And in view.py use render_to_response instead of just render.
return render_to_response("index.html")

How to organize and access URLS and TEMPLATES in django

i have just begun learning Django, and i stick with the principle that the fastest and best way to learn is through practice. I'm on the process of building my first web app, and i would really appreciate your help on the following :
I'm working on getting the front end to show up. But i'm having a hard time understanding the way the URLS work.
I have the following directory :
/myApp
/myApp
/public
/templates
/account
login.html
base.html
settings.py
urls.py
...
/account
urls.py
views.py
I have this on my myApp(the main app) 'urls.py' file
urlpatterns = patterns('',
...
url(r'^$', include(account.urls), name='account'),
)
And inside my account urls.py file, i already have the ff :
...
urlpatterns = patterns(
'account.views',
url(r'^$', 'login_user', name='login'),
)
I already defined following on the account views.py file :
def login_user(request):
return render(request, 'account/login.html')
So i guess the request should render my login.html file.
But i get the error that,
NameError at /
name 'account' is not defined
Therefore, i've figured that there must be something wrong with my settings.py file, right?
So here it is if it serves some purpose (just the important stuffs) :
...
BASE_DIR = os.path.join(os.path.dirname(__file__), '.')
...
ROOT_URLCONF = 'myApp.urls'
WSGI_APPLICATION = 'myApp.wsgi.application'
TEMPLATE_DIRS = [
os.path.join(BASE_DIR,'templates'),
]
STATIC_ROOT = os.path.join(BASE_DIR,'static')
STATIC_URL = "/static/"
STATICFILES_DIRS = [
os.path.join(BASE_DIR,'public'),
]
Right now, i really need to at least get the front end working. I hope the details i gave you gives you an idea of how i am organizing my file right now.
Additional note : I want to just create one template directory for the whole app. And as you can see on the structure, the template folder is inside the main app. How do i configure it inside the settings such that the apps use the main template folder?
You should put the account.urls in quotes. Also remove the $ sign from the regex:
url(r'^', include('account.urls')),
And in the account/urls.py file you should correct the base module name from oauth to the acount (your view is account.views.login_user, not the oauth.views.login_user):
urlpatterns = patterns('account.views',
....
)

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/'
)

Categories