I'm trying to build a Wish list, I have a model for the listings, and a model for the wish list,
I have succeeded in making it work but..
I have to loop over all the listings first and match with the product id in my wish list so i can access the fields such as title and image..
is there any easier way to do this than looping over all the listings until finding the matched ones ?
views.py
def add_to_wishlist(request, product_id):
product = WishList.objects.filter(listing_id=product_id, user=request.user.username)
if product:
product.delete()
else:
product = WishList()
product.listing_id = product_id
product.user = request.user.username
product.save()
return HttpResponseRedirect(request.META['HTTP_REFERER'])
def wishlist(request):
product = WishList.objects.filter(user=request.user)
all_listings = AuctionListing.objects.all()
return render(request, "auctions/wishlist.html", {
'wishlist': product,
'all_listings': all_listings
})
models.py
class AuctionListing(models.Model):
title = models.CharField(max_length=100)
description = models.TextField()
start_bid = models.IntegerField(null=True)
image = models.CharField(max_length=1000, blank=True)
category = models.CharField(max_length=100)
seller = models.CharField(max_length=100, default="Default_Value")
class WishList(models.Model):
user = models.CharField(max_length=64)
listing_id = models.IntegerField()
wishlist.html
{% extends "auctions/layout.html" %}
{% block title %}Users Wishlist {% endblock %}
{% block body %}
<div class="col-12 mx-auto">
<h1 class="h3">My Wishlist</h1>
<div>Manage your wishlist</div>
{% if wishlist %}
{% for listing in all_listings %}
{% for product in wishlist %}
{% if listing.id == product.listing_id%}
<div class="card-mb-3">
<div class="row g-0">
<div class="col-md-4">
<img src="{{ listing.image }}" class="img-responsive" >
</div>
<div class="col-md-8">
<div class="card-body">
<h5 class="card-title">{{listing.title}}</h5>
<p class="card-text"> {{listing.description}}</p>
<p class="card-text"><small class="text-muted">{{listing.start_bid}}</small></p>
</div>
</div>
</div>
</div>
{% endif %}
{% endfor %}
{% endfor %}
{% else %}
<p class="card-text">No products have been added to your wishlist yet</p>
{% endif %}
</div>
{% endblock %}
Because you're getting all the WishList objects for the user, you don't need to just gather all AuctionListing objects and iterate over them. You can query them using the IDs on the WishLists you get back.
I'd do something like;
views.py
def wishlist(request):
wishlists = WishList.objects.filter(user=request.user)
listing_ids = wishlists.values_list('listing_id', flat=True)
listings = AuctionListing.objects.filter(pk__in=listing_ids)
return render(request, "auctions/wishlist.html", {
'wishlists': wishlists,
'listings': listings
})
wishlist.html
{% extends "auctions/layout.html" %}
{% block title %}Users Wishlist {% endblock %}
{% block body %}
<div class="col-12 mx-auto">
<h1 class="h3">My Wishlist</h1>
<div>Manage your wishlist</div>
{% if wishlists and listings %}
{% for listing in listings %}
<div class="card-mb-3">
<div class="row g-0">
<div class="col-md-4">
<img src="{{ listing.image }}" class="img-responsive" >
</div>
<div class="col-md-8">
<div class="card-body">
<h5 class="card-title">{{listing.title}}</h5>
<p class="card-text"> {{listing.description}}</p>
<p class="card-text"><small class="text-muted">{{listing.start_bid}}</small></p>
</div>
</div>
</div>
</div>
{% endfor %}
{% else %}
<p class="card-text">No products have been added to your wishlist yet</p>
{% endif %}
</div>
{% endblock %}
Related
views.py :
def gerechten(request):
template = loader.get_template('café/gerechten.html')
mydata = gerecht_info.objects.all()
mydata2 = gerechten_Categorie.objects.all()
context = {
'mygerecht': mydata,
'mycategories': mydata2
}
return HttpResponse(template.render(context, request))
models.py :
class gerechten_Categorie(models.Model):
categorie = models.CharField(max_length=200)
def __str__(self):
return self.categorie
class gerecht_info(models.Model):
categorie = models.ForeignKey(gerechten_Categorie, on_delete=models.CASCADE)
gerecht_name = models.CharField(max_length=200)
gerecht_description = models.CharField(max_length=500, blank=True, null=True)
gerecht_price = models.CharField(max_length=50)
def __str__(self):
return self.gerecht_name
def drinkdesc(self):
return self.gerecht_description
def drinkprice(self):
return self.gerecht_price
gerechten.html :
{% if mygerecht %}
{% for cat in mygerecht %}
<div class="flex">
<div class="menu-head center">
<h2>{{cat.categorie}}</h2>
</div>
<div class="menu-item">
<ul class="price">
<li>{{cat.gerecht_name}}</li>
<li>€{{cat.gerecht_price}}</li>
</ul>
{% if cat.gerecht_description %}
<p>{{cat.gerecht_description}}</p>
{% else %}
<p></p>
{% endif %}
</div>
</div>
{% endfor %}
{% else %}
<div class="menu-head center">
<h2>no items avaible</h2>
</div>
{% endif %}
Looks like this but I want all info from 'Hapjes' into 1 'Hapjes' category, not separated.
I think what you are trying to achieve can be done with regroup template tag.
{% regroup mygerecht by categorie as article_categories %}
{% for categorie in article_categories %}
<div class="flex">
<div class="menu-head center">
<h2>{{categorie.grouper}}</h2>
</div>
{% for article in categorie.list %}
<div class="menu-item">
<ul class="price">
<li>{{article.gerecht_name}}</li>
<li>€{{article.gerecht_price}}</li>
</ul>
<p>{% if article.gerecht_description %}{{article.gerecht_description}}{% endif %}</p>
</div>
{% endfor %}
</div>
{% endfor %}
Hey guys I get this error thrown up at me when I click a product on my Django project. I assume its a spelling mistake in either my view or models but I cant see anything. I've attach both an image of the error and my code. Any help would be appreciated. apologies if my post does not contain the correct information needed I'm new to coding.
Views
from django.shortcuts import render, get_object_or_404
from .models import Category,Product
def allProdCat(request, category_id=None):
c_page = None
products = None
if category_id != None:
c_page = get_object_or_404(Category, id=category_id)
products = Product.objects.filter(category=c_page, available=True)
else:
products = Product.objects.all().filter(available=True)
return render(request,'shop/category.html',{'category':c_page, 'products':products})
def prod_detail(request,category_id,product_id):
try:
product = Product.objects.get(category_id=category_id,id=product_id)
except Exception as e:
raise e
return render(request,'shop/product.html', {'product':product})
models
import uuid
from django.db import models
from django.urls import reverse
class Category(models.Model):
id = models.UUIDField(
primary_key=True,
default=uuid.uuid4,
editable=False)
name = models.CharField(max_length=250, unique=True)
description = models.TextField(blank=True)
class Meta:
ordering = ('name',)
verbose_name = 'category'
verbose_name_plural = 'categories'
def get_absolute_url(self):
return reverse('phoneshop:products_by_category', args=[self.id])
def __str__(self):
return self.name
class Product(models.Model):
id = models.UUIDField(
primary_key=True,
default=uuid.uuid4,
editable=False)
name = models.CharField(max_length=250, unique=True)
description = models.TextField(blank=True)
category = models.ForeignKey(Category, on_delete=models.CASCADE)
price = models.DecimalField(max_digits=10, decimal_places=2)
image = models.ImageField(upload_to='product', blank=True)
stock = models.IntegerField()
available = models.BooleanField(default=True)
created = models.DateTimeField(auto_now_add=True, blank=True, null=True)
updated = models.DateTimeField(auto_now_add=True, blank=True, null=True)
class Meta:
ordering = ('name',)
verbose_name = 'product'
verbose_name_plural = 'products'
def get_absolute_url(self):
return reverse('phoneshop:prod_detail', args=[self.category.id, self.id])
def __str__(self):
return self.name
category.html:
{% extends "base.html" %}
{% load static %}
{% block metadescription %}
{% if category %}
{{ category.description|truncatewords:155 }}
{% else %}
Welcome to the cushion store where you can buy comfy and awesome cushions.
{% endif %}
{% endblock %}
{% block title %}
{% if category %}
{{ category.name }} - The Fantastic Electronic ShopnDrop
{% else %}
See our wide range of products including everything from phones to tablets - The Fantastic Electronic ShopnDrop
{% endif %}
{% endblock %}
{% block content %}
<!--Breadcrumb navigation-->
{% if category %}
<div class="row my_row_class">
<div class="mx-auto">
<p>Our Product Collection | {{category.name}}</p>
</div>
</div>
{% endif %}
<div class="mx-auto">
{% if category %}
<img class="my_image" src="{{category.image.url}}" alt="{{category.name}}">
</div>
<br>
<div>
<h1 class="text-center my_title">{{category.name}}</h1>
<p class="text-justify">{{category.description}}</p>
</div>
{% else %}
<img class="my_image my_image_padding" src="{% static 'images/banner.jpg' %}" alt="Our Products Collection">
</div>
<br>
<div>
<h1 class="text-center my_title">Our Products Collection</h1>
<p class="text-justify">Find a phone can be difficult at the best of times. With horrible products and prices its a nightmare
But with our wide range of products, The Fantastic Electronic ShopnDrop, could one be the one stop shop
for you.
</p>
</div>
{% endif %}
<div class="container">
<div class="row mx-auto">
{% for product in products %}
<div class="my bottom margin col-9 col-lg-4 col-sm-12 col-md-4">
<div class="cart text-center" style="min-width: 18rem;">
<img class = "card-img-top my_image"><img src="{{product.image.url}}" alt="{{product.name}}">
<div class="card-body">
<h4>{{product.name}}</h4>
<p>€{{product.price}}</p>
</div>
</div>
</div>
{% endfor %}
</div>
<br>
</div>
{% endblock %}
product.html
{% extends "base.html" %}
{% load static %}
{% block metadescription %}
{{ product.description|truncatewords:155 }}
{% endblock %}
{% block title %}
{{ product.name }} - The Fantastic Electronic ShopnDrop
{% endblock %}
{% block content %}
<div class="row my_prod_row_class">
<div class="mx-auto">
<p>Home | {{product.category}} | {{product.name}}</p>
</div>
<div class="container">
<br>
<div class="row">
<div class="col-12 col-sm-12 col-ml-12 col-lg-6 text-center">
<div style="min-width: 18rem;">
<img src="{{product.image.url}}" alt="{{product.name}}">
</div>
</div class="col-12 col-sm-12 col-md-12 col-lg-6">
<div>
<div>
<h1 class="my_prod_title"></h1>{{product.name}}</h1>
<p>€{{product.price}}</p>
<p class="my_title">Product Description</p>
<p class="text-justify my_prod_text">{{product.description}}</p>
{% if product.stock == 0%}
<p class="text-justify my_prod_text"><b>Out of Stock</b></p>
{% else %}
Add to Cart
{% endif %}
</div>
</div>
</div>
</div>
</div>
{% endblock %}
urls
from django.urls import path
from . import views
app_name ='phoneshop'
urlpatterns = [
path('', views.allProdCat, name ='allProdCat'),
path('<uuid:category_id>/', views.allProdCat, name='products_by_category'),
path('<uuid:cateogry_id>/<uuid:product_id>/', views.prod_detail, name='prod_detail'),
]
My Html
{% for category in categories %}
<div class="row">
<h3 style="padding-left: 15px; padding-bottom: 15px">{% filter upper %}{{ category.name }}{% endfilter %}</h3>
</div>
<div class="row">
{% with products=category.product.all|is_available:True %}
{% for product in products|slice:":4" %}
<div class="product-width col-xl-3 col-lg-3 col-md-3 col-sm-6 col-12 mb-30">
<div class="product-wrapper">
<div class="product-img">
<a href="{% url 'shop:product' category.name product.id %}">
<img alt="" src="{{product.image.all.0.image.url }}">
</a>
<div class="product-action">
<a class="action-wishlist" href="#" title="Wishlist">
<i class="ion-android-favorite-outline"></i>
</a>
<a class="action-cart" href="#" title="Add To Cart">
<i class="ion-android-add"></i>
</a>
</div>
</div>
<div class="product-content text-left">
<div class="product-title">
<h4>
{{ product.name|title }}
</h4>
</div>
<div class="product-price-wrapper">
<span>{{product.price}} TL</span>
</div>
</div>
</div>
</div>
{% endfor %}
{% endwith %}
</div>
<div class="row justify-content-end">
Daha Fazla...
</div>
{% endfor %}
My Model
Each product has a many-to-many relation with categories and products also have an is_available variable.
class ProductCategories(models.Model):
name = models.CharField(max_length = 60)
image = models.ImageField(upload_to = 'ProductCategories')
publish_date = models.DateTimeField(auto_now=False, auto_now_add=True)
is_available = models.BooleanField()
class Product(models.Model):
category = models.ManyToManyField(ProductCategories, related_name="product")
name = models.CharField(max_length = 60)
price = models.DecimalField(max_digits=65, decimal_places=2)
description = models.TextField()
publish_date = models.DateTimeField(auto_now=False, auto_now_add=True)
stock_number = models.IntegerField()
is_available = models.BooleanField()
My View
categories = ProductCategories.objects.all()
return render(request, 'shop/shopping.html', {'categories' : categories})
I am listing 4 products under each category but I would like to filter products that are available.
Should I filter products within view class and pass to template filtered product object separate Queryset or should I apply all filters within the template?
If I should filter them within the template as I tried above, is there any way to filter product objects according to their availability?
Thanks,
class ProductManager(models.Manager):
def is_available(self):
return self.get_queryset().filter(is_available=True)
class Product(models.Model):
--------
objects = ProductManager()
views.py
product = Product.objects.is_available()
return render(request, 'shop/shopping.html', {'products' : product})
templates
{% for product in products %}
{{ product.name }}
{% for item in product.category.all %}
{{ item.name }}
{% endfor %}{% endfor %}
Create a folder called "templatetags" at the same level as models.py and views.py in your application folder
Create a new file with the desired name in this folder. For example : 'app_tags.py'
Create a new file named __ init __.py in this folder
Open app_tags.py and write this code sample for create custom template filter:
from ..models import ProductCategories
from django import template
register = template.Library()
#register.filter
def is_available(value, arg):
products = value.filter(is_available = arg)
return products
And use like this in your Html:
{% load app_tags %}
...
...
...
{% with products=category.product.all|is_available:True %}
...
...
Please try this solution. I hope this helps to you.
we are currently building a cryptocurrency market (university project)
Each user has different cryptocurrencies in his "Wallet"
The wallet should show the sum (+ and - transactions) for each of the different cryptocurrencies.
For the moment we are only able to render a list which doesn't group the different currencies (those who have the same name)
We tried the sum and annotate functions. By doing so we aren't able to display the currency name. We get only the currency id
Here is our models file
class Currency(models.Model):
currency_name = models.CharField(max_length=40, unique=True)
symbol = models.ImageField(blank=True)
ticker = models.CharField(max_length=10)
created_at = models.DateTimeField(auto_now_add = True, null=True)
class Transaction(models.Model):
listing_id = models.ForeignKey(Listing, on_delete=models.CASCADE, null=True, blank=True)
created_at = models.DateTimeField(auto_now_add = True)
exchange_amount = models.IntegerField(null=True)
user_debit = models.ForeignKey(User, on_delete=models.CASCADE, related_name='user_id_debit', null=True)
user_credit = models.ForeignKey(User, on_delete=models.CASCADE, related_name='user_id_credit', null=True)
currency_id = models.ForeignKey(Currency, on_delete=models.CASCADE, null=True)
And our views.py
#profile page
#login_required
def profile(request):
wallet = Transaction.objects.filter(Q(user_credit=request.user) | Q(user_debit=request.user)).order_by('-created_at')
transactions = Transaction.objects.filter(Q(user_credit=request.user) | Q(user_debit=request.user)).order_by('-created_at')[:10]
listings = Listing.objects.filter(user=request.user).order_by('-created_at')[:10]
args = {'wallets': wallet, 'transactions': transactions, 'listings': listings}
return render(request, 'profile.html', args)
<!--This is our custom head title for the different urls. The base.html is loaded after this.-->
<head>
<title>SolvayCoin - Profile</title>
</head>
{% extends 'base.html' %}
{% block content %}
{% if user.is_authenticated %}
<!-- Main jumbotron for a primary marketing message or call to action -->
<div class="jumbotron">
<div class="container">
<h1 class="display-4">{{ user.first_name }} {{ user.last_name }}'s profile</h1>
<p>Username is <pan style="font-style: italic">{{ user.username }}</pan> and joined SolvayCoin {{ user.date_joined|timesince }} ago</p>
</div>
</div>
<div class="container extra-padding">
<hr>
<div class="row">
<div class="col-md-4">
<h2>My wallet</h2>
<ul class=list-unstyled >
{% for wallet in wallets %}
<li>{{ wallet.currency_id }}: {{ wallet.exchange_amount__sum|floatformat:"2" }}</li>
{% endfor %}
</ul>
</div>
<div class="col-md-4">
<h2>My transactions</h2>
{% if transactions %}
<ul class=list-unstyled >
{% for transactions in transactions %}
<li>
{% if user == transactions.user_credit %}
(+)
{% elif user == transactions.user_debit %}
(-)
{% endif %}
{{ transactions.currency_name_fixed }}{{ transactions.exchange_amount|floatformat:"2" }}
with {% if user == transactions.user_credit %}{{ transactions.user_debit }}{% elif user == transactions.user_debit %}{{ transactions.user_credit }}{% endif%}
</li>
{% endfor %}
</ul>
{% else %}
<p>No transactions are available.</p>
{% endif %}
</div>
<div class="col-md-4">
<h2>My listings</h2>
{% if listings %}
<ul class=list-unstyled >
{% for listings in listings %}
<li>Listing <!--{{ listings.id }}--> created {{ listings.created_at|date:'d/m/Y' }} at {{ listings.created_at|date:'H:i' }}</li>
{% endfor %}
</ul>
{% else %}
<p>No listings are available.</p>
{% endif %}
</div>
</div>
{% endif %}
<hr>
</div> <!-- /container -->
{% endblock %}
A django queryset solution seems tricky; since debit/credit resolution is also required here.
If performance and doing it in DB are not a major concern; I would do it in python as follows:
wallet_transactions = Transaction.objects.filter(Q(user_credit=request.user) | Q(user_debit=request.user)).order_by('-created_at').select_related('currency_id')
wallet = get_wallet(request.user, wallet_transactions)
def get_wallet(user, wallet_transactions):
wallet = defaultdict(int)
for txn in wallet_transactions:
if user == txn.user_credit:
wallet[txn.crrency_id.currency_name] += txn.exchange_amount
elif user == txn.user_debit:
wallet[txn.crrency_id.currency_name] -= txn.exchange_amount
else:
# Error; invalid transaction
pass
return wallet
# This will return something like this: {'SBS': 300, 'BTC': 121000}
I'd also think that there will be more business logic that gets added to get_wallet which definitely makes sense to be kept in the business layer of your application (python).
In your html; you can iterate over the wallet contents and print as follows:
<ul>
{% for currency_name, amount in wallet.items %}
<li>{{currency_name}} - {{amount}}</li>
{% endfor %}
</ul>
In your specific case; change this snippet:
<ul class=list-unstyled >
{% for currency_name, amount in wallet.items %}
<li>{{ currency_name }}: {{ amount|floatformat:"2" }}</li>
{% endfor %}
</ul>
Wagtail Blog Site - Comment Model Form Issues
I'm creating a blog site, using Wagtail, and I've run in to a snag. I've added a Comment Model to my page, which I can add comments through the admin section and they display on the correct posts, but the comment form I've created is not displaying for some reason. Here's my relevant code. Any tips on where I went wrong would be greatly appreciated.
blog/models.py
class BlogPage(Page):
date = models.DateField("Post date")
intro = models.CharField(max_length=250)
body = RichTextField(blank=True)
#tag manager
tags = ClusterTaggableManager(through=BlogPageTag, blank=True)
#get feature image
def main_image(self):
gallery_item = self.gallery_images.first()
if gallery_item:
return gallery_item.image
else:
return None
search_fields = Page.search_fields + [
index.SearchField('intro'),
index.SearchField('body'),
]
content_panels = Page.content_panels + [
MultiFieldPanel([
FieldPanel('date'),
FieldPanel('tags'),
], heading="Blog information"),
FieldPanel('intro'),
FieldPanel('body'),
InlinePanel('gallery_images', label="Gallery images"),
]
def serve(self, request):
# Get current page
post = self
# Get comment form
form = CommentForm(request.POST or None)
# Check for valid form
if form.is_valid():
comment = form.save(commit=False)
comment.post = post
comment.save()
return redirect(request.path)
return render_to_response(self,
{
'post': post,
'form': form,
},
context_instance=RequestContext(request))
class Comment(models.Model):
post = models.ForeignKey(BlogPage, related_name='comments')
author = models.CharField(max_length=250)
text = models.TextField()
created_date = models.DateTimeField(default=timezone.now)
approved_comment = models.BooleanField(default=False)
def approve(self):
self.approved_comment = True
self.save()
def __unicode__(self):
return self.text
def __str__(self):
return self.text
class CommentForm(forms.ModelForm):
class Meta:
model = Comment
fields = ('author', 'text',)
blog_page.html
{% extends "base.html" %}
{% load wagtailcore_tags wagtailimages_tags %}
{% block body_class %}template-blogpage{% endblock %}
{% block content %}
<div class="section">
<div class="container">
<h1 class="title">{{ page.title }}</h1>
<p class="meta subtitle">{{ page.date }}</p>
{% with page.main_image as main_image %}
{% if main_image %}{% image main_image fill-500x300 %}{% endif %}
{% endwith %}
<p>{{ main_image.caption }}</p>
<div class="hero-body subtitle">{{ page.intro }}</div>
<div class="content">
{{ page.body|richtext }}
{% if page.tags.all.count %}
<div class="tags">
<h3>Tags</h3>
{% for tag in page.tags.all %}
<span class="tag is-primary is-medium is-link"><a style="color: white" href="{% slugurl 'tags' %}?tag={{ tag }}">{{ tag }}</a></span>
{% endfor %}
</div>
{% endif %}
<p>Return to blog archive</p>
<hr>
<br>
<form action="" method="POST">
{% csrf_token %}
<table>
{{ form.as_table }}
</table>
<input class="control button is-primary" type='submit' name='submit' value='Add Comment'>
</form>
<br>
<hr>
<div class="section">
{% if page.comments.all.count %}
<h2 class='subtitle'>Comments</h2>
<div class="comments">
{% for comment in page.comments.all %}
{% if comment.approved_comment %}
<div class="comment">
<h5 class="date">{{ comment.created_date }}</h5>
<strong><h3 class="title is-3">{{ comment.author }}</h3></strong>
<h4 class="subtitle is-5">{{ comment.text|linebreaks }}</h4>
<br>
<hr>
</div>
{% endif %}
{% empty %}
<br>
<p>No comments yet...</p>
{% endfor %}
</div>
{% endif %}
</div>
</div>
</div>
</div>
{% endblock %}
Now I'm getting an error saying:
File "/home/kenneth/development/web/sites/mysite/dynamicsalesops/blog/models.py", line 88, in serve
context_instance=RequestContext(request))
TypeError: render_to_response() got an unexpected keyword argument 'context_instance'
Your view_post function is never used. In Wagtail, rendering pages as HTML is handled by a serve method on the page model itself, not by a separate view function: http://docs.wagtail.io/en/v1.9/reference/pages/theory.html#anatomy-of-a-wagtail-request