Home Ask Login Register

Developers Planet

Your answer is one click away!

Rana February 2016

How to add widget in django web blog

I am learning django framework using djangogirls tutorial, to reading this tutorial making a blog now i want add popular blog post show widget in this blog but unable to do it, How can i do and add this widget in myblog . please help with details.

Thanks a lot Rana


the flash February 2016

This is rather a generic approach.I hope this helps.

A widget is Django’s representation of an HTML input element. The widget handles the rendering of the HTML, and the extraction of data from a GET/POST dictionary that corresponds to the widget.

You can specify a widget as :

from django import forms

class CommentForm(forms.Form):
    name = forms.CharField()
    url = forms.URLField()
    comment = forms.CharField(widget=forms.Textarea)

Many widgets have optional extra arguments; they can be set when defining the widget on the field. In the following example, the years attribute is set for a SelectDateWidget:

from django import forms

BIRTH_YEAR_CHOICES = ('1980', '1981', '1982')
    ('blue', 'Blue'),
    ('green', 'Green'),
    ('black', 'Black'),

class SimpleForm(forms.Form):
    birth_year = forms.DateField(widget=forms.SelectDateWidget(years=BIRTH_YEAR_CHOICES))
    favorite_colors = forms.MultipleChoiceField(required=False,
        widget=forms.CheckboxSelectMultiple, choices=FAVORITE_COLORS_CHOICES)

Post Status

Asked in February 2016
Viewed 3,566 times
Voted 13
Answered 1 times


Leave an answer

Quote of the day: live life