Error 404 NOT FOUND occurs when trying to GET from django - django

I am getting the 404 error
Failed to load resource: the server responded with a status of 404 (Not Found)
127.0.0.1:8000/api/v1/products/$%7Bcategory_slug%7D/$%7Bproduct_slug%7D:1
my code for product/models.py:
from io import BytesIO
from PIL import Image
from django.core.files import File
from django.db import models
class Category (models.Model):
name = models.CharField(max_length=255)
slug = models.SlugField(null=True)
class Meta:
ordering = ('name',)
def __str__(self) :
return self.name
def get_absolute_url(self):
return f'/{self.slug}/'
class Product(models.Model):
category = models.ForeignKey(Category, related_name='products', on_delete=models.CASCADE)
name = models.CharField(max_length=255)
slug = models.SlugField(null=True)
description = models.TextField(blank=True, null=True)
price = models.DecimalField(max_digits=6, decimal_places=2)
image = models.ImageField(upload_to='uploads/', blank=True, null=True)
thumbnail = models.ImageField(upload_to='uploads/', blank=True, null=True)
date_added = models.DateTimeField(auto_now_add=True)
class Meta:
ordering = ('-date_added',)
def __str__(self) :
return self.name
def get_absolute_url(self):
return f'/{self.category.slug}/{self.slug}/'
def get_image(self):
if self.image:
return 'http://127.0.0.1:8000' + self.image.url
return ''
def get_thumbnail(self):
if self.thumbnail:
return 'http://127.0.0.1:8000' + self.thumbnail.url
else:
if self.image:
self.thumbnail = self.make_thumbnail(self.image)
self.save()
return 'http://127.0.0.1:8000' + self.thumbnail.url
else:
return ''
def make_thumbnail(self, image, size=(300, 200)):
img = Image.open(image)
img.convert('RGB')
img.thumbnail(size)
thumb_io = BytesIO()
img.save(thumb_io, 'JPEG', quality=85)
thumbnail = File(thumb_io, name=image.name)
return thumbnail
the product page,product.vue code:
<template>
<div class="page-product">
<div class="column is-multiline">
<div class="column is-9">
<figure class="image mb-6">
<img v-bind:src="product.get_image">
</figure>
<h1 class="title">{{ product.name }}</h1>
<p>{{ product.description }}</p>
</div>
<div class="column is-3">
<h2 class="subtitle">Information</h2>
<p><strong>Price: </strong>Ksh{{ product.price }}</p>
<div class="field has-addons mt-6">
<div class="control">
<input type="number" class="input" min="1" v-model="quantity">
</div>
<div class="control">
<a class="button is-dark" #click="addToCart">Add to Cart</a>
</div>
</div>
</div>
</div>
</div>
</template>
<script>
import axios from 'axios'
import { toast } from 'bulma-toast'
export default {
name: 'Product',
data() {
return {
product: {},
quantity: 1
}
},
mounted() {
this.getProduct()
},
methods: {
getProduct() {
const category_slug = this.$route.params.category_slug
const product_slug = this.$route.params.product_slug
axios
.get('/api/v1/products/${category_slug}/${product_slug}')
.then(response => {
this.product = response.data
})
.catch(error => {
console.log(error)
})
},
addToCart() {
if (isNaN(this.quantity) || this.quantity < 1) {
this.quantity = 1
}
const item = {
product: this.product,
quantity: this.quantity
}
this.$store.commit('addToCart', item)
toast({
message: 'The product was added to the cart!',
type: 'is-success',
dismissible: true,
pauseOnHover: true,
duration: 2000,
position: 'bottom-right',
})
}
}
}
</script>
my product/urls.py code is:
from django.urls import path
from product import views
urlpatterns = [
path('latest-products/', views.LatestProductsList.as_view()),
path('products/<slug:category_slug>/<slug:product_slug>/', views.ProductDetail.as_view()),
]
my main.js in for the vue app is :
import { createApp } from 'vue'
import App from './App.vue'
import router from './router'
import store from './store'
import axios from 'axios'
axios.defaults.baseURL = 'http://127.0.0.1:8000'
createApp(App).use(store).use(router, axios).mount('#app')

You make a request with "/api/v1/" in axios. Did you forget that inside Django URL patterns or "axios.defaults.baseURL"?
axios
.get('/api/v1/products/${category_slug}/${product_slug}')
.then(response => {
this.product = response.data
})
.catch(error => {
console.log(error)
})

