get_object_or_404 is undefined

I'm trying to update my Profile model with some data that I get from a form, but I get this error

name 'get_object_or_404' is not defined

Here's my code for the view (It's pretty basic at this point)

from django.shortcuts import render
from django.contrib import messages
from django.contrib.auth.models import User
from users import models
from users.models import Profile
from .forms import WeightForm
# Create your views here.
def home(request):
    profile = get_object_or_404(pk=id)
    form = WeightForm(request.POST, instance=profile)
    if form.is_valid():
       form.save

return render(request, 'Landing/index.html',{'form':form})

Upvotes: 0

Views: 276

Answers (1)

willeM_ Van Onsem
willeM_ Van Onsem

Reputation: 476729

You need to import it. Furthermore in a get_object_or_404(…) [Django-doc], you first specify a model class or a queryset, so in this case get_object_or_404(Profile, …):

from django.shortcuts import get_object_or_404

def home(request):
    profile = get_object_or_404(Profile, pk=id)
    form = WeightForm(request.POST, instance=profile)
    if form.is_valid():
       form.save()
    return render(request, 'Landing/index.html',{'form':form})

Note: In case of a successful POST request, you should make a redirect [Django-doc] to implement the Post/Redirect/Get pattern [wiki]. This avoids that you make the same POST request when the user refreshes the browser.

Upvotes: 1

Related Questions