daily pastebin goal
31%
SHARE
TWEET

Untitled

a guest Jun 19th, 2017 50 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. --- models.py ---
  2.  
  3. class Feedback(models.Model):
  4.     user = models.ForeignKey(User)
  5.     comment = models.TextField(verbose_name=_('Feedback / Comments'))
  6.  
  7.     def __unicode__(self):
  8.         return str(self.user)
  9.  
  10.  
  11. --- forms.py ---
  12. from models import Feedback
  13. from django.forms import ModelForm
  14. from django.contrib.auth.models import User
  15.  
  16. class FeedbackForm(ModelForm):
  17.     def __init__(self,theUser,*args,**kwargs):
  18.         super (FeedbackForm,self ).__init__(*args,**kwargs) # populates the post
  19.         self.user = theUser
  20.         self.instance.user = user
  21.  
  22.     class Meta:
  23.         model = Feedback
  24.         fields = ('comment',)
  25.  
  26.  
  27. --- views.py ---
  28. def feedback(request):
  29.     theUser = request.user
  30.     if request.method == 'POST': # If the form has been submitted...
  31.         form = FeedbackForm(theUser,request.POST) # A form bound to the POST data
  32.         if form.is_valid(): # All validation rules pass
  33.             form.save()
  34.             return HttpResponseRedirect('/thanks/') # Redirect after POST
  35.     else:
  36.         #usrFeedback = Feedback.objects.filter(user=request.user)[0]
  37.         #form = FeedbackForm(request.POST,instance=usrFeedback) # An unbound form
  38.         print request.user
  39.         form = FeedbackForm(theUser) # An unbound form
  40.  
  41.     return render_to_response('feedback.html', {
  42.         'form': form,
  43.         'user': request.user
  44.     })
RAW Paste Data
Top