Related

Add products with different sizes and price to Cart | Django

I use session's to create a shopping cart. I have a product model and a model for Product-sizes, which I define the size-title(XL , etc) and price of product in it . So that I can have products with different sizes as well as different prices. But my logic has some problems
if I add product_1 with size A , it's ok ...
if I add product_1 with size B , it's ok ...
but when I'm trying to add same product with different sizes at same time the cart only shows the price of first size
#shop/models.py
class ProductSizes(models.Model):
name = models.CharField(max_length=50)
price = models.IntegerField()
picture = models.ImageField(upload_to="products/%Y/%m/%d" )
class Product(models.Model):
name = models.CharField(max_length=100)
size = models.ManyToManyField(ProductSizes , blank=True , related_name="sizes" )
price = models.IntegerField()
#cart/cart.py
from django.conf import settings
from shop.models import Product
class Cart:
def __init__(self,request):
self.session = request.session
cart = self.session.get(settings.CART_SESSION_ID)
if not cart:
cart = self.session[settings.CART_SESSION_ID] = {}
self.cart = cart
# add to cart
def add(self,product,size_,quantity=1,override_quantity=False):
product_id = str(product.id)
print(product.size.name)
if product_id not in self.cart :
if size_ != None:
if size_.off_price:
self.cart[product_id] = {'quantity':0,'price':size_.off_price}
elif size_.price:
self.cart[product_id] = {'quantity':0,'price':size_.price}
else:
if product.off_price:
self.cart[product_id] = {'quantity':0,'price':product.off_price}
else:
self.cart[product_id] = {'quantity':0,'price':product.price}
if override_quantity:
self.cart[product_id]['quantity'] = quantity
else:
self.cart[product_id]['quantity'] += quantity
self.save()
def save(self):
self.session.modified = True
#remove from cart
def remove(self,product):
product_id = str(product.id)
if product_id in self.cart:
del self.cart[product_id]
self.save()
def __iter__(self):
product_ids = self.cart.keys()
products = Product.objects.filter(id__in=product_ids)
cart = self.cart.copy()
for product in products:
cart[str(product.id)]['product'] = product
for item in cart.values():
item['total_price'] = item['price'] * item['quantity']
yield item
def __len__(self):
return sum(item['quantity'] for item in self.cart.values())
def get_total_price(self):
return sum(item['price'] * item['quantity'] for item in self.cart.values())
def clear(self):
del self.session[settings.CART_SESSION_ID]
self.save()
ــــــــــــــــــ
from django.shortcuts import get_object_or_404 , redirect , render
from django.views.decorators.http import require_POST
from .cart import Cart
from shop.models import Product, ProductSizes
from .forms import CartAddProductForm
#require_POST
def cart_add(request , product_id , size):
cart = Cart(request)
product = Product.objects.get(id=product_id)
if size != 0:
size_ = product.size.get(id=size)
else:
size_ = None
form = CartAddProductForm(request.POST)
if form.is_valid():
cd = form.cleaned_data
cart.add(product=product , quantity=cd['quantity'] ,size_=size_, override_quantity=cd['override'] )
return redirect("home")
#require_POST
def cart_remove(request , product_id):
cart = Cart(request)
product = get_object_or_404(Product,id=product_id)
cart.remove(product=product)
return redirect("cart.detail")
def cart_detail(request):
cart = Cart(request)
for item in cart:
item['update_quantity_form'] = CartAddProductForm(initial={
'quantity':item["quantity"],
"override":True,
})
return render(request , "cart.html" , {"cart":cart})
#cart/forms.py & urls
from django import forms
PRODUCT_QUANTITY_CHOICES = [(i , str(i)) for i in range(1,21)]
class CartAddProductForm(forms.Form):
quantity = forms.TypedChoiceField(choices=PRODUCT_QUANTITY_CHOICES,coerce=int)
override = forms.BooleanField(required=False, initial=False,widget=forms.HiddenInput)
---------------
urlpatterns = [
path("" , views.cart_detail , name="cart.detail"),
path("add/<int:product_id>/<int:size>/" , views.cart_add , name="cart.add"),
path("remove/<int:product_id>/" , views.cart_remove , name="cart.remove"),
]
#html
<div class="shopping-cart-wrap">
<span class="cart-total-amunt">{{cart.get_total_price}} تومان</span><i class="icon-shopping-bag2 float-left"></i><span class="cart-total">{{cart|length}}</span>
<ul class="mini-cart">
{% for item in cart %}
<li class="cart-item">
<div class="cart-image">
<img alt="" src="/media/{{item.product.picture}}" width="80" height="80">
</div>
<div class="cart-title">
<a href="single-product.html">
<h4>{{item.product.name}}</h4>
</a>
<div class="quanti-price-wrap">
<span class="quantity">{{item.quantity}} ×</span>
<div class="price-box"><span class="new-price">{{item.price}} تومان</span></div>
</div>
<a class="{% url 'cart.remove' item.product.id %}" href="#"><i class="icon-x"></i></a>
</div>
</li>
{% endfor %}
<li class="subtotal-box">
<div class="subtotal-title">
<h3>جمع کل :</h3><span>{{cart.get_total_price}} تومان</span>
</div>
</li>
<li class="mini-cart-btns">
<div class="cart-btns">
مشاهده سبد
پرداخت
</div>
</li>
</ul>
</div>
after 2 days of struggling finally I found the solution
for adding products with different sizes you have to pass the size model id to session instead of the product id
shop/models.py
class ProductSizes(models.Model):
name = models.CharField(max_length=50)
price = models.IntegerField()
off_price = models.IntegerField(blank=True , null=True)
picture = models.ImageField(upload_to="products/%Y/%m/%d" )
product = models.ForeignKey("Product" , on_delete=models.CASCADE )
class Product(models.Model):
name = models.CharField(max_length=100)
description = models.TextField(blank=True)
picture = models.ImageField(upload_to="products/%Y/%m/%d" )
picture2 = models.ImageField(upload_to="products/%Y/%m/%d" , blank=True)
picture3 = models.ImageField(upload_to="products/%Y/%m/%d" , blank=True)
picture4 = models.ImageField(upload_to="products/%Y/%m/%d" , blank=True)
price = models.IntegerField()
available = models.BooleanField(default=True)
category = models.ForeignKey(Category ,on_delete=models.CASCADE , blank=True ,null=True )
slug = models.SlugField(max_length=100 , db_index=True , unique=True)
tags = TaggableManager(blank=True)
is_amazing = models.BooleanField(default=False)
amazing_till = models.CharField(max_length=10 , null=True , blank=True)
off_price = models.IntegerField(blank=True , null=True)
brand = models.ForeignKey(Brand ,on_delete=models.CASCADE , blank=True ,null=True )
created_at = models.DateField(auto_now_add=True)
update_at = models.DateField(auto_now=True)
def get_absolute_url(self):
return reverse('get.single.product', kwargs={'slug': self.slug})
class Meta:
ordering=("name",)
index_together = (('id' , 'slug'),)
cart/cart.py
from django.conf import settings
from shop.models import Product, ProductSizes
class Cart:
def __init__(self,request):
self.session = request.session
cart = self.session.get(settings.CART_SESSION_ID)
if not cart:
cart = self.session[settings.CART_SESSION_ID] = {}
self.cart = cart
# add to cart
def add(self,product,size_,quantity=1,override_quantity=False):
size_id = str(size_.id)
if size_id not in self.cart:
if size_.price:
self.cart[size_id] = {'quantity': 0, 'price': size_.price}
elif size_.off_price:
self.cart[size_id] = {'quantity': 0, 'price': size_.off_price}
self.cart[size_id]['quantity'] += quantity
self.save()
self.save()
def save(self):
self.session.modified = True
#remove from cart
def remove(self,size_):
product_id = str(size_.id)
if product_id in self.cart:
del self.cart[product_id]
self.save()
def __iter__(self):
sizes_ids = self.cart.keys()
products = ProductSizes.objects.filter(id__in=sizes_ids)
cart = self.cart.copy()
for size in products:
cart[str(size.id)]['size'] = size
for item in cart.values():
item['total_price'] = item['price'] * item['quantity']
yield item
def __len__(self):
return sum(item['quantity'] for item in self.cart.values())
def get_total_price(self):
return sum(item['price'] * item['quantity'] for item in self.cart.values())
def clear(self):
del self.session[settings.CART_SESSION_ID]
self.save()
cart/views.py
#require_POST
def cart_add(request , product_id , size):
cart = Cart(request)
product = Product.objects.get(id=product_id)
size_ = get_object_or_404(ProductSizes , id=size)
form = CartAddProductForm(request.POST)
if form.is_valid():
cd = form.cleaned_data
cart.add(product=product , quantity=cd['quantity'] ,size_=size_, override_quantity=cd['override'] )
return redirect("home")
The template
{% block cart %}
<div class="shopping-cart-wrap">
<span class="cart-total-amunt">{{cart.get_total_price}} تومان</span><i class="icon-shopping-bag2 float-left"></i><span class="cart-total">{{cart|length}}</span>
<ul class="mini-cart">
{% for item in cart %}
<li class="cart-item">
<div class="cart-image">
<img alt="" src="/media/{{item.size.product.picture}}" width="80" height="80">
</div>
<div class="cart-title">
<a href="single-product.html">
<h4>{{item.size.name}}</h4>
</a>
<div class="quanti-price-wrap">
<span class="quantity">{{item.quantity}} ×</span>
<div class="price-box"><span class="new-price">{{item.price}} تومان</span></div>
</div>
<a class="{% url 'cart.remove' item.size.id %}" href="#"><i class="icon-x"></i></a>
</div>
</li>
{% endfor %}
<li class="subtotal-box">
<div class="subtotal-title">
<h3>جمع کل :</h3><span>{{cart.get_total_price}} تومان</span>
</div>
</li>
<li class="mini-cart-btns">
<div class="cart-btns">
مشاهده سبد
پرداخت
</div>
</li>
</ul>
</div>
{% endblock cart %}

