saini tor
saini tor

Reputation: 233

How to Edit Data in Django dropdown HTML form?

I have a form where I am editing single leads data, I submitted data using the Django default admin panel. But now I am editing data on my default form. So my concern is, if I edit any leads then the default value should be selected in the dropdown which is available in the database, and if I want to change the dropdown value then I can select it from the dropdown list.

Here are my Models.py file

class Leads(models.Model):
   SOURCE = (
    ('Facebook', 'Facebook'),
    ('Website', 'Website'),
    ('PPC', 'PPC'),
    ('Reference', 'Reference'),
    ('Other Source', 'Other Source')
)
    
name = models.CharField(max_length=50, default=None)
email = models.EmailField(max_length=50, null=True, blank=True)
phone = models.CharField(max_length=10, default=None)
source = models.CharField(choices=SOURCE, default=2, max_length=100)

here are my urls.py file

path(r'^leads/<int:id>/change',views.leads_edit, name="leads_edit"),

here is my views.py file...

def leads_edit(request, id):
    leads = Leads.objects.get(id=id)
    context = {'leads':leads}
    template_name = 'file/edit.html'
    return render(request, template_name, context)

here is my edit.html file, where is am trying to edit using the dropdown field...

{% for obj in leads.SOURCE %}
    <option value="{{ leads.source }}">{{ obj}}</option>
{% endfor %}

Please check my code and let me know how I can display the selected field from the database, and how I can display the rest other fields in the dropdown list. Because I can change the dropdown fields.

Upvotes: 5

Views: 2347

Answers (2)

Mike Dinder
Mike Dinder

Reputation: 99

I'm not sure but pretty sure you might be looking for something like this.

SOURCE = (
    ('', '--------'),
    ('Facebook', 'Facebook'),
    ('Website', 'Website'),
    ('PPC', 'PPC'),
    ('Reference', 'Reference'),
    ('Other Source', 'Other Source')
)
source = models.CharField(choices=SOURCE, default='Website', max_length=100)

and defaulting to nothing would be

source = models.CharField(choices=SOURCE, default='', max_length=100)

where you wrote default = 2, I don't think that will work, since the value in your list of choices is a string. If you write them like this instead, then you could potentially default to a numeric value, even while still keeping it a charfield. You could convert to other PositiveInteger Fields as well if you wanted to go that route. The second parameter is just a label to your choice.

SOURCE = (
    (0, '--------'),
    (1, 'Facebook'),
    (2, 'Website'),
    (3, 'PPC'),
    (4, 'Reference'),
    (5, 'Other Source')
)

but also, wherever your form is actually located, you should probably do something like this in that file in addition to the above. For required, choose one or the other.

source = forms.CharField(
    widget = forms.Select(
        choices = SOURCE
    ),
    required = True/False,
    max_length = 100,
)

Then in your form class you can try to set the value of the field on the __init__ per whatever parameters and checks you need to perform. You will have to pass request into the form class when you use the class or just write this instead *def __init__(self, *args, **kwargs): instead, depends on if you need request to find your solution and it looks like you might. Passing in request would look something like this form = LeadsForm(request) or form = LeadsForm(request.POST) and if you need your id form = LeadsForm(request, some_id)

class LeadsForm(forms.Form):
    source = forms.CharField(
        widget = forms.Select(
            choices = SOURCE
        ),
        required = True/False,
        max_length = 100,
    )

    def __init__(self, request, some_id, *args, **kwargs):
        super(LeadsForm, self).__init__(*args, **kwargs)

        {{ your_lead_magic_here... }}

        self.fields['source'].initial = {{ your_calculated_lead_value_here }}

Upvotes: 0

Robot
Robot

Reputation: 86

Please try this, I hope it will help you...

{% for id, obj in leads.SOURCE %}
     <option value="{{ id }}"{% if leads.source == id %} selected="selected"{% endif %}>{{ obj }}</option>
{% endfor %}

Upvotes: 3

Related Questions