Userena Template Directory - python

I've gotten userena successfully installed and I've copied the entire userena template directory over to my app (which is not named userena but rather accounts, so the directory the templates are in is /accounts/templates/accounts/ ) but simply editing them reflects no changes. When I point my urls.py to
url(r'^accounts/signout/$', userena_views.signout, {'template_name': 'accounts/signout.html'})
It works, but only for that one file. Is there a way to have the entire app automatically refer to this templates directory? I thought about using TEMPLATE_DIRS but that would apply to the entire site and not this one app. Any solutions? Thanks for your help.
The view that calls this template:
def signout(request, next_page=userena_settings.USERENA_REDIRECT_ON_SIGNOUT,
template_name='userena/signout.html', *args, **kwargs):

Put all userena templates in accounts/templates/userena instead of accounts/templates/accounts.
That should work.

Related

Current way to get Home URL (Domain) in Django Template?

This is so simple, yet it seems that its not provided.
Basically, if my site is...
http://www.example.com
http://127.0.0.1:8000
Or a non-root install like
http://www.example.com/ye-ol-django/
http://127.0.0.1:8000/ye-ol-django/
...I would think django would know this and have a constant available in templates.
The solutions I find involve:
Set it up in settings.py with SITE_URL =
Reference settings.py in a view.
Finally access it in the template with {{ SITE_URL }} or something.
Not very D.R.Y.
Not to sound spoiled, but doesn't django provide the {{ GET_ME_THE_ROOT_URL }} reference?
Sorry, django has trained me to expect goodies like this.
Just sayin' if I was writing a framework that would be the first thing I do, besides putting a small fridge beside my desk full of hotpockets and a microwave a safe but close distance away.
Ha! Nice question.
Let's break down your problem. You want some data to be available across all the templates available in your project. And also, you want to provide the value once and not repeat it across views.
Template Context Processors is the thing you are looking for.
In your settings.py file, add a new context_processor to the list of TEMPLATE_CONTEXT_PROCESSORS.
TEMPLATE_CONTEXT_PROCESSORS = (
"django.contrib.auth.context_processors.auth",
"django.core.context_processors.media",
"django.core.context_processors.request",
"django.contrib.messages.context_processors.messages",
"your_app.context_processors.root_url"
)
Then, inside your_app, create a file named context_processors.py. This file will contain the following code.
from django.conf import settings
def root_url(request):
"""
Pass your root_url from the settings.py
"""
return {'SITE_URL': settings.ROOT_URL_YOU_WANT_TO_MENTION}
And, in each of your templates, you'll have a {{SITE_URL}} present in the context depending on the value you provide to ROOT_URL_YOU_WANT_TO_MENTION in your settings.py file.
Django sure spoils everyone. But provides the mechanisms to keep you spoilt.
Hope this solves your problem.
If you're rendering the template from a request, you can just name your root view, then refer to it with the url tag:
In your root urls.py:
url(r'^$', HomePageView.as_view(), name='home'),
In template.html:
click here
More good info over in the django docs: https://docs.djangoproject.com/en/1.8/ref/templates/builtins/#url

CSS not rendered in custom template for 404 and 500 pages django 1.8

