I have a model form field which is a choice field. Instead of having the default widget:
I would like to have a grid of cards like so:
With each of the choices that would have been in the default widget in a card.
How do I edit the python form to achieve this?
Here is my form:
from django import forms
from . import models
class PostJobForm(forms.ModelForm):
class Meta:
model = models.Job
fields = [
'title',
'description',
'pay',
'category'
]
You will have to move away from default django forms and create the forms using html, css and js the way you want.
When using django forms, it uses default templates.
<form method="post" action"">
<input type="text" id="id_title" name="title">
<!--this input is for title field of your model form-->
</form>
as shown above you can do it for any field that you want
IMP keep the name attribute of input and that of model field same
Related
I'm looking for a Django Model Field to render a HTML5 range slider like:
<input type="range" min="1" max="100" value="50">
This question comes close to Which Django Form Field can provide me a HTML output of <input type="range" />? but it asks for a form where I would search for an admin field for Django Admin.
Further this discussion shows as well how to use an IntegerField as range slider in forms:
https://code.djangoproject.com/ticket/20674
Long story short, can I use forms in Django Admin, or is there already a specific range field?
You can implement a widget, just like Django implements widgets. For example the NumberInput widget [GitHub] looks like:
class NumberInput(Input):
input_type = 'number'
template_name = 'django/forms/widgets/number.html'
We can do this ourselves, for example with a:
# app_name/widgets.py
from django.forms.widgets import NumberInput
class RangeInput(NumberInput):
input_type = 'range'
then you can use this widget with:
# app_name/forms.py
from app_name.widgets import RangeInput
class MyForm(forms.Form):
myint = forms.IntegerField(widget=RangeInput)
I am using django_fiter package for filter data, but it's displaying filter in the input box or some filter in the dropdowns, but I want all filters in the checkbox (like as Django default admin panel filter), please let me know how I can convert it in checkbox filter on my website front panel.
Here is my code for filter view form...
<form method="get">
{{ filter.form|bootstrap }}
<input type="submit" />
</form>
and here is my views.py file code...
class ProductFilter(django_filters.FilterSet):
name = django_filters.CharFilter(lookup_expr='icontains')
class Meta:
model = Product
fields = ['saleprice', 'title','veg_non','brand','supplement']
it's looking like this...
and I want in this format, please let me know how I can customize it.
I am using Django 1.9 class-based generic views, for example CreateView. When I visit the "create" page, some parts are translated (into French in my example), so I know my config and wiring is correct, but the form fields (auto-named by the view) are not (i.e. form.as_p).
How can I get form fields to be used from my translations file? (For example, "Name" is a field, already translated, but not picked up by the form.as_p).
One answer is to list out the fields individually in the template with {% trans %} tags. I was hoping to avoid that.
My example is similar to the one in the docs, so let me repeat that example here. First, code:
from django.views.generic.edit import CreateView
from myapp.models import Author
class AuthorCreate(CreateView):
model = Author
fields = ['name']
Then display template:
<form action="" method="post">{% csrf_token %}
{{ form.as_p }}
<input type="submit" value="Save" />
</form>
Have a look at the Lazy Translation:
This is essential when calls to these functions are located in code paths that are executed at module load time.
This is something that can easily happen when defining models, forms and model forms, because Django implements these such that their fields are actually class-level attributes.
So, if you have a form, you can use ugettext_lazy to translate:
from django.db import models
from django.utils.translation import ugettext_lazy as _
class MyThing(models.Model):
name = models.CharField(help_text=_('This is the help text'))
You can mark names of ForeignKey, ManyToManyField or OneToOneField relationship as translatable by using their verbose_name options:
class MyThing(models.Model):
kind = models.ForeignKey(
ThingKind,
on_delete=models.CASCADE,
related_name='kinds',
verbose_name=_('kind'),
)
Is it possible to resize textarea created by Django?
class TextAreaForm(forms.Form):
input_text_area = forms.CharField(widget=forms.Textarea)
I want to make it bigger but I don't know how. Is it possible to resize it using Django?
<br>
{{ text_area_form }}
<br>
I've found only solutions for resizing forms in Django administration.
The Django docs describe overriding default fields like so:
from django.forms import ModelForm, Textarea
from myapp.models import MyModel
class MyForm(ModelForm)
class Meta:
model = MyModel
widgets = {
'summary': Textarea(attrs={'rows':80, 'cols':20}),
}
This should be helpful to read through: (https://docs.djangoproject.com/en/dev/topics/forms/modelforms/#overriding-the-default-fields)
I have a database table 'travel'.There is a column called travelclasss and it contains values 'economy,business,firstclass'.I have a form
<form id="search-form">
FROM<input type="text" name="f"><br>
TO<input type="text" name="t"><br>
CLASS<input type="text" name="c"><br>
<input type="submit" value="Search">
</form>
If I enter in my form CLASS field either economy 'or' business 'or' firstclass I want to check these names are in my database travelclasss column.
I need implement it in django. How to check particular name is present in database using django.
You can do like this:
models.py
from django.db import models
class Travel(models.Model):
TRAVEL_CHOICES = (
('EC', 'economy'),
('BS', 'business'),
('FC', 'firstclass'),
)
travelclasss = models.CharField(max_length=2,
choices=TRAVEL_CHOICES,
default='EC')
forms.py
from django.forms import ModelForm
from yourapp.models import Travel
class TravelForm(ModelForm):
class Meta:
model = Travel
fields = ['travelclasss']
Then, you will have a drop-down box to choose from.
Here is more doc on ModelForms.
Here is more doc on Choices.