I've received the following error when trying to set up a contact subdirectory inside the mysite directory.
Here is how I structured the urls.py script:
from mysite.contact import views as contact_views
(r'^contact/$', contact_views.contact),
Do I need to change anything in the settings.py TEMPLATE_DIRS, so, I can call the contact_form.html template correctly?
Yes, template_dirs needs to contain the path to the templates folder.
Related
In a django 1.8 project, I am attempting to redirect http://myProject/ads.txt to an external url http://a.location.that.has.the.ads.txt.file and thus serve the ads.txt file without using ftp to simply place the ads.txt in the root.
Given this minimal directory structure:
django projects
myProject
myapp
urls.py
views.py
someotherapp
yetanotherapp
myProject
settings.py
urls.py (this is the top urls.py)
views.py
in myProject/myProject/urls.py, (the “top” urls.py) I have as the first entry in the urlpatterns list, the lines:
urlpatterns = patterns('',
(r'^ads\.txt', RedirectView.as_view(url='http://a.location.that.has.the.ads.txt.file', permanent=False)),
followed by many more pattern matching regex’s. This does not work and fails with a 404. What does work is
urlpatterns = patterns('',
(r'^foo/ads\.txt', RedirectView.as_view(url='http://a.location.that.has.the.ads.txt.file', permanent=False)),
and then calling http://myProject/foo/ads.txt
Unfortunately, ads.txt files must be placed at the site root. I have tried many different regex’s that test fine in a regex validator, but just don’t work (returns 404). How do I do this without the extra dir “foo”? Any thoughts appreciated. Thank you.
Turns out you cannot redirect with the top level urls.py "routing table" to above the Django project root. A nginx server level redirect did the trick.
I want to be able to have dynamic directories where templates will reside. Let me explain what I mean. I have an application system wich has such a file structure:
\proj
__init__.py
settings.py
urls.py
...
\system
__init__.py
models.py
views.py
urls.py
\modules
\module_1
__init__.py
models.py
views.py
urls.py
\templates ## Attention
one.html
two.html
\module_2
__init__.py
modules.py
\templates ##
three.html
four.html
...
\module_N
...
As you can see there is a modules folder, which contains "atomic" modules, atomic in a sense that they have all necessary files, including templates in one place. So that module_1 has a template folder with its tempaltes, module_2 has a templates folder and all other modules have their own templates folder. What I want is to be able to refer to these templates folders in my settings.py file, so that when I upload a brand new module to modules folder, I would not have to change this settings.py file. So my question is, how can I dynamically build TEMPLATE_DIRS variable:
TEMPLATE_DIRS = (
## how to implement this???
)
EDIT
I'm considering another approach. First, to convert my modules to dinamic applications like this:
MODULES_DIR = 'system/modules'
for item in os.listdir(MODULES_DIR):
if os.path.isdir(os.path.join(MODULES_DIR, item)):
app_name = 'system.modules.%s' % item
INSTALLED_APPS += (app_name, )
And then to do something to make Django look for templates in all applications' folders. But I'm not sure whether it will work and how can I complete this task.
This is simply app-specific templates, which is already catered for by the APP_DIRS flag in the template settings dict. There shouldn't be any other configuration needed.
I'm trying to use this line in urls.py:
from mysite.urls.views import Index
However, Django is saying
ImportError at /
No module named views
I think that is because it's going into /mysite/mysite/urls.py and not /mysite/urls/views.py
The structure is like this (omitted the uninvolved files):
mysite/
templates/
mysite/
settings.py
urls.py
urls/
views.py
manage.py
I found out that was my problem. I followed How to change the name of a Django app? and my problems went away. Long story short, don't name your app: urls.
Add an empty __init__.py inside your urls folder. Why that should work? Take a look here and here
Absolute imports may be what you need:
from __future__ import absolute_import
Credits to this answer: https://stackoverflow.com/a/4931577/1028012
I'm trying to combine Django with Jade, but I've had some problems.
I have model which is named About. This has a view like this:
def about(request):
return render_to_response('about.jade',{},RequestContext(request))
and in my urls I have:
url(r'about/', views.about),
But it provides an error that the Templates doesn't exist (and yes, it exists). Is it correct to write the url like this?
Any help would be appreciated!
If your getting the big Template does not exist page in your browser, this usually means that django cannot find where you have stored the your template file (irrespective of using jade).
If you ve created a djnago 1.6 project you need to add the following line to settings:
TEMPLATE_DIRS = [os.path.join(BASE_DIR, 'templates')]
Then create a templates directory inside your app (not project) directory, and put your template there.
I'm new to Django 1.4 and try my first project. It's OK to create the app:
./manage.py startapp APP_NAME
and recognized by Django in urls.py as following:
(r'^home/$', 'APP_NAME.views.home'),
but when I only create a APP_NAME.py files in root directory of projects,and change the urls.py file as following:
(r'^home/$', 'APP_NAME.home'),
the debug page tell me ViewDoesNotExist. Even if I change the urls.py file to (with from...import and without single quotes):
from APP_NAME import home
(r'^home/$', home),
It also doesn't work.
How to solve it? It intricate to create APP for every view file.
Have you actually implemented your home function in 'APP_NAME.__init__.py'; if not, your code will not work, because there is nothing that can be called
python manage.py startapp news
In your created news directory there is a views.py. Now lets add something:
from django.http import HttpResponse
def index(request):
return HttpResponse("Hello")
now in your urls.py you add this:
(r'^news/$', 'news.index'),
Just start the devserver python manage.py runserver and point your browser to http://localhost:8000/news/
You should see "Hello".
Now you could add in your news/views.py some more stuff - like details, archive and so on.
Also be sure to have an empty __init__.py file in every directory you'd like to import.
from app.views import something will not work if there's no init-file within the app-directory.