Guest User

Untitled

a guest
Jan 10th, 2018
421
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.64 KB | None | 0 0
  1. # -*- coding: utf-8 -*-
  2. from django import forms
  3. from models import Question,User,Profile
  4. from django.contrib.auth.forms import UserCreationForm
  5.  
  6.  
  7. class QuestionForm(forms.ModelForm):
  8.     def __init__(self, *args, **kwargs):
  9.         self.user = kwargs.pop('user', None)
  10.         super(QuestionForm, self).__init__(*args, **kwargs)
  11.  
  12.     class Meta:
  13.         model = Question
  14.         exclude=['snippet','rating','date_created','author']
  15.         widgets = {
  16.             'title': forms.TextInput(attrs={'class': 'form-control'}),
  17.             'text':forms.Textarea(attrs={'class': 'form-control form-text'}),
  18.             #'author': forms.Select(attrs={'class': 'form-control'}),
  19.             'tags': forms.SelectMultiple(attrs={'class':'form-control'})
  20.         }
  21.         help_texts={
  22.             'title':"Help text for title"
  23.         }
  24.  
  25.     def clean_text(self):
  26.         text=self.cleaned_data['text']
  27.         if(len(text)<10):
  28.             raise forms.ValidationError(u'Too short!')
  29.         return text
  30.  
  31.     def save(self,commit=True):
  32.         user = super(QuestionForm, self).save(commit=False)
  33.         self.cleaned_data['author'] = user.username
  34.         if commit:
  35.             user.save()
  36.         return user
  37.  
  38.  
  39. class RegistrationForm(UserCreationForm):
  40.     email = forms.EmailField(required=True)
  41.     class Meta:
  42.         model = User
  43.         fields = ("username", "email","first_name",'last_name',"password1", "password2")
  44.  
  45.     def save(self, commit=True):
  46.         user = super(RegistrationForm, self).save(commit=False)
  47.         user.email = self.cleaned_data['email']
  48.         if commit:
  49.             user.save()
  50.         return user
Advertisement
Add Comment
Please, Sign In to add comment