Django: how to use the nice admin tables in the actual application - django

New to Django, just worked through the 'polls' tutorial. Now I would like to convert those simple views into tabular views, just as in the pretty admin interface. Should I try to clone code from the admin module? Otherwise, how to proceed?

The admin forms are just Django forms and formsets. To build a form for your model you use modelforms. Maybe nice to look at the admin tabular_inline template. It lives at: django/contrib/admin/templates/admin/edit_inline/tabular.html.
The snippet is looping over the form fields.

Related

Build react form based on serializer of Django Rest Framework

I want to build web app where users can add different ads for sale(cars,apartments, gadgets etc...) and each of this categories models in django are specific and need specific fields in form for creating ad. Creating this forms using django is easy by using ModelForms.
DRF documentation says that
A serializer class is very similar to a Django Form class, and includes similar validation flags on the various fields..... . So I guess I will create form on base of serializers.
My question is: How make a single React component which will render form on base of different serializers that frontend gets from Django-Rest-Framework?
Please any advice or links where I can read about this . If you need I can share some code . Thank you in advance.
Quite a late response, but here's some workaround.
You could check this npm package that convert JSON into React Form: https://www.npmjs.com/package/react-jsonschema-form
You could generate the JSON description from your Django model, by looping among attribute.
It's not straightforward but it worked fine for me.

Django Forms without admin

I am learning Django, I saw so many videos of DJango.
I just wanted to know one thing that can we create an app (like login app, or contact app) without registering it into in admin (admin.py).
Off course it should have model etc to save the contact details or login details etc. Is it possible in Django ?
Just don't create your admin.py file or not register the model that you don't want to see there. Django admin is fully optional.

form from multiple models

I need to create a form where the rows are a list of users and the columns are all the user's permissions. The models are the default django User's and django Permission's models.
How can I do this things? Are there a way to do by django forms or I must do manually with django and js?
If you just want to view this data, extract the relevant information (User and Permission instances), and then generate a table using django templates. Django intentionally does not 'bless' any css or js frameworks, so you are required to do the html yourself (using tools of your choosing).
Forms aren't going to help you here I don't think. I advise you to draw up what it is you want to see, and start figuring out what the html is going to need to look like. Forms in django are simply a mechanism for passing data between django and the user. You are free to render them how you wish.

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

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.

Django form to enter/save html to database

I'm in my first week of Django development and am working on an admin page that will let me write some quick html using TinyMCE and then save it to the database. I don't need to display this web page on the site or add it to urls.py, etc. The html snippet will be loaded from the database and used in a view function.
I've read in "Practical Django Projects" how to integrate TinyMCE, so my question is more concerned with the best approach for the form itself. Specifically:
1. Is there a built-in form like flatpage that works well for this? I only need one field in the form for the html.
2. How do I save the form's text after it's entered?
I created a model with a JSONField to save the html in, but I'm not clear on what to do next. Thanks.
here is the documentation for Django Flatpages App, maybe you serve.
I ended up using the ModelAdmin class to get what I wanted. I created a new model in models.py and then used ModelAdmin to enable an admin-editable form for the model's data.
Hope this helps someone.