diff --git a/producthunt/urls.py b/producthunt/urls.py index 45086f3..3291e50 100644 --- a/producthunt/urls.py +++ b/producthunt/urls.py @@ -23,4 +23,5 @@ urlpatterns = [ path('admin/', admin.site.urls), path('', views.home, name='home'), path('accounts/', include('accounts.urls')), + path('products/', include('products.urls')), ] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT) diff --git a/products/templates/products/create.html b/products/templates/products/create.html new file mode 100644 index 0000000..fdf33a2 --- /dev/null +++ b/products/templates/products/create.html @@ -0,0 +1,33 @@ +{% extends 'base.html' %} +{% block content %} + {% if error %} + {{ error }} +
+
+ {% endif %} +

Create

+
+ {% csrf_token %} + Title: +
+ +
+ URL: +
+ +
+ Image: +
+ +
+ Icon: +
+ +
+ Body: +
+ +

+ +
+{% endblock %} diff --git a/products/urls.py b/products/urls.py new file mode 100644 index 0000000..02d4f92 --- /dev/null +++ b/products/urls.py @@ -0,0 +1,6 @@ +from django.urls import path +from . import views + +urlpatterns = [ + path('create', views.create, name='create'), + ] diff --git a/products/views.py b/products/views.py index d6b02d4..b1d7dcd 100644 --- a/products/views.py +++ b/products/views.py @@ -1,5 +1,32 @@ -from django.shortcuts import render +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')