Django 3.0.6
For one model I need a modified admin site template.
Namely, I want to modify this template:
admin/includes/fieldset.html
I have copied the fieldset.html from Django package directory and placed it like this:
/my_project/image/templates/admin/includes/fieldset.html
Here image is my application. It is this application that needs a modified admin template.
The problem is that all other models also get this template. And the used template filters don't receive necessary params and explode.
Documentation: https://docs.djangoproject.com/en/3.0/ref/contrib/admin/#set-up-your-projects-admin-template-directories
Well, I got confused and fail to organize the necessary directories structure.
How can I use the modified template only for one Image app?
You have a couple of issues. Firstly, only the following templates can be overridden per-app or per-model:
actions.html
app_index.html
change_form.html
change_form_object_tools.html
change_list.html
change_list_object_tools.html
change_list_results.html
date_hierarchy.html
delete_confirmation.html
object_history.html
pagination.html
popup_response.html
prepopulated_fields_js.html
search_form.html
submit_line.html
fieldset.html isn't in there, so you'll need to see if the including template(s) is in this list, and if it is, replace this template and use it to include your own version of fieldset.html.
Also, your location (slightly modified since can't override included templates per-app): /my_project/image/templates/admin/template.html isn't quite right. This will overwrite that template for every app. To do it per-app, you need a further subdirectory under admin:
/my_project/image/templates/admin/image/template.html
The reason for this is that templates don't really care about which app they're in, so the fact that the template lives in your image app doesn't mean anything to Django, the convention of putting them in your app's sub-directory is solely to avoid overriding templates that you don't intend to.
Related
I have recently installed Django Userena on my Linux Apache server.
After correcting some errors, I began looking through the documentation. However, after searching through the docs and after many Google searches, I still cannot find out how to change the appearance of the pages! For example, how do I change the appearance of the signin page, the signup page, etc? I know that each userena template extends base.html, but where do I go from there?
I am probably missing something very simple, so please forgive me if the answer is very obvious.
This is the signin method signature for Userena(source) -
def signin(request, auth_form=AuthenticationForm,
template_name='userena/signin_form.html',
redirect_field_name=REDIRECT_FIELD_NAME,
redirect_signin_function=signin_redirect, extra_context=None):
As you can see, there is a template_name method that holds the template location. You can override this. In your urls.py, you can use it like -
url(r'^signin/', 'userena.views.signin', {'template_name': 'signin.html'}, name="signin"),
You can then create the signin.html page inside your templates folder and extend base.html. The signin view sends the login form in a variable called form. You can see the source. You can use the form on your template signin.html like {{ form.as_p }}. You can also format each field individually if you can follow the userena.forms. AuthenticationForm. Again, check the source code. You can do the same for any view Userena has that allows overriding like this.
When in doubt, read the source code. :)
You need to override userena default templates.
Create a directory inside your templates/ named userena/, Then for example if you want to change signup form, easily create signup_form.html template file inside that userena/ dir that you've just created and start writing.
For instance here is default signup_form.html template.
You can find userena default templates at its github repo
Just copy the supplied userena templates in your template directory.
You can find them on a linux box with find / -name userena
The path you are looking for is ../userena/templates/userena. Copy the userena folder into your template directory and start changing the signin_form.html.
Just copy the templates to your own template directory. If you follow this link download and just copy the directory 'userena' to your template directory. You can then customise the templates including the email text and templates.
I'm using Grappelli with Django. When I follow the procedure as outlined in the Django docs to override a specific template for a specific model, it appears that Django (or Grappelli??) are struggling to render the template correctly.
I have copied the change_form.html file to the templates directory with the following subpath:
admin/properties/Calendar
where properties is the name of the app and Calendar is the name of the model for which I want to override the change form. Now the following appears
At least two things are not rendered correctly:
The breadcrumb bar is much thinner and the crumbs are tightly aligned with the left border of the bar.
The link for the history of changes is in the wrong place (it appears on the left), has the wrong style (no rounded edges) and appears twice.
I double checked that the correct template file is used by Django, so the mechanism as documented works albeit with the above described flaws. When I rename the duplicated template file or delete it from my app, everything looks fine again.
Before I dive into this, I was hoping someone could hint at what is going wrong here. All not overridden templates seem to work just fine.
From what I read I suspect you copy the default admin change_form.html template and not the grapelli version, found at grappelli/templates/admin/change_form.html.
If you want to override a grapelli template, then you should use that as source!
I've started working on a django project that has couple apps that are used entirely through templatetags through other apps. Voting app, comments app..etc
Although am faced with a challenge when it comes to rendering assets for that particular templatetag (css,js).
I already have a base.html with blocks for css and js but how would I be able to access them using templatetags to append related assets. Currently am heavily relying on inclusion templates and I did a lot of research and it seems impossible to touch blocks.
any advise? even if I would change the approach of using blocks am open to any suggestion
it seems perfectly reasonable to tell the users of your library (app) that they must include your css/js somewhere in their template. E.g. django crispy-forms does this. Just give them some sample code to include in their base template
The Django documentation states the following clearly:
Not every template in contrib\admin\templates\admin may be overridden per app or per model.
It then lists the ones that can, and base.html, base_site.html and index.html – the ones I'm interested in – are not among those listed. They can be overridden per-project, but not per-app.
My question is: is there a way around this that doesn't involve editing the code inside django.contrib.admin? I'm willing to consider some monkeypatching solutions :-). I really want my app to have custom versions of those three files inside its templates directory, and have each project that uses the app to use those.
The reason I'm interested is that I'm creating a large, reusable app with a heavily customized admin interface, and per-project overrides of the "core" templates aren't the best solution, since I'd have to copy the custom templates to the template directory of each project that the app gets used in. Releasing new versions of the app with new modifications to those core templates would mean re-copying everything to the affected projects. Ugh.
I understand the reasoning behind the decision to only make a select few templates overridable per-app; after all, if overriding them all was possible, which app's overridden admin would take precedence?
But in my case, the app will be the "centerpiece" of several client projects, with other apps in those projects merely being in a supporting role.
CSS-based customization of the existing templates only gets you so far, and I'm hesitant to rely on JavaScript DOM manipulation solutions unless absolutely necessary.
One solution that comes to mind is to place the custom base.html etc. templates inside appname/templates/admin/ and then symlink them to the project's templates folder. That way any updates to the app will automatically take effect on the project level.
Symlinking is probably my method of choice if nothing better is suggested, but I'd like to hear if anyone has a nicer solution.
As I see your goal is to override templates for entire project, not for app or for model, but you don't want to put templates in project's template folder.
So you should just create 'base.html', etc. in 'your_app/templates/admin' folder.
Next you have to tell django that templates should be loaded not only from project's template folder, but also from your app's folder.
This can be done using TEMPLATES_DIR variable in settings.py file, smth. like that:
TEMPLATE_DIRS = (
os.path.join(PROJECT_PATH, 'templates'),
os.path.join(PROJECT_PATH, 'my_app','templates'),
)
I have my models and I would like to make use of the Django change_form template to edit my data.
Currently I have created my own template that works fine but lacks some of the basic stuff that change_form template might have, like fields validation.
Please give examples showing how should I call the template from my view, and what object variables need to be sent to the template.
Pointers to external projects/links that make use of this templates will be highly appreciated.
The only change_form.html template I know of is in Django's admin application. You don't use the template directly, you'll want to set up the admin application for editing your data. In order to do that, follow the steps in the documentation. If you have problems, post those problems as separate, specific questions.