How to display your queries using filter?

So I am trying to set up a filter for my website and it is not working; It does not give me an error and the url updates like a query is being made but when I click "submit" it still shows all of the content in the page. I can't quite figure out what is wrong.
filters.py
import django_filters
from django_filters import DateFilter
from .models import *
class UserpostFilter(django_filters.FilterSet):
start_date = DateFilter(field_name = "date_published", lookup_expr='gte')
end_date = DateFilter(field_name = "date_published", lookup_expr='lte')
class Meta:
model = Userpost
fields = '__all__'
exclude = ['image', 'user', 'date_published']
models.py
class Userpost(models.Model):
user = models.ForeignKey(User, on_delete=models.CASCADE, null=True, blank=True)
Year = models.CharField(max_length = 4)
Mileage = models.CharField(max_length = 8)
Make = models.CharField(max_length = 50)
Model = models.CharField(max_length = 50)
Price = models.DecimalField(max_digits=15, decimal_places=2)
email = models.EmailField()
date_published = models.DateField(default = timezone.now)
image = models.ImageField(null = True, blank = True, upload_to = r"C:\Users\gabri\Desktop\test\ecommerce\static\images")
def __str__(self):
return self.Year + " " + self.Make + " " + self.Model
#property
def imageURL(self):
try:
url = self.image.url
except:
url = ''
return url
views.py
def posts(request):
cars = Userpost.objects.all()
p = Paginator(Userpost.objects.all(), 9)
page = request.GET.get('page')
cars_list = p.get_page(page)
nums = "a" * cars_list.paginator.num_pages
myFilter = UserpostFilter(request.GET, queryset = cars)
cars = myFilter.qs
context = {'cars':cars, 'cars_list':cars_list, "nums":nums, "myFilter":myFilter}
return render(request, 'store/userposts.html', context)
userposts.html
<div class = "row">
<div class="col">
<div class = "card card-body">
<form method="get">
{{myFilter.form}}
<button class="btn btn-primary" type="submit">Search</button>
</form>
</div>
</div>
</div>
<br>
<div class="row">
{% for car in cars_list %}
<div class="col-lg-4">
<img class="thumbnail" src="{{car.imageURL|default:'/images/transparentLogo.png'}}">
<div class="box-element product">
<h6><strong>{{car.Year}} {{car.Make}} {{car.Model}}</strong></h6>
<hr>
<a class="btn btn-outline-success" href="{% url 'post_detail' car.pk %}">View</a>
<h4 style="display: inline-block; float: right"><strong>${{car.Price|floatformat:2}}</strong></h4>
</div>
</div>
{% endfor %}
</div>
I would really appreciate if you guys could help me
EDIT
So I changed the order of the views to first filter and then paginate but it still does the same thing. I get no error, but display all the content from the page rather than the filtered ones.
def posts(request):
cars = Userpost.objects.all()
myFilter = UserpostFilter(request.GET, queryset=cars)
cars = myFilter.qs
p = Paginator(Userpost.objects.all(), 9)
page = request.GET.get('page')
cars_list = p.get_page(page)
nums = "a" * cars_list.paginator.num_pages
context = {'cars':cars, "myFilter":myFilter, 'cars_list':cars_list, "nums":nums}
return render(request, 'store/userposts.html', context)
The paginator should work on the cars not on the orignal queryset, since you are paginate over the filtered results
def posts(request):
cars = Userpost.objects.all()
myFilter = UserpostFilter(request.GET, queryset=cars)
if not myFilter.is_valid():
raise ValidationError('filter is invalid')
cars = myFilter.qs
# This is the change
p = Paginator(cars, 9)
page = request.GET.get('page')
cars_list = p.get_page(page)
nums = "a" * cars_list.paginator.num_pages
context = {'cars':cars, "myFilter":myFilter, 'cars_list':cars_list, "nums":nums}
return render(request, 'store/userposts.html', context)

