How to use the Django admin template for a form? - python

I want to use the existing Django Admin template for a form of my own. The situation is like there are two models. say A and B and I want a form that displays the details about both the models in a songle form of my own. So I am using a ModelForm. But I want to use the templates from the django admin. How can I do that?

I think its better to override the admin interface.
https://docs.djangoproject.com/en/dev/ref/contrib/admin/#overriding-admin-templates

Would a Class-based generic view work for you?

Related

how to make a data fetch form in django?

I want the user to have a form for fetching data from the database, for example, as in /admin:
Form in admin
So that the user can select multiple entries and, for example, delete them, how can I do this?
if you want to modify the django admin and add the functionality you need to use this package from django.contrib import admin to register your personal models by extending admin.ModelAdmin
To change the admin interface you need to override template files in .../site-packages/django/contrib/admin/templates/ Here is the full tutorial for more clarification
https://realpython.com/customize-django-admin-python/

Django override custom admin site's templates

I have two admin sites in my django app. One named admin and the other named owneradmin.
I want to override their templates separately. Is there a way to do it ?
If you want to override the index template, the easiest way is to set index_template for the AdminSite.
CustomAdminSite.index_template = 'path/to/custom/template.html'
OwnerAdminSite.index_template = 'path/to/owner/template.html'
See django docs for more details about index template and template overriding.

django admin custom template for model redirection

A curious question
Suppose I have a module/app say Demo which contains a model say "anything". Now I registered this model to admin.py.
Now when I log in to admin and see this model I see general update, delete option in admin template.
Is it possible that when I see this model in admin and click that model it redirects to another url that is of that module's url and it display in another template and I can show anything I like from view..
I am sorry if it is confusing. What I want is I want to show the admin's model property like update, add and delete in another custom template made by me and it should be redirected to url defined in the module's urls.py
Thanks in advance.. :)
This can be done by extending your base get_urls() method docs here

how do you style a form already inside django

I'm using django-registration. I'd like to update the AuthenticationForm in 'django.contrib.auth.forms'. Specifically, I would like to pass the 'attrs' dict to add a few attributes. How do I update a form that comes with django?
You can use the views that come with auth and override the form parameter with your own form:
django.contrib.auth.views.login(request[, template_name, redirect_field_name, authentication_form])
More info here.
The standard way is to subclass AuthenticationForm, change the attrs in constructor, pass the form to login view and write a new entry in urls.py.
This is a nightmare: in order to add html attribute to a field it is necessary to use python subclassing, to know how exactly django's form metaclass work (self.fields['field'].widget.attrs, not just self.field.widget.attrs), to know regexes (for urls.py), to know how django's urls.py work (should you put the overriding line before of after include('django.contrib.auth.urls')?) and to know where is the auth form and auth view imported from.
And now the "commercial break": just use http://pypi.python.org/pypi/django-widget-tweaks for your task ;)

Overriding admin view methods - Django

I need to override the an add form within the admin panel.
I'm thinking of accomplishing this by writing a view which would then point to the admin view for the final result.
Something similar to this (where admin_basic_ass_user_view is the admin view)
#required_login
def add_user(request):
if condition:
return admin_basic_add_user_view(request)
return render_to_response("admin/auth/user/add_form.html", { ... })
Any ideas?
Why not just override the relevant methods with your ModelAdmin subclass? That's why it's a class, after all.
Add something like this to your urls.py
((r'^admin/auth/users/add/$', 'Project.SomeAPP.admin_views.add_user'),
The path needs to point to your new view. You should see the results of your new view in the admin interface's add user page.
EDIT: I forgot to mention, make sure you add that line BEFORE the normal admin interface line in the urls.py

Categories