Django: How to change the user view on the admin site? - django

I'm new in Django, so I have a question about the admin site. I want to modify the user view. I want add some buttons(e.g. "activate") or links in the User-table. But I cannot find any script, where I could modify it.

Are you interested augmenting the behavior of the Django Users model as well? If it's just modifying the presentation in the admin area, have a look at the Django docs. The admin UI is customized by adding an admin.py file to your application and referencing the ModelAdmin class. From the docs:
For each of those models, optionally create a ModelAdmin class that encapsulates the customized admin functionality and options for that particular model.

Buttons and links sound like it can be done in the template. You may want to override add_form.html which renders the User form but you need to have this hierarchy in your templates directory
templates/
admin/
auth/
user/
add_form.html
This form extends "admin/change_form.html". For your reference, check out the actual code online.

Related

Adding Wagtail to an existing Django app

I'm quite new to Django and Wagtail, and I'm having some difficulty with what I think is a very basic use.
How do I allow Wagtail to edit an existing view's template, while serving that template using Django's serving mechanism?
Assume I have an app (HomePage) created to serve the site's main index (/). I have the HomePage's views set up to render template and certain elements dynamically. Now I want that template to be editable via Wagtail's CMS interface. Something as simple as an image on the frontpage, or a headline.
The closest I've gotten so far has been to follow the Wagtail beginner's tutorial to override the base HomePage class in my app's models.py. That only made my pages available via the /pages/ URL.
Thank you for any help.
Since your site's home page is not a Page object in the Wagtail sense, I'd suggest looking at Wagtail's facilities for managing non-page content - snippets and ModelAdmin would be possible candidates, but I reckon the site settings module would be the best fit.
A Setting model gives you a set of fields which can be configured for display in the Wagtail admin using a 'panels' definition, just like you'd get for a page model - with the important property that only one settings record exists per site. You can retrieve this record within your homepage view or template as shown in the docs, and output it on your template as desired.
One way do that, is to let Wagtail serve your homepage. You will need to change your project's url configuration accordingly, to make wagtail's urls serve the root of your site.
Then, you can pack your dynamic content into a custom template_tag and include in your homepage html template.

Redirect a custom form to django admin form

I created a custom admin form for a particular model. All functionalities working. Now I want to call this form from the click of a button on another page that I have created. I was able to do that by creating an object of the form class, but it doesn't render the form as the admin site does (I had some fieldsets etc defined). Can I straight away redirect to the admin site's form? That would avoid duplicating the code for how I handle the form data. Any other leads would also be appreciated.
Now I want to call this form from the click of a button on another page that I have created.
Is this also within the admin interface? If so, don't use the admin view for all your users. As that will require nasty stuff with groups, permissions, etc.
I was able to do that by creating an object of the form class, but it doesn't render the form as the admin site does (I had some fieldsets etc defined).
Isn't this as expected, as the admin site comes with its own css and javascript? So you need to write it yourself
Can I straight away redirect to the admin site's form? That would avoid
duplicating the code for how I handle the form data. Any other leads
would also be appreciated.
True, but I believe the admin views have special authorisation checks on them. So you come back to the things with groups and permissions.
What I would do is move your form handling to an method in like utils.py and call this method from the normal and admin view.

Django model layout with multiple columns

My admin page for a specific model has two stackedInlines. Currently they display one under the other. I would like them to display side by side so The page would look like this (don't have enough reputation to embed the image :[ )
Any easy way to go about this without having to write my own admin page?
If that's the only solution, how exactly would I go about that?
you can override Django admin template for each app in your project.
you just have too create same folder as your app in template directory and overriding html.
How to override and extend basic Django admin templates?
You can add CSS/Javascript to admin page by defining class Media.
You'll have to override the admin template to accomplish this. See roshan's answer for more info.

How to add item to main django admin site without creating any database tables

How to add app item to main django admin site menu without creating any database tables. So then i can just overwrite admin template for this app?
You can add admin/foo/ urls to your own views in your URLConf. See "Creating Custom Admin Views" here: http://www.djangobook.com/en/1.0/chapter17/
Now you just need to add links to it. Override your favorite admin template, and add the link over there.
First, you can create an custom admin view that is independent of the models in your application and override the admin/index.html to include the new link explicitly. Alternatively, you can also override the get_urls in the AdminSite class
There are saner and better ways to add a new custom admin view. If you still want to use a model however, you can simply extend a django model and mark it as a proxy and override it's template.

django-registration view customization

I'm using django-registration (see: https://bitbucket.org/ubernostrum/django-registration ) on one of my projects. The standard setup for the django-registration is to add a the code below in the urls.py file
(r'^accounts/', include('registration.urls'))
and also customize the templates in a folder called registration.
The code above is creating links to the registration, login and password recovery which is fine. But in my project there are some other functions I usually add to my views so if I just add the include('registration.urls') it appears that I have no way of customizing the views containing those django-registration forms.
Is there a way to call the forms used by the django-registrationin a view so I can add a few more things on those views ?
The registration form is provided by the registration backend. Check out registration.backends.default.DefaultBackend.
There's a method get_form_class(request) that returns the registration.forms.RegistrationForm class. All you have to do is create a new backend, inherit from DefaultBackend and override the get_form_class() method to return a new form class.
You can pretty much do anything by providing a custom backend, except changing the base behavior of the registration app. If you need to radically customize the views in a manner that providing a custm backend doesn't make the cut, then just create a authn or users app and import any bits from django-registration you find useful. You can, say, keep the default models and managers within the registration app namespace, but hook up a custom backend to your own internals in a new app.