Error message: Employee matching query does not exist

I have model Employee and same table in my local database. I need to have the possibility to edit any record and save it locally. When I have something in the webflow_id field I got this error when I tried to select the edit option: Employee matching query does not exist.
When I tried to edit record without this webflow_id it doesn't change, but creates a new record.
my views.py:
def staff_edit(request, webflow_id):
#employees = Employee.objects.all()
#print(employees)
if request.method == 'GET':
if webflow_id == 0:
form = EmployeeEditForm()
else:
try:
#employees = Employee.objects.get(pk=webflow_id)
employees = Employee.objects.get(pk=webflow_id)
except Employee.DoesNotExist:
raise Http404("Employee DoesNotExist")
form = EmployeeEditForm(instance=employees)
return render(request, 'staffedit.html', {'form': form})
else:
if webflow_id == 0:
form = EmployeeEditForm(request.POST)
else:
employees = Employee.objects.get(pk=webflow_id)
form = EmployeeEditForm(request.POST, instance=employees)
if form.is_valid():
form.save()
return redirect('feedback:staff')
context = {'form': form} #when the form is invalid
return render(request, 'staffedit.html', context)
models.py:
class Employee(models.Model):
webflow_id = models.CharField(max_length=100, primary_key=True, default=True)
first_name = models.CharField(max_length=100)
last_name = models.CharField(max_length=100, default=True)
email = models.EmailField(max_length=100)
user_type = models.CharField(max_length=100)
status = models.CharField(max_length=100, default=True)
roles = models.ManyToManyField('Role', through='EmployeeRole')
def __str__(self):
return self.webflow_id + " " + self.email + " " + self.first_name + " " + self.last_name
this is my general html staff.html:
$(document).ready(function() {
var data;
fetch("http://192.168.2.85:8000/fetchapi_employees/")
.then(response => response.json())
.then(json => data = json)
.then(() => {console.log(data); //this gave me Array of objects from my database
$('#datatable').DataTable( {
data: data.employees,
deferRender: true,
scrollY: false,
scrollX: false,
scrollCollapse: true,
scroller: true,
"columns": [
{ data: "webflow_id" },
{ data: "first_name" },
{ data: "last_name" },
{ data: "email" },
{ render: function ( data, type, row ) {
return '<i class="far fa-edit fa-lg" aria-hidden="true"></i>';
} },
{ render: function ( data, type, row ) {
return '<i class="fa fa-plus-circle" aria-hidden="true"></i>';
} },
],
"order": [[1, 'asc']]
} )
})
} );
staffedit.html:
<div class="container">
<div class="col-md-10.offset-md-1 mt-5">
<div class="jumbotron">
<h1 class="display-4">Edit employee</h1>
<hr class="my-4">
<form action="" method="post" autocomplete="off">
{% csrf_token %}
{{form.as_p}}
<button type="submit" class="btn btn-success"><i class="far fa-save"></i> Save</button>
</form>
</div>
</div>
</div>
forms.py:
class EmployeeEditForm(ModelForm):
class Meta:
model = Employee
fields = [
'webflow_id',
'first_name',
'last_name',
'roles',
]
and this is my urls.py:
path('staff/edit/<int:webflow_id>', views.staff_edit, name="staffedit"),
Can anyone see what I'm missing?
Best regards!
webflow_id is a CharField
webflow_id = models.CharField(max_length=100, default=True)
Url parameter must be contain string:
path('staff/edit/<str:webflow_id>', views.staff_edit, name="staffedit"),
Also your webflow_id is not primary key. If you want to use it as primary key, you have to add primary_key=True.
webflow_id = models.CharField(max_length=100, primary_key=True)
I would also use Try Except for get method:
from django.http import Http404
try:
Employee.objects.get(webflow_id=webflow_id)
except Employee.DoesNotExist:
raise Http404("Employee DoesNotExist")

