How to use django raw_id_fields in a view? - django

I am making a view, to make some POST requests on my Django server. I need a way to select something and the proper widget that I need for the same is Django admin panel's raw_id_fields.
Widget:
Popup:
I was looking for a jquery plugin to do the same, but couldn't find anything. Is there a plugin / widget to have this functionality in my own views.
Is there a way to use this exact widget in my views.
I found django-yaac whose README says that I can use this outside the admin. However I can't seem to figure out how to use this outside the admin.

I'm not sure if this helps you, but anywhere in your template you can access a model's id like this:
{{ myModelObject.id }}
You can use it in a hidden field or javascript variable, and then you can manipulate it however you like.

Related

Can I access my website from Django admin panel?

I am trying to develop a website where I can see the homepage as it is from Django admin panel. The reason I want that is I want to edit the name of the static content like 'Home' to 'All' along with the change of slide show picture. In summary, I want to change the website layout from Django admin panel that is not accessible to users. Is that possible? I just want an idea of how to implement the process.
Static texts you can change in admin panel with different modules:
for example django-rosetta or my own library Django-tof. https://github.com/wP-soft-GmbH/django-tof
But in your case, i think, you want to made something more.
For this case you can use django-flat-pages, already included in Django, if you have a static web-page.
you can edit every element on the page and after save, you can see it on the front.
if you really want to change the django templates, which you use in your views, you can create a simple template editor in the admin panel based on a widget like django-Ckeditor.

django admin: Adding a textarea and a button that invokes a function

On the main admin screen I want a link that when clicked would bring up a page with a textarea along with a button that when clicked will invoke a python function. That function would return data that would be displayed on the page below the textarea.
If this were a regular page that was part of my app I could easily do this, but I'm not sure how to integrate something like this into my admin page. Everything thing in admin seems to be tied to a model, and this is not. Do I have to create a dummy model for this? Let's say I do create a dummy model, then how do I get django to display my custom template for this?
Can anyone help with this?
You can use this django app: https://github.com/jsocol/django-adminplus to add links at the bottom of the list of models without adding a dummy once.

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.

Create custom django widget for timefield with now option

I have models with datetimefields and timefields. When the user interacts with these fields in a form they often just need to enter the current time. I need a now link almost exactly like what shows up in the django admin, so the user can just click it and the current time gets put in the field.
I tried looking through the django source but it seems to utilize some frontend javascript which I'm not very familiar with. Is there a simple way to make a widget that can be easily used in a timefield and datetimefield?
So this is not on the admin panel? As in on the site? Then this is not really a question to be posed to Django, I suggest tagging javascript. If you are unfamiliar with javascript, then tag jquery, they have things for this.
In case you're lazy, here's a start:
Here
jQuery premade
Javascript methods
Sorry, but this is more of a UI issue than a Django issue. Hope I helped, though.

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.