can't save form content to database, help plsss!!
- by dana
i'm trying to save 100 caracters form user in a 'microblog' minimal application.
my code seems to not have any mystakes, but doesn't work.
the mistake is in views.py, i can't save the foreign key to user table
models.py looks like this:
class NewManager(models.Manager):
def create_post(self, post, username):
new = self.model(post=post, created_by=username)
new.save()
return new
class New(models.Model):
post = models.CharField(max_length=120)
date = models.DateTimeField(auto_now_add=True)
created_by = models.ForeignKey(User, blank=True)
objects = NewManager()
class NewForm(ModelForm):
class Meta:
model = New
fields = ['post']
# widgets = {'post': Textarea(attrs={'cols': 80, 'rows': 20})
def save_new(request):
if request.method == 'POST':
created_by = User.objects.get(created_by = user)
date = request.POST.get('date', '')
post = request.POST.get('post', '')
new_obj = New(post=post, date=date, created_by=created_by)
new_obj.save()
return HttpResponseRedirect('/')
else:
form = NewForm()
return render_to_response('news/new_form.html', {'form': form},context_instance=RequestContext(request))
i didn't mention imports here - they're done right, anyway.
my mistake is in views.py, when i try to save it says:
local variable 'created_by' referenced before assignment
it i put created_py as a parameter, the save needs more parameters... it is really weird
help please!!