我们从Python开源项目中,提取了以下6个代码示例,用于说明如何使用django.forms()。
def test_clean_file_upload_form_oversize_data(self): t = workflows.create_instance.CustomizeAction(self.request, {}) upload_str = 'user data' files = {'script_upload': self.SimpleFile('script_name', upload_str, (16 * 1024) + 1)} self.assertRaises( forms.ValidationError, t.clean_uploaded_files, 'script', files)
def test_clean_file_upload_form_invalid_data(self): t = workflows.create_instance.CustomizeAction(self.request, {}) upload_str = b'\x81' files = {'script_upload': self.SimpleFile('script_name', upload_str, sys.getsizeof(upload_str))} self.assertRaises( forms.ValidationError, t.clean_uploaded_files, 'script', files)
def edit_profile(request): if request.method =='POST': form=EditProfileForm(request.POST, instance=request.user) if form.is_valid(): form.save() return redirect('/') else: form=EditProfileForm(instance=request.user) args={'forms':form} return render(request,'user_change.html', {'form':form})
def edit_user_profile(request): if request.method =='POST': form=EditUserProfileForm(request.POST, instance=request.user.userprofile) if form.is_valid(): form.save() return redirect('/') else: form=EditUserProfileForm(instance=request.user.userprofile) args={'forms':form} return render(request,'user_profile_change.html', {'form':form})
def get_initial(self): """ Returns the initial data to use for forms on this view. """ initial = {} params = self.request.GET subject = Subject.objects.get(id=params['subject_id']) topics = subject.topic_subject.all() initial['subject'] = subject initial['topic'] = topics initial['end_date'] = date.today() return initial
def register(request): # Like before, get the request's context. context = RequestContext(request) # A boolean value for telling the template whether the registration was successful. # Set to False initially. Code changes value to True when registration succeeds. registered = False # If it's a HTTP POST, we're interested in processing form data. if request.method == 'POST': # Attempt to grab information from the raw form information. # Note that we make use of both UserForm. user_form = UserForm(data=request.POST) # If the two forms are valid... if user_form.is_valid(): # Save the user's form data to the database. user = user_form.save() # Now we hash the password with the set_password method. # Once hashed, we can update the user object. user.set_password(user.password) user.save() # Update our variable to tell the template registration was successful. registered = True # Invalid form or forms - mistakes or something else? # Print problems to the terminal. # They'll also be shown to the user. else: print user_form.errors # Not a HTTP POST, so we render our form using two ModelForm instances. # These forms will be blank, ready for user input. else: user_form = UserForm() # Render the template depending on the context. return render_to_response( 'register.html', {'user_form': user_form, 'registered': registered}, context)