Search code examples
pythondjangoformsmodelsdisplay

Print the values of a model in a form and edit them Django


Started of learning Django python a week or two ago, so please keep this in mind.

What I want is for data to be displayed in forms and it will be edited with in the same forms, for example:

enter image description here

Where it says: First Name Last Name email

I would like it to pull the data from the database and have it written out inside of forms.

First Name Kevin Last Name Clark email [email protected]

and so on for each entry. Like it is written down bellow where it says you created these. So one editing form with values written out for each set of entry's.

I hope that I have managed to pose a clear question and I do not believe to the best of my knowledge that it is a duplicate, if it is I am so sorry.

OS:Fedora IDE:Eclipse Django Python

views.py

def edit(request):
    instance = New.objects.all()
    data = { "firstName" : "Clark",
                  "lastName": "Kevin"}
    form = NewForm(request.POST) #instance=instance)
    print form.instance.firstName

    if(form.is_valid()):
        instance = form.save(commit=False)
        instance.save()

    context = {
               "object_list" : instance,
               "formset" : form,
               "instance" : instance
    }

    return render(request, "secondTry/new.html", context)

models.py

class New(models.Model):
firstName = models.CharField(max_length=50, null=True)
lastName = models.CharField(max_length=50, null=True)
email = models.EmailField()
timestamp = models.DateTimeField(auto_now_add=True, auto_now=False)
updated = models.DateTimeField(auto_now_add=False, auto_now=True)

forms.py

class NewForm(ModelForm):
class Meta:
    model = New
    fields = [
              "firstName", 
              "lastName", 
              "email"
              ]

html

<!DOCTYPE html>
<html>
    <head>

    </head>

    <body>
        <h1>Create a New One</h1>
            <form method='post' action=""> {% csrf_token %}
                <table>
                    {{ formset }}
                </table>
                <input type='submit' value="Submit">
            </form>

            {{ instance }} {% csrf_token %}

            <h2>You Created These</h2>
            {% for obj in object_list %}
                    {{ obj.firstName }} 
                    {{ obj.lastName }} 
                    {{ obj.email }} <br/><br/>
                {% endfor %}






    </body>
</html>

Note, I am not using all the variables in def edit as I am going back and forth trying to figure this out so it might be a bit messy.

Thank you all so much for your time.


Solution

  • If I'm understanding your question correctly, you just want the initial values to show up in the form? In that case you're looking for https://docs.djangoproject.com/en/1.9/ref/forms/api/#dynamic-initial-values

    When you're creating an instance of your Form, do something like name_form = Form(request.POST, initial=db_instance) Assuming that your edit view is already loading the instance from the DB somehow.