product create

This commit is contained in:
Sharad Ahlawat 2020-03-10 12:40:47 -07:00
parent bf7b9d2a9d
commit 4a04371fb6
4 changed files with 68 additions and 1 deletions

View File

@ -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)

View File

@ -0,0 +1,33 @@
{% extends 'base.html' %}
{% block content %}
{% if error %}
{{ error }}
<br />
<br />
{% endif %}
<h1>Create</h1>
<form method="POST" action="{% url 'create' %}" enctype="multipart/form-data">
{% csrf_token %}
Title:
<br/>
<input type="text" name="title"/>
<br/>
URL:
<br/>
<input type="text" name="url"/>
<br/>
Image:
<br/>
<input type="file" name="image"/>
<br/>
Icon:
<br/>
<input type="file" name="icon"/>
<br/>
Body:
<br/>
<input type="textbox" name="body"/>
<br/><br />
<input type="submit" value="Add Product" class="btn btn-primary">
</form>
{% endblock %}

6
products/urls.py Normal file
View File

@ -0,0 +1,6 @@
from django.urls import path
from . import views
urlpatterns = [
path('create', views.create, name='create'),
]

View File

@ -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')