Send date to Template of django

in my project, user after register can complete his information in another form. after completed this form and submitted it every time he visits this page, he sees the completed form. but i have a problem in this form, calender of user's birthday field works well at the first time when field is empty, but after the form is completed and submitting when the user wants to change it the calendar looks like this:
models.py:
from django.db import models
from django.contrib.auth.models import User
class UserProfile(models.Model):
ED_CHOICES = [
('hs', 'High school'),
('bc', 'Bachelor'),
('ms', 'Master'),
('do', 'Doctor'),
]
user = models.OneToOneField(User, on_delete=models.CASCADE)
phone = models.CharField(max_length=13)
birthday = models.DateField()
education = models.CharField(max_length=20, choices=ED_CHOICES)
gender = models.CharField(max_length=15)
forms.py:
class ProfileForm(forms.ModelForm):
ED_CHOICES = [
('hs', 'High school'),
('bc', 'Bachelor'),
('ms', 'Master'),
('do', 'Doctor'),
]
phone = forms.CharField(
widget=forms.TextInput(attrs={'class': 'input--style-4'}),
label=''
)
birthday = forms.DateField(
widget=forms.DateInput(attrs={'class': 'input--style-4 js-datepicker'}),
label=''
)
education = forms.CharField(
widget=forms.Select(choices=ED_CHOICES, attrs={'class': 'select-dropdown'}),
label=''
)
gender = forms.CharField(
widget=forms.TextInput(attrs={'class': 'radio-container m-r-45'}),
label=''
)
class Meta:
model = models.UserProfile
fields = ['phone', 'birthday', 'education', 'gender']
views.py:
from django.shortcuts import render, redirect
from . import forms, models
from django.contrib.auth import get_user_model
my_user = get_user_model()
my_profile = models.UserProfile
first = True
def profile(request, slug):
global first
user = my_user.objects.get(username=slug)
if first:
pro_form = forms.ProfileForm(request.POST or None)
else:
pro = my_profile.objects.get(user=user)
pro_form = forms.ProfileForm(request.POST or None, instance=pro)
form = forms.UserProfile(request.POST or None, instance=user)
context = {'FORM': form, 'PRO': pro_form, 'USER': user}
if form.is_valid() and pro_form.is_valid():
form.save()
pro = pro_form.save(commit=False)
pro.user = user
pro.save()
first = False
if 'gender' in request.POST:
pro_form.gender = request.POST.get('gender')
return redirect('HOME')
return render(request, 'accounts/index.html', context)
template:
<div class="row row-space">
<div class="col-2">
<div class="input-group">
<label class="label">Birthday</label>
<div class="input-group-icon">
{{ PRO.birthday }}
<i class="zmdi zmdi-calendar-note input-icon js-btn-calendar"></i>
</div>
</div>
</div>
js:
(function ($) {
'use strict';
/*==================================================================
[ Daterangepicker ]*/
try {
$('.js-datepicker').daterangepicker({
"singleDatePicker": true,
"showDropdowns": true,
"autoUpdateInput": false,
locale: {
format: 'DD/MM/YYYY'
},
});
var myCalendar = $('.js-datepicker');
var isClick = 0;
$(window).on('click',function(){
isClick = 0;
});
$(myCalendar).on('apply.daterangepicker',function(ev, picker){
isClick = 0;
$(this).val(picker.startDate.format('DD/MM/YYYY'));
});
$('.js-btn-calendar').on('click',function(e){
e.stopPropagation();
if(isClick === 1) isClick = 0;
else if(isClick === 0) isClick = 1;
if (isClick === 1) {
myCalendar.focus();
}
});
$(myCalendar).on('click',function(e){
e.stopPropagation();
isClick = 1;
});
$('.daterangepicker').on('click',function(e){
e.stopPropagation();
});
} catch(er) {console.log(er);}
/*[ Select 2 Config ]
===========================================================*/
try {
var selectSimple = $('.js-select-simple');
selectSimple.each(function () {
var that = $(this);
var selectBox = that.find('select');
var selectDropdown = that.find('.select-dropdown');
selectBox.select2({
dropdownParent: selectDropdown
});
});
} catch (err) {
console.log(err);
}
})(jQuery);

