def new_topic(request): if request.method != 'POST': form = TopicForm else: form = TopicForm(request.POST) if form.is_valid(): new_topic = form.save(commit=False) new_topic.owner = request.user new_topic.save() return HttpResponseRedirect(reverse('learning_logs:topics')) context = {'form': form} return render(request, 'learning_logs/new_topic.html', context)
def new_topic(request): #未提交数据:创建一个新表单 if request.method!='POST': form=TopicForm() else: form=TopicForm(request.POST) if form.is_valid(): form.save() return HttpResponseRedirect(reversed('learning_logs:topics')) context={'form':form} return render(request,'learning_logs/new_topic.html',context)
def new_topic(request): """se for get ira criar um form em branco """ if request.method != 'POST': form = TopicForm() else: form = TopicForm(request.POST) """verifica se os dados fornecidos sao validos , caso sim grava os dados no banco com o comando save""" if form.is_valid(): form.save() """Redireciona para a pagina topics""" return HttpResponseRedirect(reverse('topics')) context = {'form': form} return render(request, 'learning_logs/new_topic.html', context)
def new_topic(request): if request.method != 'POST': form = TopicForm() else: form = TopicForm(request.POST) if form.is_valid(): new_topic = form.save(commit=False) new_topic.owner = request.user new_topic.save() return redirect('topics') return render(request, 'learning_logs/new_topic.html', {'form': form})
def new_topic(request): """Add a new topic""" if request.method != 'POST': #No data submitted, create a blank form form = TopicForm() else: #Post data submitted, process data form = TopicForm(request.POST) if form.is_valid(): new_topic = form.save(commit=False) new_topic.owner = request.user new_topic.save() return HttpResponseRedirect(reverse('learning_logs:topics')) return render(request, 'new_topic.txt', {'form': form})
def new_topic(request): """Allows user to create a new topic.""" if request.method == 'POST': form = TopicForm(request.POST) if form.is_valid(): new_topic = form.save(commit=False) new_topic.owner = request.user new_topic.save() return redirect('/topics/') form = TopicForm() return render(request, 'learning_logs/new_topic.html', { 'form': form, })
def new_topic(request): if request.method != 'POST': # 判断请求的方法 form = TopicForm() else: form = TopicForm(request.POST) if form.is_valid(): # 验证表单字段是否都填写了(默认都必须填写) new_topic = form.save(commit=False) new_topic.owner = request.user new_topic.save() # 将表单数据写入数据库 return HttpResponseRedirect(reverse('learning_logs:topics')) # 重定向 URL context = {'form': form} return render(request, 'learning_logs/new_topic.html', context)
def new_topic(request): """添加topic""" if request.method != "POST": form = TopicForm() else: form = TopicForm(request.POST) if form.is_valid(): new_topic = form.save() new_topic.owner = request.user new_topic.save() return HttpResponseRedirect(reverse("topics")) context = {"form": form} return render(request, "learning_logs/new_topic.html", context)
def new_topic(request): """Defines a new topic.""" if request.method != 'POST': form = TopicForm() else: form = TopicForm(data=request.POST) if form.is_valid(): new_topic = form.save(commit=False) new_topic.owner = request.user new_topic.save() return redirect('learning_logs:topics') context = {'form': form} return render(request, 'learning_logs/new_topic.html', context)
def addtopicview(request): """Form for a new topic""" form = TopicForm(request.POST or None) if form.is_valid(): new_topic = form.save(commit = False) new_topic.owner = request.user new_topic.save() form = TopicForm() else: print(form.errors) context = { "form" : form, } return render(request, "topiccreate.html", context)
def new_topic(request): """添加新主题""" if request.method != 'POST': #未提交数据: 创建一个新的表单 form = TopicForm() else: #POST提交的数据,对数据进行处理 form = TopicForm(request.POST) if form.is_valid(): new_topic = form.save(commit=False) #调用form.save(),并传递实参commit=False,这是因为我们先修改新主题,再将其保存到数据库中 new_topic.owner = request.user #将新主题的owner属性设置为当前用户 new_topic.save() #在主题包含所有必不可少的数据,将被成功地保存。 return HttpResponseRedirect(reverse('learning_logs:topics')) context = {'form':form} return render(request,'new_topic.html',context)
def new_topic(request): """添加新主题""" if request.method != 'POST': # 未提交数据:创建一个新表单 form = TopicForm() else: # POST提交的数据,对数据进行处理 form = TopicForm(request.POST) if form.is_valid(): new_topic = form.save(commit=False) new_topic.owner = request.user new_topic.save() return HttpResponseRedirect(reverse('learning_logs:topics')) context = {'form': form} return render(request, 'learning_logs/new_topic.html', context)
def new_topic(request): ''' adds a new topic ''' if request.method != 'POST': #no data was submitted, create a blank form form = TopicForm() else: #data was submitted save new topic form = TopicForm(request.POST) if form.is_valid(): #Associate new topics with the current user newTopic = form.save( commit=False) newTopic.owner = request.user newTopic.save() return redirect('learning_logs:topics') #display blank form or invalid form context = {'form': form} return render(request, 'learning_logs/new_topic.html', context)
def test_topic_form_valid_data(self): form = TopicForm(data={'text': 'topic1'}) self.assertTrue(form.is_valid())
def test_topic_form_no_data(self): form = TopicForm(data={}) self.assertFalse(form.is_valid()) self.assertEqual(len(form.errors), 1)