def add_view(request): if request.method == "POST": form = EntryForm(request.POST) if form.is_valid(): form.save() return HttpResponseRedirect('/entries/home/') else: form = EntryForm() context = {'form':form } return render(request,'entries/adds.html',context)
def update_entry(request, id): """ Updates an entry in the database """ entry = get_object_or_404(Entry, id=id) form = EntryForm(request.POST or None, instance=entry) if form.is_valid(): form.save() return redirect('entries') return render(request, 'create_entry.html', {'form': form})
def add(request): if request.method == 'POST': # checks if the method is a post request -is posting something form = EntryForm(request.POST) if form.is_valid(): form.save() # only works if you used EntryForm return redirect('home') # the name that was given to the url of index else: form = EntryForm() # checks if the method is a get method context = {'form': form} return render(request, 'entries/add.html', context)
def add(request): if request.method == 'POST': form = EntryForm(request.POST) if form.is_valid(): form.save() return redirect('home') else: form = EntryForm() context = {'form': form} return render(request, 'entries/add.html', context)
def edit_entry(request, entry_id): context = RequestContext(request) if request.method == 'POST': entry = get_entry(int(entry_id)) form = EntryForm(instance=entry, data=request.POST) if form.is_valid(): form.save() return redirect('entry-show', entry_id) else: entry = get_entry(int(entry_id)) form = EntryForm(instance=entry) context_dict = {'form': form} return render_to_response('entries/edit_entry.html', context_dict, context)
def test_valid_data(self): # Test for valid data form = EntryForm({ 'title': "Entry Test Title", 'event': "Entry Test Event" }, entry=self.entry) self.assertTrue(form.is_valid()) my_entry = form.save() self.assertEqual(my_entry.title, "Entry Test Title") self.assertEqual(my_entry.event, "Entry Test Event") self.assertEqual(my_entry.entry, self.entry)
def create_entry(request): context = RequestContext(request) if request.method == 'POST': form = EntryForm(request.POST) if form.is_valid(): new_id = form.save().pk return redirect('entry-show', new_id) else: form = EntryForm() context_dict = {'form': form} return render_to_response('entries/create_entry.html', context_dict, context)
def create_entry(request): """ Create a new entry in the database """ if request.method == 'POST': form = EntryForm(request.POST or None) if form.is_valid(): # Validate form inputs title = form.cleaned_data['title'] event = form.cleaned_data['event'] """ prevent autosaving of data before appending the authour and date posted info. """ post = form.save(commit=False) post.authour = request.user post.published_date = timezone.now() post.save() return redirect("/entries/") else: form = EntryForm() return render(request, "create_entry.html", {"form": form})