I'm in the process of setting up my views in Django, but I'm confused about how to link it to my pageOne.html page so it shows that instead of the "Welcome" page. Currently, when I run the server using python manage.py runserver 127.0.0.2:8001 I get an error message that says "Template Does Not Exist at" pageOne.html.
I've set
TEMPLATE_DIRS = (os.path.join(os.path.dirname(BASE_DIR), "static", "templates"))
I've also tried
TEMPLATE_DIRS = ('/Users/andrewnguyen/Desktop/Websites/gale/python/techExercise/templates',)
but that hasn't really worked either.
Update:
Here's a screenshot. I imagine that my error is somewhere in my urls, settings or views.py. I've changed the path to my TEMPLATE_DIRS as mentioned in the comment below. That did not work. I've included an image of my file structure on Imgur: http://imgur.com/kR1xcGJ
Your TEMPLATE_DIRS settings should look like this:
TEMPLATE_DIRS = (
os.path.join(BASE_DIR, "templates"),
)
Update
After looking at that screenshot you posted, I can say you've made a lot of mistakes.
The templates folder is inside the static folder.
Move the templates folder out of the static folder.
There's a mistake in the return statement of your view.
It should look like this:
return render(request, 'pageOne.html')
There's no need to prepend /static/templates/ to the template name. Django automatically looks for templates in your settings' TEMPLATE_DIRS path.
And finally, nothing you're doing is very difficult. All these mistakes are caused by ignorance, since everything is mentioned elaborately in the docs. Please read them carefully.
Related
I'm having trouble understanding how static files are handled in Django. I've read through the official Django documentation as well as multiple threads, including this wonderful one here:
Differences between STATICFILES_DIR, STATIC_ROOT and MEDIA_ROOT
Most people define the STATICFILES_DIRS list as a list of paths where django will search for additional static files aside from the app's static folder.
I understand that, but what does this have to do with the formfields I'm overriding in my admin.py?
I have overridden the default ManyToMany form to the FilteredSelectMultiple widget in a few of my admin models like so:
from django.contrib.admin.widgets import FilteredSelectMultiple
formfield_overrides = {
models.ManyToManyField: {'widget': FilteredSelectMultiple("User Names", is_stacked=False)}
}
This works fine and produces the widget override I wanted: Functional Widget Screenshot
However, when I define STATICFILES_DIRS in settings.py to include my root static folder like so:
STATICFILES_DIRS = (
os.path.join(BASE_DIR, 'static/'),
)
It breaks my override and defaults back to the original ManyToMany field form:
Broken Widget Screenshot
We do not have STATIC_ROOT defined in our settings.py as we don't plan on using the collect static feature. We plan on keeping/referencing our static files at the root static folder. Also in our settings.py we have:
STATIC_URL = '/static/'
I don't understand how these settings for dealing with static files are interfering with the formfield_override above. I would appreciate some insight on this, so that I could find a way to approach this issue.
Thank you!
I figured out the cause of my issue. I missed this portion in the Django documentation that explains STATICFILES_FINDERS: STATICFILES_STORAGE Documentation
It turns out that my production server was finding the static files in my outer root folder first, which were not up to date. This explains why I wasn't seeing my changes. I'm going to reconfigure the way I handle static files to ensure that the outer root static folder that my production server looks always has the latest static files via the collectstatic feature
I want to theme django auth's password change templates to my site. The problem is that django is seeing the django/contrib/admin/templates/registration/ versions of the templates instead of my beautifully crafted myapp/template/registration/password*.html.
This poster was told to fiddle with app order in settings.py, which is a bit fragile to my taste. I think this poster may have gotten a reasonable answer, but if so, I haven't quite understood it yet.
So what I did was to add a bunch of non-DRY cruft to my urls.py file, copied from auth_urls.py:
# Provide explicit templates where I've provided them, shadowing the URL's
# that registration.backends.defaults.urls will provide.
url(r'^accounts/password/change/$',
auth_views.password_change,
{'post_change_redirect': reverse_lazy('auth_password_change_done'),
'template_name': 'registration/password/change_form.html' },
name='auth_password_change'),
# ...
url(r'^accounts/', include('registration.backends.default.urls')),
This works, but saddens me in its repetition. Surely django encourages something cleaner. Any pointers?
(Fwiw, django 1.7 and python 3.4.2.)
Put this template into your project's templates dir and add the following code to the settings.py.
TEMPLATE_DIRS = (
os.path.join(BASE_DIR, 'templates'),
)
By default django.template.loaders.filesystem.Loader is placed before django.template.loaders.app_directories.Loader in TEMPLATE_LOADERS so project's templates directory has precedence over app's templates.
I just wanted to understand something about adding a templates folder in django. This is the code for including my templates folder, that django conveniently made for me:
TEMPLATE_DIRS = (
# This includes the templates folder
os.path.join(os.path.dirname(__file__), '..', 'templates').replace('\\','/'),
)
What I do not understand is the part with 'templates', why is it not '/templates', or even \\templates in the case of something Unix based? I come here after reading this article, in particular the part about Template Loading.
os.path.join is building the absolute path based on the operating system being used.
Here, Django is computing the templates directory by getting the path. Here, windows would have included a \\, hence the .replace
You can read more about it here
I've been trying to get custom templates for the admin page for Django working but have been unsuccessful. I've read the django documentation and several blogs which explain it as being such an easy step, which I assumed it was.
As of right now the admin page works but my own rewrite of the CSS or templates is not working. My setup is as follows
/project_folder/
manage.py
settings.py
urls.py
__init__.py
/app/
views.py
models.py
__init__.py
/templates/
/admin/
base_site.html
In the urls.py I have
(r'^admin/', include(admin.site.urls)),
Which works since I cannot login etc. So I am assuming the /admin/base_site.html would overwrite the default one but it isn't doing a thing.
Anyone know what is going on here ? I followed it from the Django tutorials/guides and went onto some blogs to see if they had answers but they all said the same thing.
Edit 1:
I do have my templates directory setup correctly.
TEMPLATE_DIRS = (
os.path.join(PROJECT_PATH, 'templates/'),
)
This works correctly as I have the rest of my site working with a media directory for CSS etc. The only thing not seeming to 'accept' the templates is the admin section.
Alright I fixed it, this was a stupid mistake but I was already playing with this for the past 2 hours. I had to declare my app before django.contrib.admin. It wouldn't accept it otherwise.
One more mistake that one should resist making on this exercise. The exercise says to change this...
<h1 id="site-name"> {{ site_header|default:_('Django administration') }} </h1>
to this...
<h1 id="site-name">Polls Administration</h1>
There is a temptation to only change the string constant, but that is incorrect. I.e. do NOT do this, it will not alter the heading:
<h1 id="site-name"> {{ site_header|default:_('Polls Administration') }} </h1>
That was the mistake I made, and I had to go through the exercise meticulously to fix it.
I couldn't get the admin template to be recognized when doing this step in part two of the Django tutorial.
This is how I solved it:
Using the information in this answer, I printed the value of TEMPLATE_DIRS:
At the command line, navigate to the directory in which the project's settings.py file exists (for me this is R:\jeffy\programming\sandbox\python\django\tutorial\mysite\mysite\)
Start the interactive shell: python
>>> import settings
>>> settings.TEMPLATE_DIRS, which outputs ['R:\\jeffy\\programming\\sandbox\\python\\django\\tutorial\\mysite\\templates']
So this is the expected location of the templates directory for this project. The admin directory goes into that, and the base_site.html file goes into that.
The other problem I had was that it was working, but I only changed the <TITLE> field, so I just didn't notice it. (I thought I was changing the main header.)
I had this same problem walking through the Django 1.6.5 tutorial (https://docs.djangoproject.com/en/1.8/intro/tutorial02/), but realized it was a mistake in not reading carefully. The tutorial has you do is put this into your settings.py:
TEMPLATES = [
{
...
'DIRS': [os.path.join(BASE_DIR, 'templates')],
...
}
I then put the templates folder I wanted to put override for the admin site under the app just like your example:
/project_folder/
manage.py
settings.py
urls.py
__init__.py
/app/
views.py
models.py
__init__.py
/templates/
/admin/
base_site.html
With this it wouldn't work for the reason similar to the issue you had noted by Daniel Roseman in one of the comments, my DIR was evaluating to project_folder/templates (as I told it to). Then I noticed in the tutorial it explicitly said put the templates/admin folder at the project level, not the app level:
Create a templates directory in your project directory (the one that contains manage.py). Templates can live anywhere on your filesystem that Django can access. (Django runs as whatever user your server runs.) However, keeping your templates within the project is a good convention to follow.
By doing this I had the following structure:
/project_folder/
manage.py
settings.py
urls.py
__init__.py
/templates/
/admin/
base_site.html
/app/
views.py
models.py
__init__.py
With this, the everything worked as expected (I could overwrite the default django templates for the admin pages).
While you should be able to put templates anywhere and configure Django to find them, it seems to makes sense to put your main admin templates at the project level, as the admin site's not app specific, but available for the entire project.
Make sure you have set TEMPLATE_DIRS in settings.py to the correct folder!
Can someone point me in the right direction on how to use Django-filer? I have installed the necessary tools for the filer to work and I get the Filer tab in the admin interface, but when I upload an image to the app I am not able to get the preview thumbimage. The image is also uploaded to the Media_root path(Public files default path). What else have I missed?
Had a similar problem. I had missed that when easy_thumbnails is automatically pulled in by pip, you still have to explicitly add it to your INSTALLED_APPS in settings.py.
I spotted this because when I was uploading images, they weren't appearing in the clipboard, and when inspecting the response from the upload, there was an internal server error complaining that an easy_thumbnails table wasn't available.
tl;dr; check you added easy_thumbnails to your settings and did a migration / syncdb
Take a look in your urls.py. You should have the following code in it:
if settings.DEBUG:
urlpatterns += patterns('', (
r'^media/(?P<path>.*)$', 'django.views.static.serve', {'document_root': settings.MEDIA_ROOT}))
Cheers