Esempio n. 1
0
def edit(request, id=None):
  if not request.user.is_authenticated():
    return HttpResponse("not logged int")

  c = {}
  c.update(csrf(request))

  if id:
      article = get_object_or_404(Article, pk=id)
      if str(article.owner) != str(request.user):
          return HttpResponseRedirect("/")
  else:
      article = Article(author=request.user)

  if request.POST:
      form = EditForm(request.POST, request.FILES, instance=article)
      if form.is_valid():
          form.image = request.POST.get('image', False)
          form.save()

          # If the save was successful, redirect to another page
          return HttpResponseRedirect('/')

  else:
      form = EditForm(instance=article)

  return render(request, 'article/edit_article.html', {
      'form': form,
      'article_id' : id,
      'the_user' : request.user,
      'token' : c,
  }, context_instance=RequestContext(request))