No formatting supported by TextField in django admin panel - django

I have Post model and have it registered in admin. It has description of TextField() but while adding new post from admin panel, description area does not support newline or <br> or any kind of formatting.
I have no usage of forms and only way to add post is through admin panel.

https://docs.djangoproject.com/en/3.0/ref/templates/builtins/#linebreaks
this reference worked for me.

Related

CreateView not uploading image and name of the user for a post

I'm making a web app which allows users to register to the site and let them post their posts.
For taking in their post I have used the built in CBV: CreateView. In the user post I take in the name of the user using foreignkey with null=True, image, title.
The problem is: when I go to the detail page of that post it shows that there is no file associated with the image field, so I made an if statement to check if the post contains an image. But then too, it does not show the image. It shows User:None.
I don't know what is happening, I have read that CBV take the self.request automatically and there is no need to link the current user to the post.
Following are the screenshots of my code:
models.py
browser image before clicking submit button
browser image after clicking submit button (detail page of the post)
post_detail.html
views.py
If the image is in you database than you can render it this way
<img src="{{post_detail.image.url}}">
I found the answer.
I forgot to add the enctype="multipart/form-data" in the form of my createview html.
Now it's working perfectly fine.
Screenshot of userpost_form.html
Thank you all for all the support.

Django how to change the admin panel template

I am trying to change what is displayed when I click on a model in the django admin. For example if I have the model Book and then I go into the admin panel and click book, it will give me all the fields that I created the model with.
But I want to edit that page that shows all that data. For example, if I click on the "Book" model in the admin panel and see my fields to edit, I want to put another bit of fields on the bottom from another model. Or maybe I want to put like a "Similar Books" list. Is it possible to edit this page?
Admin customization information is here. You can do stuff like list_display or fieldsets etc.

How to customize the "Clear" checkbox for a models.ImageField in django Admin page?

i have a Model for a user profile in my django app that has a models.ImageField and i have an ModelAdmin for it
when a user uploads an image , in the admin page , when i go in that user's Customize page , in the ImageField section , there is the url of uploaded image and a checkbox named "Clear" and a button for updating the image. how can i change the text of that checkbox ? for example i want it to have the text "Delete" instead of "Clear"
It seems like "Clear" is hardcode.
So either you create a custom widget simply like that:
class MyClearableFileInput(ClearableFileInput):
clear_checkbox_label = ugettext_lazy('Delete')
And assign it to your form field like that
MyForm(forms.Form):
myfile=ImageField(widget=MyClearableFileInput)
Or add overwrite it in your admin
class MyModelAdmin(admin.ModelAdmin):
formfield_overrides = {
models.ImageField: {'widget': MyClearableFileInput},
}
Or you use the translation mechanisms to translate Clear into Delete. Django translation is described in the docs pretty well.
I personally, just think that it is quite some overhead for your problem, unless you are using translations anyway. I would clearly recommend the custom widget - the addtional code is really minimal.

Django: publishing and unpublishing posts

I have a basic Django blogging application with the fields such as textfield, datetime, imagefield, slug etc. I want to have a feature where I can publish and unpublish posts in the admin interface(with the help of checkbox). How can this be done?
Add a BooleanField that describes whether or not the post is published, and check the value of this field in your views.

How to modify the way a ForeignKey field is rendered in a Django admin page to avoid browser crash?

I have a Customer model which contains a ForeignKey to a Contact model.
I have over 100,000 contacts in my DB and when I load the admin page for a specific customer, the dropdown menu for the contact is getting populated with ALL of the contacts in the database. This has recently, due to its shear length, started causing my Firefox to crash while the admin page is loading.
Is there a way to either:
replace the field with an integer
field I can manually modify to the
contact ID when necessary
replace the dropdown menu with some
alternative input method which won't
crash the browser
remove this input
from the Customer admin page
altogether
Thanks!
You can do any of the either of things you want to.
Simplest solution is the exclude the field from the admin. Just say so in the admin class.
You can change the field to be text input and display it's primary key rather than the item itself, by including it in the raw_id_fields of the admin class.
You can also replace the standard dropdown widget with the Auto complete text field input. Use the implemented widget, or other equivalents. - This is probably the solution you like the best.
You can also override the formfield_for_foreignkey method on the Admin model to customize the queryset that gets displayed in the foreign-key dropdown. You may want to checkout my implementation for displaying only the current User's (or subdomain's) added entities.
Sounds like specifying the contact field in raw_id_fields in your admin.py entry for the relevant model would sort you out. Docs are here.
PS. Surprised (but not that surprised) that FF gives out before your database server tanks...