how to combine views in one template

36 views
Skip to first unread message

Mark Alan Jones

unread,
Jun 15, 2017, 8:05:19 PM6/15/17
to Django users
Hello Django Community,

I am new to Django and tackling my first app outside of the tutorial. The app I'm working on is a simple ticket system for build requests at my IT Company. I only have one model 'Build', and I have checkboxes that denote how far the build has been done. 

I have a page that shows all build requests, and then a separate page with the details of each build request. I want to add a form to this builddetails template, but I'm not sure what is the correct way to do this when the template is already pulling info from the model. Every tutorial I've followed has shown using a form under a separate URL, but that is not viable.

my current view for builddetails is this:

def builddetails(request, id):
    build = Build.objects.get(pk=id);
    return render(request, 'app/builddetails.html', { 'build': build });

How do I combine this with a ModelForm on the same page? 

the ModelForm I was trying to use looks like this:

class BuildForm(forms.ModelForm):
    class Meta:
        model = Build,
        fields = ['notes', 'build_engineer', 'latest_bios','bios_settings', 'hot_keys_tested', 'office_activated']

If I need to provide any more information please let me know, and thank you so much in advance for any help you can offer. 

Matthew Pava

unread,
Jun 16, 2017, 9:27:31 AM6/16/17
to django...@googlegroups.com

Hi Mark,

You would pass the form in the context of the template in the view.  You can pass pretty much anything to a template through the context.

 

def builddetails(request, id):

    build = Build.objects.get(pk=id)

    form = BuildForm(request.POST or None)

    if form.is_valid():

           form.save()

    return render(request, 'app/builddetails.html', { 'build': build, ‘form’: form })

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
To post to this group, send email to django...@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/733168e2-2559-4726-9067-dc2a23397bb7%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

yingi keme

unread,
Jun 16, 2017, 10:23:39 AM6/16/17
to django...@googlegroups.com
def builddetails(request, id):
    build = Build.objects.get(pk=id)
    Obj = BuildForm(request.POST)
    Obj.save()
    return render(request, .....)

Mark Alan Jones

unread,
Jun 17, 2017, 7:51:00 PM6/17/17
to Django users, Matthe...@iss.com
Thank you very much for the help Matthew, that is absolutely spot on, and greatly appreciated.

My view now looks like this:
def builddetails(request, id):
    if request.method == 'POST':
        form = BuildForm(request.POST or None)
        if form.is_valid():
            form.save()
            return HttpResponseRedirect('/builds')

    else:
        form = BuildForm()
        build = Build.objects.get(pk=id)
    return render(request, 'app/builddetails.html', { 'build': build, 'form': form })

It appears to be working, but I get an error when saving the form: 

IntegrityError at /builds/8

NOT NULL constraint failed: app_build.requested_by_id
I want to use this form to update records in the database - do I need to do something special in the view so that it knows which record to update?

Thanks again,

Mark

To post to this group, send email to djang...@googlegroups.com.

Mark Alan Jones

unread,
Jun 17, 2017, 8:03:15 PM6/17/17
to Django users, Matthe...@iss.com
I've just worked it out! 

Posting for any other hopeless newbies

def builddetails(request, id):
    if request.method == 'POST':
        
        form = BuildForm(request.POST or None)
        if form.is_valid():
            build = Build.objects.get(pk=id)
            build_form = BuildForm(request.POST, instance=build)
            build_form.save()
            return HttpResponseRedirect('/builds')

    else:
        form = BuildForm()
        build = Build.objects.get(pk=id)
    return render(request, 'app/builddetails.html', { 'build': build, 'form': form })

Thanks all for help
Reply all
Reply to author
Forward
0 new messages