I'd like to extend django-registration with some custom forms and templates. What's the best practice for structuring your project to include these customizations? Would I just add my own 'registration' app and include the forms and templates there?
I know this is one of the strengths of Django, but I couldn't find any specific guidance on where to put this stuff.
You can easily customize the templates by creating a folder inside your templates directory named the same name as the app's template directory name you are overriding.
For example: if the app you want to override a template file in has the following structure:
django_registration/
templates/
django_registration/
login_form.html
(it is standard practice to create a sub template directory named the same as the app name) Then you just have to create a file with the same name, in a sub directory of your templates directory named django_registration:
my_app/
templates/
django_registration/
login_form.html
You can also just override any of the views and form classes that you need, and just import those classes instead of django-registration's classes
Related
I have read several posts here where people discussed their preferred directory structure to keep various files of their Django project. But I get a file-does-not-exist error if I put my templates in the inner my_project folder instead of the outer my_project folder which contains the manage.py file. Is there some config file that specifies the default location of the templates (and other files)?can you please help me with the command change if I put templates in the inner (sub) folder? I just began my first Django project and would appreciate your help.
Ok, django looks for your templates at default places. I recommend you put your html files there (as a beginner).
Default places are:
- my_site_project/
-- templates/
--- base.html
--- navbar.html
--- footer.html
-- my_app1/
--- templates/
---- my_app1/
----- index_app1.html
----- about_app1.html
-- manage.py
If you put a general templates/ folder inside your project, same folder as your manage.py, those templates will be pulled.
If you want app specific templates the convention is to put a folder inside your app, again called templates/. This can lead to overlap if you put index.html inside your app-templates and an index.html inside your general templates/. Therefore you need to namespace the templates of your app. Create the app; create a folder called templates/ inside it; inside just created templates/folder you create another folder called my_app1; in that folder create your .html files.
Read the tutorial here. It is probably easier to understand than my jibberish. It is the official django tutorial.
A part of it:
First, create a directory called templates in your polls directory.
Django will look for templates in there.
Your project’s TEMPLATES setting describes how Django will load and
render templates. The default settings file configures a
DjangoTemplates backend whose APP_DIRS option is set to True. By
convention DjangoTemplates looks for a “templates” subdirectory in
each of the INSTALLED_APPS.
You can, in your settings.py via DIRS, specify where django looks for your templates. Although as a beginner I do not know why you should not stick to the conventions first.
I guess this is a question related to best practises in Django development.
I'm trying to build a web service with a main page (base.html) that contains multiple apps. I would like to make the apps self-contained, so I've made a templates directory in each app, and would also like to take advantage of the template inheritance feature of Django to make this whole thing as fluid as possible.
Now my concern is, where should I put the base.html in my project, so that the system knew where to find it?
Also, what changes should I make in the settings.py file in order for the system to be able to connect the templates? Is there a standard or a known method that takes minimal effort for this sort of arrangement?
You can put the templates folder in the project folder (ie folder contains settings.py).
as #danialroseman said, you just need to update the DIRS in TEMPLATES variable in settings.py. Let the project folder be myproject(ie folder contains settings.py)::
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
### ADD YOUR DIRECTORY HERE LIKE SO:
'DIRS': [ os.path.join(BASE_DIR,'myproject', 'templates')],
...
you dont need to create a seperate app.
One common design pattern that I have both seen and used is to have a centralized "app" as a part of your project that contains all the shared "stuff" you care to use in other applications. So you might have the following directory structure:
base/
static/
css/
common.css
js/
common.js
templates/
base.html
myapp1/
urls.py
views.py
templates/
...
myapp2/
urls.py
views.py
templates/
...
myproject/
settings.py
urls.py
Now you just include the "base" application just like any other, and you put shared stuff inside it. Other applications can refer to templates that live there, and can include any common libraries that you may want to share.
In settings.py:
INSTALLED_APPS = ['base', 'myapp1', 'myapp2']
There's no need for a central app for this. The TEMPLATES setting also includes an option for DIRS, which is a list of directories that will always be searched. So you can set this to an appropriate directory - eg os.path.join(BASE_DIR, 'templates') and put your non-app-specific templates such as base.html there.
I am overriding a template file in django-suit. Unfortunately this file is not detected and overridden.
According to their documentation:
Alternatively you can copy base_site.html to any of template
directories, which are defined in TEMPLATE_DIRS setting (if any). By
default Django looks in every registered application templates/ dir.
I have copied the file base_site.html into an existing app/templates/admin directory but this is not detected. I am already consuming template files from this directory so it should work.
What may be wrong in my situation?
Make sure your app is above suit in INSTALLED_APPS in settings file.
This is really important step to learn how template loaders work. It checks the folders in order of app in INSTALLED_APPS setting if it finds the template in first folder it uses that template. That's why also suit has to be above admin app because it overrides admin templates.
I located the Userena templates in my site-packages/userena. I was told by someone that I should not be editing any of the files in this directory but instead I should make new templates.
I'm guessing that I should be making new files that inherit from the parents files in the site-packages/userena folder? If this is true, I understand how to do this using Python and inheritance, but how do I do this with templates?
Do I just make a new template in my project/templates folder and add a line at the top to inherit from site-packages/userena/templates?
Check the Userena github and find the templates. You can override them by putting the templates under templates/userena/ (copy their templates folder and use it in your project)
I'm aware I can "store it anywhere in my python path" and all, but what's an organized pattern I can use to store middleware classes for my project?
I am appending my project root directory and project directory to the sys path through mod_wsgi:
sys.path.append( '/srv/' )
sys.path.append( '/srv/workarounds/' )
The latter line being the project root. As an example, let's say I want to apply this middleware class: http://djangosnippets.org/snippets/1179/
Would I just copy the snippet contents into a middleware.py file and dump it in my project root? Create a directory for middleware, add that directory to my python path?
My usual layout for a django site is:
projects/
templates/
common/
local/
Where:
projects contains your main project and any others
common contains things you may share across sites, or are at least not project-specific, like if you need to download django-profile and django-registration rather than having it directly in python/site-packages
templates contains just that
local contains things that are going to be specific to the current machine, so that you can have properly separated data, like database location and password - I then soft-link the machine-specific versions (say "machine1-localconfig.py") to local/localconfig.py and then can "import localconfig" in settings.py
I generally put middleware that's project-specific inside a project, and middleware that's not project-specific in common/middleware/
Make sure to add the templates directory to the right place in settings (or most probably, localconfig.py and then import it in settings), and makse sure to add the projects, common, and local directories to your PYTHONPATH.
If you have only a couple of tightly coupled middleware classes put them into a middleware.py module under the app root. (This is how the django.contrib apps do it - see the sessions' app middleware here).
If you have many varying middleware classes, create a middleware pacakge with submodules of the related middleware classes. Though if you do end up in this situation, consider how you can refactor your project into several mini-apps that all solve a specific need (and open source them :)).
Personally, I have a common django package where I dump common middleware (like the your linked LoginRequiredMiddleware class) into a middleware package. If this makes sense in your project's context I would highly suggest it. It's saved my countless hours of duplication, and bug fixing. django-common and django-annoying are good examples of this kind of project layout