form from multiple models - django

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.

Related

How to build conversational form with django?

I am trying to build a conversational form with Django.
It will be used in landing page. The form questions will be loaded one by one as user answers them. And there will be some greeting and "human-way" responses to user input (such as "wow! you did a good choice!" after user selects one of the choices from form). The experience and look of the app will be like a real-time chat but user can only select one of the choices from form or upload a file/image.
1. Which technology is better to use for it? I am planning to do it with Fetch.
2. Since I want it to work without page reloading, how do I need to load Django forms through Fetch? Do I need to pass elements of it with JSON and construct it in client-side or can I just pass it as an html with {{form.as_p}} and display it in HTML?
Does these options make difference in matter of security?
I do not know anything about Fetch, but anyway I think it must be constructed clientside, but at first I would simply display the form in a template to get the ids of its fields and then use it in clientside code.
What about security - you'll need to pass the csrf token via your form.

How to use Django Rest API in Django normal project for autocomplete using Select2?

In my Django project, I have a model named Product. The model consists of products which have following entities:
name, id, price and so on.
In my project, an admin can add a new/old product anytime.
Now, for searching, I want to add autocomplete. I want to use Select2.
So users don't have to memorize the name of the products. To do that I found out here in the Select2 doc
that :
Select2 comes with AJAX support built in, using jQuery's AJAX methods
With this, I can search an API and fetch the data to show the users in autocomplete search field.
My Question:
Should I create a Django rest API and use that API to store products and fetch the data?
1.1 Would it be wise?
1.2 Is it possible to make a rest API within a normal Django project? If not then how to do that?
Or should I just use a normal urls.py and querying the result from
Select2 ajax function to that urls.py and to a custom query.py and
fetch the data directly from the database?
I don't think using rest framework with normal Django project would cause any problem. You are just adding some extra urls, that's all. It won't cause any problem to your project. Moreover, you can use the API to get json data of various models.
Hope this helps.

wagtail pages vs using django views and urls

When and where should I use wagtail pages and when should I write my custom urls and views using django? for example should I create pages for user profiles or should I add a urlpattern for profile and write profile logic in a django view?
This question is pretty broad and it depends a lot on what you are trying to achieve, but here are some thoughts.
General Approach
Use Wagtail pages if your pages have CMS (user) editable content and need to be placed (and easily moved) within the main page tree along with other editable pages.
If a page is not editable and URL must be fixed, it might be best to approach this either with Django URLs and views. Eg. Sitemap or Search page.
The docs cover how to set up mixed URLs in Integrating into Django.
Specific Use Case - User Profiles
You could create a Wagtail page model like UserProfileIndex which could contain some CMS editable content about that page such as body text and images.
This assumes you are wanting to make a social media type index and page for each user.
Then use RoutablePageMixin to have the URLs for each individual profile page.
Sometimes this index page thinking can come in handy, it let's you change the root URL but puts the logic for sub-urls within the model.
Remember that this means CMS users could technically change the slug of this page, unless it is locked down.
If you are trying to create a user profile page that is more suited for a user to manage their profile then it would probably best to use Django view.

Is there a way to have custom content with mezzanie/Django

I'm new using Mezzanine and figured out how to set-up pages where I can manage content from Admin page.
But I have static pages, where I want to store some content and being able to control that content from Admin page.
Is this something I can do with Mezzanine?
I imagine that I need to create a model with richtext field add that model to admin interface and than somehow access to that model through templatage.
But any exact example would greatly appreciated.
Thanks!
See the docs on creating custom content types. The primary approach is to subclass the Page model and add your custom fields.
Alternatively, if your custom content is conceptually independent from your pages, it might make sense to create independent models with relational fields to the RichTextPage model and edit them through inlines.
Note that the mezzanine docs on custom content types and the django docs on inlines use the same author/book example so you can easily compare the two strategies.

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.