Exemple #1
0
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)
Exemple #2
0
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})
Exemple #3
0
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)
Exemple #4
0
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)
Exemple #5
0
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)
Exemple #6
0
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)
Exemple #7
0
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)
Exemple #8
0
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})