I want to get an object value from a model. After trying the get() method, I’ve been unable to make it work.And I’m getting the below error. I have a page that display the name of a place to users, so if a user see a place he likes, he will go ahead and fill the form.And in the form, I want to get the name of the place automatically from another model, I don’t want users to fill the name of the place. Below are my codes
NameError at /welcome/
global name 'name' is not defined
Models
class Fin(models.Model):
user=models.ForeignKey(User)
name=models.CharField(max_length=100)
def __unicode__(self):
return self.user
class Place(models.Model):
user=models.ForeignKey(User)
mall=models.ForeignKey(Fin)
full_name=models.CharField(max_length=100)
e_mail=models.EmailField(max_length=100)
phone_no=models.CharField(max_length=100)
pub_date=models.DateTimeField()
def __unicode__(self):
return self.full_name
class Meta:
ordering=['-pub_date']
class PlaceForm(ModelForm):
class Meta:
model=Place
fields=('full_name','e_mail','phone_no')
exclude=('user','pub_date','mall')
Views:
def place_me(request):
if request.method=="POST":
form=PlaceForm(request.POST)
if form.is_valid():
data=form.cleaned_data
newbooks=Place(
user=request.user,
pub_date=datetime.datetime.now(),
mall=Fin.objects.get(name),
full_name=data['full_name'],
e_mail=data['e_mail'],
phone_no=data['phone_no'])
newbooks.save()
return HttpResponse('Thanks for choosing themall, we will contact you as soon as possible. Go grab a beer :)')
else:
return HttpResponse('Some fields are not filled correctly')
else:
return render_to_response('buuk.html',{'PlaceForm':PlaceForm},context_instance=RequestContext(request))
This line
mall=Fin.objects.get(name)you have not declared thenamevariable any where in your viewplace_me, hence it is undefined.Also the way you are passing argument in
.getis wrong, it has to be like this: