SHARE
TWEET

Untitled

a guest Jun 19th, 2017 51 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.fields['comment'].queryset = User.objects.filter(username=theUser)
  20.  
  21.     class Meta:
  22.         model = Feedback
  23.  
  24.  
  25.  
  26. --- views.py ---
  27. def feedback(request):
  28.     theUser = get_object_or_404(User, username=request.user)
  29.     if request.method == 'POST': # If the form has been submitted...
  30.         form = FeedbackForm(theUser,request.POST) # A form bound to the POST data
  31.         if form.is_valid(): # All validation rules pass
  32.             form.save()
  33.             return HttpResponseRedirect('/thanks/') # Redirect after POST
  34.     else:
  35.         #usrFeedback = Feedback.objects.filter(user=request.user)[0]
  36.         #form = FeedbackForm(request.POST,instance=usrFeedback) # An unbound form
  37.         print request.user
  38.         form = FeedbackForm(theUser) # An unbound form
  39.  
  40.     return render_to_response('feedback.html', {
  41.         'form': form,
  42.         'user': request.user
  43.     })
RAW Paste Data
Top