Django custom button counter

I want to have 2 buttons at the bottom of my each post like that:
I need suggestions how could I achieve that. I tried django-vote library, but it does not work for me.
My code:
<--template.html-->
<div class="post-bottom overflow">
<ul class="nav navbar-nav post-nav">
<li><i class="fa fa-tag"></i>0 Creative</li>
<li><i class="fa fa-heart"></i>32 Love</li>
</ul>
</div>
_
###**VIEWS.PY**###
class IndexView(generic.ListView):
template_name = 'web_serv/index.html'
context_object_name = 'post_list'
paginate_by = 10
def get_queryset(self):
queryset = Post.objects.all()
if self.request.GET.get('category'):
queryset = queryset.filter(category=self.request.GET.get('category', ''))
return queryset
def get_context_data(self, **kwargs):
context = super(IndexView, self).get_context_data(**kwargs)
context['category_list'] = Category.objects.all()
return context
_
#models.py#
class Post(VoteModel, models.Model):
title = models.CharField(max_length=200)
author = models.CharField(max_length=40)
category = models.ForeignKey(Category)
picture = ImageWithThumbsField(sizes=((850, 400), (66, 66)))
content = models.TextField()
created_date = models.DateTimeField(default=timezone.now)
class Meta:
ordering = ['-created_date']
def __str__(self):
return self.title + ' - ' + str(self.created_date.date())
Any suggestions are welcome :)
What you are asking is a bit of work, so here's the rough outline. You can duplicate PostLove if you want to add a PostCreative as well.
models.py
class PostLove(models.Model):
user = models.ManyToManyField(settings.AUTH_USER_MODEL, blank=True, related_name='postlovesAsUser')
post = models.ForeignKey(Post, on_delete=models.CASCADE, related_name='postlovesAsPost')
total_loves = models.IntegerField(default=0)
def __str__(self):
return '{0}'.format(self.post.title)
views.py
class PostLoveView(View):
def post(self, request):
data = {}
user = request.user
post_id = int(request.POST['post_id'])
post = get_object_or_404(Post, id=post_id)
loved, created = PostLove.objects.get_or_create(post=post)
user_loved = get_object_or_none(PostLove, user=user, post=post)
if user_loved:
loved.user.remove(user)
PostLove.objects.filter(post=post).update(total_loves=F('total_loves')-1)
data['success'] = 'unloved'
else:
loved.user.add(user)
PostLove.objects.filter(user=user, post=post).update(total_loves=F('total_loves')+1)
data['success'] = 'loved'
return JsonResponse(data)
urls.py
url(r'^post-love/$', views.PostLoveView.as_view(), name='post_love'),
html
<div class="post-bottom overflow">
<ul class="nav navbar-nav post-nav">
<li><i class="fa fa-tag"></i>0 Creative</li>
<li><a id="love-button" data-post-id="{{ post.id }}" href="#"><i class="fa fa-heart"></i><span id="love-count">32</span> Love</a></li>
</ul>
</div>
javascript
$("#love-button").click(function(e) {
e.preventDefault();
var $this = $(this),
postID = $this.data('post-id'),
$totalLoves = $('#love-count'),
total = parseInt($totalLoves.html()),;
$.ajax({
type: "POST",
url: "{{ url 'post_love' }}",
data: {"post_id": postID}
})
.done(function(response) {
alert("Success")
var is_loved = JSON.parse(response)['success'];
if (is_loved == "loved") {
$totalLoves.html(total + 1);
} else if (is_loved = "unloved") {
$totalLoves.html(total - 1);
}
})
.fail(function() {
alert("Failure")
})
})
You can then migrate your DB to create the new models and you should be done.