New django template - python

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"

Related

Django Mezzanine CMS base urls.py location?

I'm a Django newbie and looking at Mezzanine CMS and looked at all URLs, can somebody point where http://127.0.0.1:8000/about/ is defined, what urls.py, I'm guessing it is
urlpatterns = [
url("^(?P<slug>.*)%s$" % ("/" if settings.APPEND_SLASH else ""),
views.page, name="page"),
]
is that correct?
That is correct. Mezzanine page urls are not defined explicitly in a urls.py, but are stored in the database in the Page model's slug field. You can navigate to the "about" page and the admin and modify its slug field there.
Note that in practice, page views are always intercepted and returned by mezzanine.pages.middleware.PageMiddleware, which could be relevant for debugging purposes.

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

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

Userena Template Directory

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.

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