This is my folder structure
Music
-Music
-Feature
-static
-feature
core.css
-css
other css files
-js
-img
-templates
404.html
500.html
index.html
-feature
about.html
detail.html
template.html
manage.py
views.py
from django.shortcuts import render
def error404(request):
return render(request,'404.html')
urls.py
urlpatterns = [
url(r'^featured/(?P<pk>[0-9]+)/$', views.DetailView.as_view(), name='detail'),
url(r'^about/$', views.about, name='about'),
url(r'^FAQ/$', views.faq, name='faq'),
]
handler404 = 'mysite.views.error404'
The custom 404.html file gets rendered but with no css.And normally the css works fine on other pages but when I set debug=falseto check the custom 404 error page in settings.py the css for the entire project disappears. Something to do with folder structure or some other problem?
edit: core.css is the main css file and the part with other css files contains css for plugins
It's about serving static files. When you use DEBUG = True then django takes care about them otherwise your server should do it. Django in debug mode uses this view. The warning from there:
This view will only work if DEBUG is True.
That’s because this view is grossly inefficient and probably insecure.
This is only intended for local development, and should never be used
in production.
You can run your server with --insecure option just to test 404 error or you can explicilty create url for that page to check its styling:
Use the --insecure option to force serving of static files with the
staticfiles app even if the DEBUG setting is False. By using this you
acknowledge the fact that it’s grossly inefficient and probably
insecure. This is only intended for local development, should never be
used in production and is only available if the staticfiles app is in
your project’s INSTALLED_APPS setting. runserver --insecure doesn’t
work with CachedStaticFilesStorage.
Your handler404 and view are okay. But you don't need them. Just a custom 404 template is enough. See: https://docs.djangoproject.com/en/1.8/topics/http/views/#the-http404-exception
In order to use the Http404 exception to its fullest, you should
create a template that is displayed when a 404 error is raised. This
template should be called 404.html and located in the top level of
your template tree.
The template is in the right location. I think the problem is serving your static files. Open developer tools in your browser to see what resources fail to load (console, network or sources tab). Inspect the paths. Is there an external style sheet link in the head section of the 404 source? (elements tab or view source code).
https://docs.djangoproject.com/en/1.8/howto/static-files/

Django 1.6 - templates for password change/reset

I want to use django's password change/reset views so for example for the view
django.contrib.auth.views.password_change
I need create a template named
registration/password_change_form.html
the problem is that even though I have this template implemented in my project, django still shows the password-change page of the admin website, the only way I can make django use my template is by renaming it to something different - like registration/password_change_form_1.html and then pass the name
url(r'^password/change/$',
auth_views.password_change,
{'template_name': 'registration/password_change_form_1.html',
'password_change_form': MyPasswordChangeForm},
name='password_change'),
Am I missing something here? why won't django use my template when I use the default name?
I think because your app is under django.contribute.admin in the INSTALLED_APP.
Django automatically generates the admin template with the default name, so, if you use the admin, you must specify a different template name.
It simply fails to find your template, since it is overiden by the generated one.
Add in settings
INSTALLED_APPS = (
...
'registration',
)
After
TEMPLATE_DIRS = (
...
"/home/user/templates",
)
Add in directory templates "registration"
base.html that will contain the template and other templates
run in django==1.5

New django template

I am trying to edit some things in django and am running into a few problems as I am quite new to it.
I am working off an existing django configuration with templates that currently work, but I don't really understand how to create new templates. If I create a new .html file in my templates folder in "myprojects" and then declare the new template under cms_templates = (...) in settings.py nothing happens. Am I missing a step to get the template to upload and appear in my cms?
I also had a problem with an existing template, when I try to go to the contact page I am told that the template does not exist: http://paul.evansoderberg.com/en/contact/
The template is declared under "cms_templates", the file exists in the templates folder, and the page is created in django cms yet I get this error message.
Help would be greatly appreciated, thank you.
In settings I have :
CMS_TEMPLATES = (
('index.html', 'index'),
('contact.html', 'contact'),
('main.html', 'main'),
('about.html', 'about'),
('cv.html', 'cv'),
)
The template field in admin is set to "contact"

where to put django-registration's activation_email.txt template

As per the docs,it expects this file in a folder named 'registration' in templates directory.I tried that ,but I am getting a templateNotFound exception.
I have configured the registration template location like
from django.conf.urls.defaults import *
urlpatterns=patterns('django.contrib.auth.views',....)
urlpatterns+=patterns('registration.views',
url(r'^register/$','register',{'template_name':'myapp/registration_form.html'},name='myapp_register'),
)
Is there some place I can configure the location for activation_email.txt template ? When I submit the registration form ,the user is created in db ,but django searches all over python path for the activation_email.txt template
Why is it that all other templates are found with out any problem while this template is not found?
Look at your TEMPLATE_DIRECTORIES setting. The template paths are relative to any of the TEMPLATE_DIRECTORIES.
If template_name is myapp/registration_form.html, then you should create a dir "myapp" in one of the TEMPLATE_DIRECTORIES, and put registration_form.html in there.
To double check TEMPLATE_DIRECTORIES, run manage.py shell and in there from django.conf import settings; print settings.TEMPLATE_DIRECTORIES.

Categories