How do I use the change_form.html template in django? - django

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.

Related

Use the modified admin template only for one Image app

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.

Combining multiple Django Views/Templates

I am working on a website. On the Homepage, I want to show the posts in the center and show famous tags on the right side of the page. Now, posts and tags are two different apps and their views/tempaltes are calculated/generated in two separate functions.
How can I show what I want on my Homepage keeping my mind that its two different views being called. As far as I understand, I can only call a single view to show my homepage.
This must be possible but I am unable to understand this. Any help will be much appreciated.
I think you need to create a custom template tag that in this case called: Inclusion tags
You can look into writing a custom template tag for the tags. That way the tag logic can stay in the right application.
It depends how these different apps render the templates. If they just render the template without extending from a base template you could simply use AJAX to load all the content into your homepage.
If not, then there is no other way than writing a custom solution for this. This could be an extension to your views, a custom template tag as already mentioned or something else according to what exactly you need.

Changing django extended template to have all provided templates according to my design

To utilize django's own provided templates like forgot password, change password and such templates and views funcionality, I made my own templates for login e.t.c. But after some time I found that there are many views and templates already present that one can utilize just by providing right URL but only problem is that these templates are extending django's own template. So I want to know that is there a way to only set extended template of my own choice that is my site's main template so that I just give the URL of forgot password or change password e.t.c. related pages and django render the template by extending my main template instead of django's own template. I am not sure if admin panel is extending the same template that other pages are.
Please tell how can I do so and also please tell if doing so can have any problem.
More detail:
I know inheritance and extending with our template file but how can I set my own template in such way that they automatically inherit mine.
For example, for forgotpassword, I had to make a template file and need to paste form elements and top of it I write {%extends main.html%} but I don't want that, I know that default forgot password template is being inherited from some default django template I want to set it some so that it always inherit from my template in that case.
Short answer: You have to write your own templates.
Longer answer:
Django does include some templates, but these are all intended solely for the admin application. The auth views default to using 'registration/some_template.html', and those templates all live in the admin app ( see https://github.com/django/django/tree/master/django/contrib/admin/templates/registration). The auth views all support a template keyword argument that you can supply in your URLConf, so that you can specify which of your own templates to use.
Think of the admin templates as a reference that you can use when writing your own templates, but not as something you're meant to use directly.
Try to get your head around template inheritance. I think you can achieve what you want using template inheritance. This is a good post introducing the topic.

How can I insert a rendered form from a form application in a Django template from another application?

I'm doing a form application (similar to django-contact-form) and would like to be able to insert the rendered form in a Django template of another application.
What's the best way to do that? Should I create a template for the form (with for example only {{ form.as_p)? If so how can I insert it in another template?
Thanks
Jul
What do you mean when you say a separate 'application'? Do you mean a completely separate Django site, or just a separate app within your current site?
If the latter, there's no issue: just import the form into the view you want to use it in.
If the former, then there isn't really any good way to do it. You could try using an iframe within your template to point to the external site, but it's not a particularly nice architecture.
Couldn't you load the application with the form you need in the settings.py file under INSTALLED_APPS and then call it from your template somehow? I'm a little new to django, but from what I can tell that seems like it would be the "django" way of doing it.

How can I pass a standard, static variable from all views in Django?

I'm working on a blog application, and I want to have a sidebar that includes a list of all the months the blog has been in existence, to provide links to archives pages. Moreover, I'd like to make this automatically update when the month changes, rather than hardcoding it in the template. Of course, as far as I can tell, this means that I'll have to calculate the list of months in every view, and pass it into every template from every view.
I'd like to avoid this, if possible. Is there a way to calculate the list once and automatically apply it to every template, without having to explicitly pass it into the template from every view?
There are a few possible solutions to your problem.
If you really want to have this on every page on your site a context processor is probably your best choice. Context processors are basic way to inject data into all template contexts. Be aware however that the context processor will be called on every request.
An alternative solution would be to create a custom template tag and use it on a shared base template for all of the pages you wish to have your sidebar. Template tags are a bit more complex to create but they are more flexible.
With either solution you should also look at Django's cache framework. The cache framework makes it pretty easy to temporarily store your calculated values for a while to save some work on each request.
You want a template context processor
Django - having middleware communicate with views/templates
http://docs.djangoproject.com/en/dev/ref/templates/api/?from=olddocs#id1
Django's template inheritance should cover this. You could create a base template that handles your sidebar functionality. Your other views extend this template.
Template Inheritance:
http://www.djangobook.com/en/1.0/chapter04/#s-template-inheritance
A combination of custom template tags as mentioned previously and template fragment caching should do the trick.