Pagedown(markdown editor) with Django - django

I trying to use the pagedown(markdown editor), the one that stackoverflow in my django based website. However to get the markdown editor in a textarea it is required to give the text area both id and class as
<textarea id="wmd-input" class="wmd-input"/>
However the form fields generated by django have a default id as id_<field-name>. Is there a way I can assign the same id to this text_area?

you can directly pass id as well in the models.py where your are passing class name. This will override the default behavior.
widgets = {
'<attribute_name>': Textarea(attrs={'class':'wmd-input','id':'wmd-input'}),
}

Related

add user friendly json editor to django admin

I have a django app, using also rest_framework, and a model Product with field of type JSONField. so data is stored as JSON in Postgres, Now I want to provide the admin with a nice user friendly way on how he can change the json field (names/keys and values). is there an extension for that or is there a faster way on how to do that.
here is the column definition in the database.
my_column = JSONField(default={"editorial1": "text 1", "editorial_2": "text2", "editorial_3": "text"})
BOTH KEYS AND VALUES SHOULD BE EDITABLE BY THE ADMIN
The admin should not know anything about JSON, and should not enter/edit any json format field
You can use prettyjson's PrettyJSONWidget:
class ProductModelForm(forms.ModelForm):
class Meta:
fields = (
...
'my_column',
)
widgets = {
'my_column': PrettyJSONWidget(),
}
I ended up using the django-admin-json-editor. Not the best thing in the world, but it does the trick
https://github.com/abogushov/django-admin-json-editor
You can try https://github.com/jrief/django-entangled
In comparison to the editors mentioned above, it doesn't replace the widget used to render the JSON, but allows to override the ModelForm which otherwise is generated by Django's ModelAdmin.

Django Admin Page: Help Text for Model Methods?

I have a model method in Django that I am displaying on an admin page just like I would a model field. With a field, I can just add a help_text argument to it to give a description of what the field is and what the user should put into it. However, with a model method, help_text does not work. Adding the attribute short_description changes the way the method name is displayed, which is sort of okay, but I'm looking for a way to add a few sentences of description beneath the method value that is displayed. Is there any way to do this natively, or would I have to resort to overriding admin templates or something? (Which I do not think is worth it for something this minor).
You can do this using JS.
Replace ID-OF-THE-FIELD with the actual id of the desired field.
(function($) {
var myField = $('#ID-OF-THE-FIELD');
// find the id of the desired field by doing
// Right-Click > Inspect element
var help = $('<p class="help">A very long help text</p>');
help.insertAfter(myField);
})(django.jQuery);
Put this code into a JS file and supply this file using class Media of your ModelAdmin class.

Show/Make a field editable or disabled in admin based on other field's value

I am working on a Django project and want to achieve the following functionality :-
class XYZModel(models.Model):
available = models.BooleanField(default=False)
availability_date = models.DateTimeField(null=True,blank=True)
So i want that availability_date becomes editable only if available is set to True, while giving values to the object in admin.
If available is False, availability_date is shown disabled...
How can i do so?...
You'll need to add some JavaScript to the change form for your XYZModel admin class. This can be done by overriding the change form for the template, or by adding a reference to the JavaScript file in a custom form class for your model admin.

Django: Correct way to extend a certain field in the admin change form with HTML?

Let's say I have a model with a field based on the ImageField class.
class Foo(models.Model):
imagefile = models.ImageField('File', upload_to='foo/%Y/%m%/%d/')
Django adds - after first upload - an input tag of type file to let me change it and a link to the image to view it.
I want this original field specific (HTML) code as is (and by no means create it myself manually) but also add other HTML/JS code, say to include a thumbnail-preview or add some AJAX-stuff. I can image a few other use cases for other fields, too.
What's the correct (say: easy/unobtrusive) way to implement something like that?
You need to write a custom widget. Look at django.forms.widgets for the code of the existing FileInput widget, which you can subclass and override the render method where necessary. You'll then just need to assign that widget for your file field in your admin form.

Insert django form into template dynamically using javascript?

I want to add same django form instance on same template. i already add one before and other add dynamically using javascript.
for example 'form" is a django form: newcell.innerHTML = {{ form.firstname }};
The problem is that when i submit the form, in view the request object has only one value (that is not add using javascript). how can i get the values of other form elements values that is added dynamically runtime.
It is something like the "Attach Another File" feature in gmail, where the user is presented with a file upload field and new fields are added to the DOM on the fly as the user clicks to "Attach Another File" plus button
You could always try separating out your FileField into a FileModel.
Take a look at the following pseudo code (as in python based on memory--i've moved over to clojure for now).
models.py
class FileModel(models.Model):
file = models.FileField()
...
class ThingToWhichYoureAttaching(models.Model):
name = models.CharField()
attachments = models.ManyToManyField(FileModel)
...
forms.py
class FileForm(forms.ModelForm):
class Meta:
model=FileModel
class ThingForm(forms.ModelForm):
attachments = forms.MultipleChoiceField()#override the manytomany form field with style field of your choice.
class Meta:
model=ThingToWhichYoureAttaching
When they pop up the window with the PLUS button, show the FileForm but on the main page leave the ThingForm untouched. You can also have an initial FileField on the main page with the ThingForm for people who don't have javascript. Just make sure to process the FileForm BEFORE the ThingForm so that the File is available for the Thing.
When processing the popup form you can use AJAX (i recommend jquery) to submit the FileForm to the server, and return the markup to insert the File in the Attachments field.