producthunt/products/views.py
2020-03-10 12:40:47 -07:00

33 lines
1.2 KiB
Python

from django.shortcuts import render, redirect
from django.contrib.auth.decorators import login_required
from .models import Product
from django.utils import timezone
def home(request):
return render(request, 'products/home.html')
@login_required()
def create(request):
if request.method == 'POST':
if request.POST['title'] and request.POST['url'] and request.FILES['image'] and request.FILES['icon'] and \
request.POST['body']:
product = Product()
product.title = request.POST['title']
if request.POST['url'].startswith('http'):
product.url = request.POST['url']
else:
product.url = 'http://' + request.POST['url']
product.image = request.FILES['image']
product.icon = request.FILES['icon']
product.body = request.POST['body']
product.pubdate = timezone.datetime.now()
product.hunter = request.user
product.save()
return redirect('home')
else:
return render(request, 'products/create.html', {'error': 'all fields required'})
else:
return render(request, 'products/create.html')