Skip to content

Commit

Permalink
Adiciona formulário para receitas
Browse files Browse the repository at this point in the history
  • Loading branch information
brnocesar committed Jan 22, 2021
1 parent 3eee356 commit 1b4275f
Showing 1 changed file with 31 additions and 2 deletions.
33 changes: 31 additions & 2 deletions apps/receitas/views.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from django.shortcuts import render, get_object_or_404
from django.shortcuts import render, get_object_or_404, redirect
from .models import Receita
from django.contrib.auth.models import User

def index(request):

Expand All @@ -16,4 +17,32 @@ def receita(request, receita_id):
return render(request, 'receitas/receita.html', {'receita': get_object_or_404(Receita, pk=receita_id)})

def create(request):
return render(request, 'receitas/create.html')
if not request.user.is_authenticated:
return redirect('index')

if request.method == 'POST':
nome = request.POST['nome']
ingredientes = request.POST['ingredientes']
modo_preparo = request.POST['modo_preparo']
tempo_preparo = request.POST['tempo_preparo']
rendimento = request.POST['rendimento']
categoria = request.POST['categoria']
foto = request.FILES['foto']
# user = request.user # porque nao assim?
user = get_object_or_404(User, pk=request.user.id)

receita = Receita.objects.create(
pessoa=user,
nome=nome,
ingredientes=ingredientes,
modo_preparo=modo_preparo,
tempo_preparo=tempo_preparo,
rendimento=rendimento,
categoria=categoria,
foto=foto
)
receita.save()

return redirect('dashboard')

return render(request, 'receitas/create.html')

0 comments on commit 1b4275f

Please sign in to comment.