how to combine views in one template

조회수 36회
읽지 않은 첫 메시지로 건너뛰기

Mark Alan Jones

읽지 않음,
2017. 6. 15. 오후 8:05:1917. 6. 15.
받는사람 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

읽지 않음,
2017. 6. 16. 오전 9:27:3117. 6. 16.
받는사람 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

읽지 않음,
2017. 6. 16. 오전 10:23:3917. 6. 16.
받는사람 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

읽지 않음,
2017. 6. 17. 오후 7:51:0017. 6. 17.
받는사람 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

읽지 않음,
2017. 6. 17. 오후 8:03:1517. 6. 17.
받는사람 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
전체답장
작성자에게 답글
전달
새 메시지 0개