I am trying to add and subtract 1 from the value of a Django model's IntegerField and display it on the webpage, depending on when a button is clicked.
models.py
class User(AbstractUser):
following = models.ManyToManyField("self", related_name="followers", symmetrical=False)
following_num = models.IntegerField(default=0)
In views.py:
To add: following_num = page_visitor.following_num +1
To subtract: following_num = page_visitor.following_num -1
This is displaying in the html where the number should be displaying:
<django.db.models.query_utils.DeferredAttribute object at 0x0000022868D735E0>
entire view:
def username(request, user):
#get user
user = get_object_or_404(User.objects, username=user)
posts = Post.objects.filter(user=user).order_by('-date_and_time')
page_visitor = get_object_or_404(User.objects, username=request.user)
if user == request.user:
followButton = False
else:
followButton = True
if request.method == "POST":
if "follow" in request.POST:
request.user.following.add(user)
following_num = page_visitor.following_num +1
#following_num = F('User.following_num') + 1
elif "unfollow" in request.POST:
request.user.following.remove(user)
following_num = page_visitor.following_num -1
#following_num = F('User.following_num') - 1
followers_num = page_visitor.following_num
following_num = User.following_num
return render(request, "network/username.html",{
"user": user,
"posts": posts,
"followButton": followButton,
"followers": followers_num,
"following": following_num
})
html
{% block body %}
<h1>{{ user }}</h1>
<h4>Followers:{{ followers }}</h4>
<h4>Following:{{ following }}</h4>
<!--follow/unfollow button-->
{% if followButton == True %}
<form action = "{% url 'username' user %}" method = "POST">
{% csrf_token %}
{% if user not in request.user.following.all %}
<input type="submit" value="Follow" name="follow">
{% else %}
<input type="submit" value="Unfollow" name="unfollow">
{% endif %}
</form>
{% endif %}
<!--displays all the posts-->
{% for post in posts %}
<div class = "individual_posts">
<h5 class = "post_user">{{ post.user }}</h5>
<h6 id = "post_itself">{{ post.post }}</h6>
<h6 class = "post_elements">{{ post.date_and_time }}</h6>
<h6 class = "post_elements">{{ post.likes }} Likes</h6>
</div>
{% endfor %}
{% endblock %}
you reference the class (model) and not the object (instance)
following_num = User.following_num
as you alredy passed your user object to the template you can also access the attributes directly
{{user.following_num }} {{request.user.following_num }}
but you better rename the passed user variable to avoid confusion / errors
Get user information in django templates
Without the html that is supposed to display the number I cant tell why it is displaying that, but if you intend to change the following count then you need to call page_visitor.save() after changing a property of page_visitor in order for it to save the new property's value in the database
Related
I have the following ModelForm, UpdateView and template but when I click on the 'Save' button and the 'Save and continue editing' button, nothing happens. I tried following the fix in this post but it didn't work. How do I post the data in these forms into their respective tables?
forms.py:
class ProductForm(SEOFormMixin, forms.ModelForm):
FIELD_FACTORIES = {
"text": _attr_text_field,
"richtext": _attr_textarea_field,
"integer": _attr_integer_field,
"boolean": _attr_boolean_field,
"float": _attr_float_field,
"date": _attr_date_field,
"datetime": _attr_datetime_field,
"option": _attr_option_field,
"multi_option": _attr_multi_option_field,
"entity": _attr_entity_field,
"numeric": _attr_numeric_field,
"file": _attr_file_field,
"image": _attr_image_field,
}
class Meta:
model = Product
fields = [
'title', 'upc', 'description', 'is_public', 'is_discountable', 'structure', 'slug', 'meta_title',
'meta_description']
widgets = {
'structure': forms.HiddenInput(),
'meta_description': forms.Textarea(attrs={'class': 'no-widget-init'})
}
def __init__(self, product_class, metal, data=None, parent=None, *args, **kwargs):
self.set_initial(product_class, metal, parent, kwargs)
super().__init__(data, *args, **kwargs)
if parent:
self.instance.parent = parent
# We need to set the correct product structures explicitly to pass
# attribute validation and child product validation. Note that
# those changes are not persisted.
self.instance.structure = Product.CHILD
self.instance.parent.structure = Product.PARENT
self.delete_non_child_fields()
else:
# Only set product class for non-child products
self.instance.product_class = product_class
self.instance.metal = metal
self.add_attribute_fields(product_class, metal, self.instance.is_parent)
if 'slug' in self.fields:
self.fields['slug'].required = False
self.fields['slug'].help_text = _('Leave blank to generate from product title')
if 'title' in self.fields:
self.fields['title'].widget = forms.TextInput(
attrs={'autocomplete': 'off'})
def set_initial(self, product_class, metal, parent, kwargs):
"""
Set initial data for the form. Sets the correct product structure
and fetches initial values for the dynamically constructed attribute
fields.
"""
if 'initial' not in kwargs:
kwargs['initial'] = {}
self.set_initial_attribute_values(product_class, metal, kwargs)
if parent:
kwargs['initial']['structure'] = Product.CHILD
def set_initial_attribute_values(self, product_class, metal, kwargs):
"""
Update the kwargs['initial'] value to have the initial values based on
the product instance's attributes
"""
instance = kwargs.get('instance')
if instance is None:
return
for attribute in product_class.attributes.all():
try:
value = instance.attribute_values.get(
attribute=attribute).value
except exceptions.ObjectDoesNotExist:
pass
else:
kwargs['initial']['attr_%s' % attribute.code] = value
for attribute in metal.product_attribute.all():
try:
value = instance.attribute_values.get(
attribute=attribute).value
except exceptions.ObjectDoesNotExist:
pass
else:
kwargs['initial']['attr_%s' % attribute.code] = value
def add_attribute_fields(self, product_class, metal, is_parent=False):
"""
For each attribute specified by the product class, this method
dynamically adds form fields to the product form.
"""
for attribute in product_class.attributes.all():
field = self.get_attribute_field(attribute)
if field:
self.fields['attr_%s' % attribute.code] = field
# Attributes are not required for a parent product
if is_parent:
self.fields['attr_%s' % attribute.code].required = False
for attribute in metal.product_attribute.all():
field = self.get_attribute_field(attribute)
if field:
self.fields['attr_%s' % attribute.code] = field
# Attributes are not required for a parent product
if is_parent:
self.fields['attr_%s' % attribute.code].required = False
def get_attribute_field(self, attribute):
"""
Gets the correct form field for a given attribute type.
"""
return self.FIELD_FACTORIES[attribute.type](attribute)
def delete_non_child_fields(self):
"""
Deletes any fields not needed for child products. Override this if
you want to e.g. keep the description field.
"""
for field_name in ['description', 'is_discountable']:
if field_name in self.fields:
del self.fields[field_name]
def _post_clean(self):
"""
Set attributes before ModelForm calls the product's clean method
(which it does in _post_clean), which in turn validates attributes.
"""
for attribute in self.instance.attr.get_all_attributes():
field_name = 'attr_%s' % attribute.code
# An empty text field won't show up in cleaned_data.
if field_name in self.cleaned_data:
value = self.cleaned_data[field_name]
setattr(self.instance.attr, attribute.code, value)
super()._post_clean()
views.py:
class ProductCreateUpdateView(PartnerProductFilterMixin, UpdateView):
template_name = 'oscar/dashboard/catalogue/product_update.html'
model = Product
context_object_name = 'product'
form_class = ProductForm
category_formset = ProductCategoryFormSet
image_formset = ProductImageFormSet
recommendations_formset = ProductRecommendationFormSet
stockrecord_formset = StockRecordFormSet
creating = False
parent = None
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.formsets = {'category_formset': self.category_formset,
'image_formset': self.image_formset,
'recommended_formset': self.recommendations_formset,
'stockrecord_formset': self.stockrecord_formset}
def dispatch(self, request, *args, **kwargs):
resp = super().dispatch(
request, *args, **kwargs)
return self.check_objects_or_redirect() or resp
def check_objects_or_redirect(self):
"""
Allows checking the objects fetched by get_object and redirect
if they don't satisfy our needs.
Is used to redirect when create a new variant and the specified
parent product can't actually be turned into a parent product.
"""
if self.creating and self.parent is not None:
is_valid, reason = self.parent.can_be_parent(give_reason=True)
if not is_valid:
messages.error(self.request, reason)
return redirect('dashboard:catalogue-product-list')
def get_queryset(self):
"""
Filter products that the user doesn't have permission to update
"""
return self.filter_queryset(Product.objects.all())
def get_object(self, queryset=None):
"""
This parts allows generic.UpdateView to handle creating products as
well. The only distinction between an UpdateView and a CreateView
is that self.object is None. We emulate this behavior.
This method is also responsible for setting self.product_class and
self.parent.
"""
self.creating = 'pk' not in self.kwargs
if self.creating:
# Specifying a parent product is only done when creating a child
# product.
parent_pk = self.kwargs.get('parent_pk')
if parent_pk is None:
self.parent = None
# A product class needs to be specified when creating a
# standalone product.
product_class_slug = self.kwargs.get('product_class_slug')
metal_slug = self.kwargs.get('metal_slug')
self.product_class = get_object_or_404(
ProductClass, slug=product_class_slug)
self.metal = get_object_or_404(
Metal, slug=metal_slug)
else:
self.parent = get_object_or_404(Product, pk=parent_pk)
self.product_class = self.parent.product_class
self.metal = self.parent.metal
return None # success
else:
product = super().get_object(queryset)
self.product_class = product.get_product_class()
self.parent = product.parent
self.metal = product.get_metal_type()
return product
def get_context_data(self, **kwargs):
ctx = super().get_context_data(**kwargs)
ctx['product_class'] = self.product_class
ctx['metal'] = self.metal
ctx['parent'] = self.parent
ctx['title'] = self.get_page_title()
for ctx_name, formset_class in self.formsets.items():
if ctx_name not in ctx:
ctx[ctx_name] = formset_class(self.product_class,
self.request.user,
instance=self.object)
return ctx
def get_page_title(self):
if self.creating:
if self.parent is None:
return _('Create new %(product_class)s product') % {
'product_class': self.product_class.name}
else:
return _('Create new variant of %(parent_product)s') % {
'parent_product': self.parent.title}
else:
if self.object.title or not self.parent:
return self.object.title
else:
return _('Editing variant of %(parent_product)s') % {
'parent_product': self.parent.title}
def get_form_kwargs(self):
kwargs = super().get_form_kwargs()
kwargs['product_class'] = self.product_class
kwargs['parent'] = self.parent
kwargs['metal'] = self.metal
return kwargs
def process_all_forms(self, form):
"""
Short-circuits the regular logic to have one place to have our
logic to check all forms
"""
# Need to create the product here because the inline forms need it
# can't use commit=False because ProductForm does not support it
if self.creating and form.is_valid():
self.object = form.save()
formsets = {}
for ctx_name, formset_class in self.formsets.items():
formsets[ctx_name] = formset_class(self.product_class,
self.request.user,
self.request.POST,
self.request.FILES,
instance=self.object)
is_valid = form.is_valid() and all([formset.is_valid()
for formset in formsets.values()])
cross_form_validation_result = self.clean(form, formsets)
if is_valid and cross_form_validation_result:
return self.forms_valid(form, formsets)
else:
return self.forms_invalid(form, formsets)
# form_valid and form_invalid are called depending on the validation result
# of just the product form and redisplay the form respectively return a
# redirect to the success URL. In both cases we need to check our formsets
# as well, so both methods do the same. process_all_forms then calls
# forms_valid or forms_invalid respectively, which do the redisplay or
# redirect.
form_valid = form_invalid = process_all_forms
def clean(self, form, formsets):
"""
Perform any cross-form/formset validation. If there are errors, attach
errors to a form or a form field so that they are displayed to the user
and return False. If everything is valid, return True. This method will
be called regardless of whether the individual forms are valid.
"""
return True
def forms_valid(self, form, formsets):
"""
Save all changes and display a success url.
When creating the first child product, this method also sets the new
parent's structure accordingly.
"""
if self.creating:
self.handle_adding_child(self.parent)
else:
# a just created product was already saved in process_all_forms()
self.object = form.save()
# Save formsets
for formset in formsets.values():
formset.save()
for idx, image in enumerate(self.object.images.all()):
image.display_order = idx
image.save()
return HttpResponseRedirect(self.get_success_url())
def handle_adding_child(self, parent):
"""
When creating the first child product, the parent product needs
to be implicitly converted from a standalone product to a
parent product.
"""
# ProductForm eagerly sets the future parent's structure to PARENT to
# pass validation, but it's not persisted in the database. We ensure
# it's persisted by calling save()
if parent is not None:
parent.structure = Product.PARENT
parent.save()
def forms_invalid(self, form, formsets):
# delete the temporary product again
if self.creating and self.object and self.object.pk is not None:
self.object.delete()
self.object = None
messages.error(self.request,
_("Your submitted data was not valid - please "
"correct the errors below"))
ctx = self.get_context_data(form=form, **formsets)
return self.render_to_response(ctx)
def get_url_with_querystring(self, url):
url_parts = [url]
if self.request.GET.urlencode():
url_parts += [self.request.GET.urlencode()]
return "?".join(url_parts)
def get_success_url(self):
"""
Renders a success message and redirects depending on the button:
- Standard case is pressing "Save"; redirects to the product list
- When "Save and continue" is pressed, we stay on the same page
- When "Create (another) child product" is pressed, it redirects
to a new product creation page
"""
msg = render_to_string(
'oscar/dashboard/catalogue/messages/product_saved.html',
{
'product': self.object,
'creating': self.creating,
'request': self.request
})
messages.success(self.request, msg, extra_tags="safe noicon")
action = self.request.POST.get('action')
if action == 'continue':
url = reverse(
'dashboard:catalogue-product', kwargs={"pk": self.object.id})
elif action == 'create-another-child' and self.parent:
url = reverse(
'dashboard:catalogue-product-create-child',
kwargs={'parent_pk': self.parent.pk})
elif action == 'create-child':
url = reverse(
'dashboard:catalogue-product-create-child',
kwargs={'parent_pk': self.object.pk})
else:
url = reverse('dashboard:catalogue-product-list')
return self.get_url_with_querystring(url)
urls.py:
urlpatterns += [
path(
'products/create/',
self.myapps_catalogue_views.ProductCreateRedirectView.as_view(),
name='catalogue-product-create'
),
path(
'products/create/<slug:product_class_slug>/<slug:metal_slug>/<slug:gemstone_slug>',
self.myapps_catalogue_views.ProductCreateUpdateView.as_view(),
name='catalogue-product-create'
),
]
template (selected parts):
<!--templates/oscar/dashboard/catalogue/product_update.html-->
{% extends 'oscar/dashboard/layout.html' %}
{% load form_tags %}
{% load i18n %}
{% block body_class %}{{ block.super }} create-page catalogue{% endblock %}
{% block title %}
{{ title }} | {% trans "Products" %} | {{ block.super }}
{% endblock %}
{% block breadcrumbs %}
#omitted due to space constraints
{% endblock %}
{% block headertext %}{{ title }}{% endblock %}
{% block dashboard_content %}
<form action="{% if request.GET.urlencode %}?{{ request.GET.urlencode }}{% endif %}" method="post" class="form-stacked wysiwyg fixed-actions" enctype="multipart/form-data" data-behaviour="tab-nav-errors" autocomplete="off">
{% csrf_token %}
{% if parent %}
<div class="row">
<div class="col-md-12">
<div class="alert alert-info">
{% url 'dashboard:catalogue-product' pk=parent.id as parent_url %}
{% blocktrans with title=parent.title %}
You are currently editing a product variant of
{{ title }}.
{% endblocktrans %}
</div>
</div>
</div>
{% endif %}
<div class="row">
{% block tab_nav %}
#omitted due to space constraints
{% endblock tab_nav %}
<div class="col-md-9">
<div class="tab-content">
{% block tab_content %}
{% block product_details %}
<div class="tab-pane active" id="product_details">
<div class="table-header">
<h3>{% trans "Product details" %}</h3>
</div>
<div class="card card-body product-details">
{% block product_details_content %}
<span class="error-block">{{ form.non_field_errors }}</span>
{% for field in form.hidden_fields %}
{{ field }}
{% endfor %}
{% for field in form.primary_form_fields %}
{% if 'attr' not in field.id_for_label %}
{% include 'oscar/dashboard/partials/form_field.html' with field=field %}
{% endif %}
{% endfor %}
{% endblock product_details_content %}
</div>
</div>
{% endblock product_details %}
{% block product_categories %}
#omitted due to space constraints
{% endblock product_categories %}
{% block product_attributes %}
#omitted due to space constraints
{% endblock product_attributes %}
{% block product_images %}
#omitted due to space constraints
{% endblock product_images %}
{% block stockrecords %}
#omitted due to space constraints
{% endblock stockrecords %}
{% block child_products %}
#omitted due to space constraints
{% endblock child_products %}
{% block recommended_products %}
#omitted due to space constraints
{% endblock recommended_products %}
{% block seo %}
#omitted due to space constraints
{% endblock seo %}
{% block metal_attributes %}
<div class="tab-pane" id="metal_attributes">
{% block metal_attributes_content %}
<table class="table table-striped table-bordered">
<caption>
{% trans "Attributes" %}
<span class="badge badge-success">
{% trans "Metal Type:" %} {{ metal }}
</span>
</caption>
{% for field in form %}
{% if 'attr' in field.id_for_label %}
<tr>
<td>
{% include "oscar/dashboard/partials/form_field.html" %}
</td>
</tr>
{% endif %}
{% endfor %}
</table>
{% endblock metal_attributes_content %}
</div>
{% endblock metal_attributes %}
{% endblock tab_content %}
</div>
</div>
</div>
{% block fixed_actions_group %}
<div class="fixed-actions-group">
<div class="form-group">
<div class="float-right">
<a href="{% url 'dashboard:catalogue-product-list' %}">
{% trans "Cancel" %}
</a>
{% trans "or" %}
{% if parent %}
<button class="btn btn-secondary" name="action" type="submit" value="create-another-child" data-loading-text="{% trans 'Saving...' %}">
{% trans "Save and add another variant" %}
</button>
{% endif %}
<button class="btn btn-secondary" name="action" type="submit" value="continue" data-loading-text="{% trans 'Saving...' %}">
{% trans "Save and continue editing" %}
</button>
<button class="btn btn-primary" name="action" type="submit" value="save" data-loading-text="{% trans 'Saving...' %}">
{% trans "Save" %}
</button>
</div>
{% if product %}
<a class="btn btn-success" href="{{ product.get_absolute_url }}">{% trans "View on site" %}</a>
{% endif %}
</div>
</div>
{% endblock fixed_actions_group %}
</form>
{% endblock dashboard_content %}
Previews of the Template:
Just a suggestion: Take a look at the urls.py file, and change the paths order. Remember that Django evaluate them in order and take the first expression that match the pattern.
Also noticed that you're using request.GET.urlencode() in form action, but that will return only the URL GET params. Try to add the correct page URL there instead.
So I got a QuerySet result from an aggregate function to display in a low-spec eBay clone of mine. But my problem is displaying certain fields in the Django template as I want, for example, I want to display the highest bidder's username and bid. When I call the whole object itself like so {{winner}}, then it displays. But when I try to access its fields like so {{winner.user_id.username}}, I get no output although the QuerySet does execute.
When I try to get a field like so (winner.user_id.username):
When I only call winner:
models.py
from django.contrib.auth.models import AbstractUser
from django.db import models
CATEGORIES = [
('Appliances', 'Appliances'),
('Tech', 'Tech'),
('Gaming', 'Gaming'),
('Fashion', 'Fashion'),
('Sports and Fitness','Sports and Fitness'),
('Other','Other'),
("Hygiene and Medicine","Hygiene and Medicine"),
("Stationery","Stationery"),
('Decor', 'Decor'),
('Furniture','Furniture'),
('Cars and Mechanical Things','Cars and Mechanical Things'),
("Tools","Tools")
]
# Create models here
class User(AbstractUser):
pass
class Auction_Listing(models.Model):
user_id = models.IntegerField(default=1)
list_title = models.CharField(max_length=64)
desc = models.TextField(max_length=600)
img_url = models.URLField(max_length=200, null=True, blank=True)
start_bid = models.IntegerField()
category = models.CharField(choices=CATEGORIES, max_length=35, null=True, blank=True)
active = models.BooleanField(default=True)
def __str__(self):
return f"ID:{self.id}, {self.list_title}: {self.desc}, {self.start_bid} posted by user:{self.user_id} in Category:{self.category}, url:{self.img_url}"
class Bids(models.Model):
user_id = models.ForeignKey('User', on_delete=models.CASCADE)
auctions = models.ForeignKey('Auction_Listing', on_delete=models.CASCADE, default=1,related_name='bidauc')
bid = models.IntegerField()
def __str__(self):
return f"ID:{self.id}, Bid {self.bid} posted by user:{self.user_id} on auction {self.auctions}"
class Auction_Comments(models.Model):
user_id = models.ForeignKey('User', on_delete=models.CASCADE)
comment = models.TextField(max_length=324, default='N/A')
auctions = models.ForeignKey('Auction_Listing', on_delete=models.CASCADE, default=1,related_name='comauc')
def __str__(self):
return f"ID:{self.id}, Comment: {self.comment} posted by user:{self.user_id} on auction {self.auctions}"
class Watchlist(models.Model):
user_id = models.ForeignKey('User', on_delete=models.CASCADE)
auctions = models.ForeignKey('Auction_Listing', on_delete=models.CASCADE, default=1, related_name='watchauc')
def __str__(self):
return f"ID:{self.id}, user:{self.user_id} on auction {self.auctions}"
views.py
def render_listing(request, title):
if request.method == "POST":
form = BidForm(request.POST)
bid = int(request.POST['new_bid'])
listing = Auction_Listing.objects.get(list_title=title)
comments = Auction_Comments.objects.filter(auctions=listing)
if bid <= listing.start_bid:
error = True
else:
error = False
listing.start_bid = bid
listing.save()
new_bid = Bids(user_id=request.user, auctions=listing, bid=bid)
new_bid.save()
return render(request, 'auctions/listing.html', {
"listing": listing,
"form": form,
"comments": comments,
"error": error,
"comform": CommentForm()
})
else:
form = BidForm()
comform = CommentForm()
listing = Auction_Listing.objects.get(list_title=title)
comments = Auction_Comments.objects.filter(auctions=listing)
high_bid = Bids.objects.filter(auctions=listing).aggregate(maximum=Max("bid"))
winner = Bids.objects.filter(auctions=listing, bid=high_bid['maximum'])
print(winner)
return render(request, 'auctions/listing.html', {
"listing": listing,
"form": form,
"comments": comments,
"error": False,
"comform": comform,
"winner": winner
})
template's code:
{% extends "auctions/layout.html" %}
{% load static %}
{% block title %} Listing: {{listing.list_title}} {% endblock %}
{% block body %}
{% if listing.active %}
<h2>{{ listing.list_title }}</h2>
<div class='listing'>
{% if listing.img_url == "" or listing.img_url == None %}
<a href='#'><img src="{% static 'auctions/img404.png' %}" class='img-fluid'></a>
{% else %}
<a href='#'><img src="{{ listing.img_url }}" class="img-fluid" alt='image of {{ listing.list_title }}'></a>
{% endif %}
<p>
{{ listing.desc }}
</p>
<p>
Current Bid: ${{ listing.start_bid }}
</p>
<p>Category: {{ listing.category }}</p>
<p></p>
{% if user.is_authenticated %}
<div class="bid">
<a href='{% url "watch" listing.list_title %}' class='btn btn-primary'>Add to/Remove from Watchlist</a>
{% if listing.user_id == user.id %}
<a href='{% url "close" listing.list_title %}' class='btn btn-primary'>Close Auction</a>
{% endif %}
</div>
<div class="bid">
<h3>Bid:</h3>
<form method="POST" action='{% url "renlist" listing.list_title %}'>
{% csrf_token %}
{{form}}
<button type="submit" class='btn btn-primary'>Make New Bid</button>
{% if error %}
Please enter a bid higher than the current bid.
{% endif %}
</form>
</div>
{% else %}
<p><a href='{% url "register" %}' class='register'>Register</a> to bid on this item and gain access to other features</p>
{% endif %}
</div>
<div class="listing">
<h3>Comments:</h3>
{% if user.is_authenticated %}
<div id='comform'>
<h4>Post a Comment</h4>
<form method="POST" action="{% url 'commentadd' %}">
{% csrf_token %}
{{comform}}
<button type="submit" class="btn btn-primary">Post Comment</a>
</form>
</div>
{% endif %}
<p>
{% for comment in comments %}
<div class="comment">
<h4>{{comment.user_id.username}} posted:</h4>
<p>{{comment.comment}}</p>
</div>
{% empty %}
<h4>No comments as of yet</h4>
{% endfor %}
</p>
</div>
{% else %}
<h2>This auction has been closed</h2>
<div>
<a href='{% url "watch" listing.list_title %}' class='btn btn-primary'>Add to/Remove from Watchlist</a>
</div>
<p>{{winner.user_id.username}}</p>
{% endif %}
{% endblock %}
Thanks in advance for the help!
Kind Regards
PrimeBeat
This will give a QuerySet. You can see in your second image.
A QuerySet represents a collection of objects from your database.
winner = Bids.objects.filter(auctions=listing, bid=high_bid['maximum'])
You need to iterate over that QuerySet, get whatever data you want and show it in your template.
{% for win in winner %}
<p>{{ win.user_id.username }}</p>
{% endfor %}
I need to limit the number of posts in Django queries. I have tried to add a min and max but nothing seemed to have worked. I have added home.html into the code.
Example: I should only have the 15 most recent posts in my blog. The rest can be seen by clicking on the category button.
Home.html:
{% extends 'base.html' %}
{% block content %}
<h1>Posts</h1>
<ul>
{% for post in object_list %}
<li>{{post.title}}
<style>
a {
text-transform: capitalize;
}
</style>
- {{ post.category }} - <a href="{% url 'show_profile_page' post.author.profile.id %}">{{ post.author.first_name }}
{{ post.author.last_name }}</a> - {{ post.post_date }} <small>
{% if user.is_authenticated %}
{% if user.id == post.author.id %}
- (Edit)
(Delete)
{% elif user.id == 1 %}
- (Edit)
(Delete)
{% endif %}
{% endif %}
</small><br/>
{{ post.snippet }}</li>
{% endfor %}
</ul>
{% endblock %}
view.py:
class HomeView(ListView):
model = Post
template_name = 'home.html'
ordering = ['-id']
def get_context_data(self, *args, **kwargs):
cat_menu = Category.objects.all()
context = super(HomeView, self).get_context_data(*args,**kwargs)
context["cat_menu"] = cat_menu
return context
models.py:
class Post(models.Model):
title = models.CharField(max_length=255)
header_image = models.ImageField(null=True, blank=True, upload_to='images/')
title_tag = models.CharField(max_length=255)
author = models.ForeignKey(User, on_delete=models.CASCADE)
body = RichTextField(blank=True, null=True)
post_date = models.DateField(auto_now_add=True)
category = models.CharField(max_length=255, default='intro')
snippet = models.CharField(max_length=255)
likes = models.ManyToManyField(User, related_name='post_likes')
dislikes = models.ManyToManyField(User, related_name='post_dislikes')
I think you have another template for displaying categorised objects when you click category button. As you said
"I should only have the 15 most recent posts in my blog. The rest can
be seen by clicking on the category button."
In this case you can use a simple hack to display most recent posts from your table.
query all objects in descending order in views
all_objs = Post.objects.all().order_by('-id')
Then use {% if forloop.counter <= 15 %} to display last 15 items only. as follow.
templates
{% for post in object_list %}
{% if forloop.counter <= 15 %}
<h4>{{obj}} #or anything really that is meant to be shown on the home page.</h4>
{% endif %}
{% endfor %}
You can do something like this:
def get_context_data(self, *args, **kwargs):
context = super(HomeView, self).get_context_data(*args,**kwargs)
context["cat_menu"] = Category.objects.all()
context["most_recent_posts"] = Post.objects.filter(author=self.request.user).order_by('-post_date')[:15]
return context
This will get the 15 most recent posts authored by the current user, ordered by the date it was posted.
Then just handle displaying this in home.html for example:
<ul>
{% for p in most_recent_posts %}
<li>{{ p.title }}</li>
{% endfor %}
</ul>
Just limit your query to the latest 15 entries sorted by post_date:
cat_menu = Category.objects.latest("post_date")[:15]
https://docs.djangoproject.com/en/3.2/topics/pagination/
The best way is Django Pagintion.
{% for contact in page_obj %}
{# Each "contact" is a Contact model object. #}
{{ contact.full_name|upper }}<br>
...
{% endfor %}
<div class="pagination">
<span class="step-links">
{% if page_obj.has_previous %}
« first
previous
{% endif %}
<span class="current">
Page {{ page_obj.number }} of {{ page_obj.paginator.num_pages }}.
</span>
{% if page_obj.has_next %}
next
last »
{% endif %}
</span>
</div>
from django.core.paginator import Paginator
from django.shortcuts import render
from myapp.models import Contact
def listing(request):
contact_list = Contact.objects.all()
paginator = Paginator(contact_list, 25) # Show 25 contacts per page.
page_number = request.GET.get('page')
page_obj = paginator.get_page(page_number)
return render(request, 'list.html', {'page_obj': page_obj})
you can use Django pagination api . Manage your data through page number. Initially pass 1 and after that page number given by pagination.
paginator = Paginator(yourquerysetdata, 20)
page_num = request.data.get('page')
result = yourSerializerName(paginator.get_page(page_num) many=True).data
try:
page = paginator.page(page_num)
except:
page = paginator.page(1)
count = paginator.num_pages
resultobj = paginator.get_page(page_num)
has_prev = resultobj.has_previous()
has_next = resultobj.has_next()
page_range = resultobj.paginator.page_range.stop - 1
if has_prev:
prev_page_no = resultobj.previous_page_number()
else:
prev_page_no = 0
if has_next:
next_page_no = resultobj.next_page_number()
else:
next_page_no = None
context = dict()
context['page'] = page.number
context['page_no'] = count
It is very simple. You just have to modify the query that you are using to fetch the posts.
In the get_context_data() method, replace cat_menu = Category.objects.all() with cat_menu = Category.objects.all().order_by('-post_date')[:15]. This will limit the number of results to 15 most recent objects.
For more understanding, you can take a look at the official Django docs for Limiting QuerySets.
I'm using a SessionWizardView and I can't understand why the done()method is never called. Instead, after posting my form, in the last step, I can see a POST HTTP 200 on my server, but this does nothing.
The get_form() method works as expected.
I suspect a distraction error since I have the exact same logic for another view, and this works well.
Here is the whole code bellow.
The view
class DiscountsCreateView(PermissionRequiredCanHandleProducts,
ModelInContextMixin,
RestaurantMixin, SubSectionDiscounts,
SessionWizardView):
""" Wizard view to create a discount in 2 steps """
model = Discount # used for model context
form_list = [DiscountForm0, DiscountForm1]
template_name = "discounts/discount_add.html"
def get_form(self, step=None, data=None, files=None):
form = super().get_form(step, data, files)
if step is None:
step = self.steps.current
# step0 - name, kind, tax_rate
# => nothing special to do, always the same form
# step1 - specific fields related to the chosen kind
if step == '1':
step0_data = self.storage.get_step_data('0')
kind = step0_data['0-kind']
# combo => combo, combo_unit_price
if kind == Discount.COMBO:
form.fields['combo'].queryset = Combo.objects.restaurant(self.restaurant)
# NOTE : this is not a scalable way to show/hide fields (exponential)
form.fields['rebate_amount'].widget = forms.HiddenInput()
elif kind == Discount.REBATE:
form.fields['combo'].widget = forms.HiddenInput()
form.fields['combo_unit_price'].widget = forms.HiddenInput()
return form
def done(self, form_list, **kwargs):
data = [form.cleaned_data for form in form_list]
try:
Discount.objects.create(
name=data[0]['name'],
kind=data[0]['kind'],
tax_rate=data[0]['tax_rate'],
rebate_amount=data[1]['rebate_amount'],
combo=data[1]['combo'],
combo_unit_price=data[1]['combo_unit_price']
)
except Exception as e:
messages.add_message(self.request, messages.ERROR, MSG_DISCOUNT_ADD_KO.format(e))
else:
messages.add_message(self.request, messages.SUCCESS, MSG_DISCOUNT_ADD_OK)
return redirect(reverse('bo:discount-list'))
The forms
class DiscountForm0(forms.Form):
name = forms.CharField(
label=verbose_display(Discount, 'name'))
kind = forms.ChoiceField(
label=verbose_display(Discount, 'kind'),
choices=Discount.KIND_CHOICES)
tax_rate = forms.ModelChoiceField(
label=verbose_display(Discount, 'tax_rate'),
queryset=TaxRate.objects.all())
class DiscountForm1(forms.Form):
"""
Contains all the specific fields for all discount kinds.
The goal is to only show the fields related to the right discount kind
"""
# For REBATE kind only
rebate_amount = forms.DecimalField(
label=verbose_display(Discount, 'rebate_amount'),
validators=[MaxValueValidator(0)])
# For COMBO kind only
combo = forms.ModelChoiceField(
label=verbose_display(Discount, 'combo'),
queryset=Combo.objects.none())
combo_unit_price = forms.DecimalField(
label=verbose_display(Discount, 'combo_unit_price'),
validators=[MinValueValidator(0)])
The templates
add_discount.html
{% extends "base_dashboard.html" %}
{% load verbose_name %}
{% block dashboard_title %}
Créer une {% model_name model %} : étape {{ wizard.steps.step1 }} / {{ wizard.steps.count }}
{% endblock dashboard_title %}
{% block dashboard_content %}
<form action='' method='post' novalidate>
{% csrf_token %}
{% include 'includes/_wizard_form_horizontal.html' with wizard=wizard %}
</form>
{% endblock dashboard_content %}
_wizard_form_horizontal.html
{{ wizard.management_form }}
{% if wizard.form.forms %}
{{ wizard.form.management_form }}
{% for form in wizard.form.forms %}
{% include 'includes/_form_horizontal.html' with form=form %}
{% endfor %}
{% else %}
{% include 'includes/_form_horizontal.html' with form=wizard.form %}
{% endif %}
{% if wizard.steps.prev %}
<button class="btn btn-primary" name="wizard_goto_step" type="submit"
value="{{ wizard.steps.prev }}">
« étape précédente
</button>
{% endif %}
<input type="submit" class="btn btn-primary" value="étape suivante »"/>
The done() method is always called if the form submitted in the last step is_valid(). So if it's not, it must mean your form isn't valid.
In your case, you're hiding fields that are required by your DiscountForm1. So you're also hiding the error for these fields. You should make them optional and check in the form's clean() method if the appropriate fields are filled.
The following code is from an multi-vendor ecommerce portal. We need to display different shipping methods according to the store(or vendor) on the checkout summary page.
However even though I get correct queryset while print i.e Store 1 has Test Rest of World Shipping method and Store 2 has UPC and DHL, the rendered form shows incorrect values -
#########################################################
class ShippingCountryChoiceField(forms.ModelChoiceField):
widget = forms.RadioSelect()
def label_from_instance(self, obj):
price_html = format_price(obj.price.gross, obj.price.currency)
label = mark_safe('%s %s' % (obj.shipping_method, price_html))
return label
class ShippingMethodForm(forms.Form):
def __init__(self, country_code, *args, **kwargs):
stores = kwargs.pop('stores')
super(ShippingMethodForm, self).__init__(*args, **kwargs)
for count, store in enumerate(stores, start=1):
method_field = ShippingCountryChoiceField(
queryset=ShippingMethodCountry.objects.select_related(
'shipping_method').order_by('price').filter(shipping_method__store=store),
label=pgettext_lazy('Shipping method form field label', 'Shipping method for %s' % store),
required=True)
if country_code:
queryset = method_field.queryset
method_field.queryset = queryset.unique_for_country_code(country_code)
if self.initial.get('method') is None:
method_field.initial = method_field.queryset.first()
method_field.empty_label = None
self.fields['method_%d' % count] = method_field
print [q.queryset for q in self.fields.values()]
###################################################
#load_checkout
#validate_voucher
#validate_cart
#validate_is_shipping_required
#validate_shipping_address
#add_voucher_form
def shipping_method_view(request, checkout):
country_code = checkout.shipping_address.country.code
stores = checkout.cart.lines.values_list('variant__product__store', flat=True)
stores = Store.objects.filter(id__in=stores)
print checkout.shipping_method
shipping_method_form = ShippingMethodForm(
country_code, request.POST or None, initial={'method': checkout.shipping_method},
stores=stores)
if shipping_method_form.is_valid():
for count, store in enumerate(stores):
checkout.shipping_method[store] = shipping_method_form.cleaned_data['method_%s' % count]
return redirect('checkout:summary')
print [q.queryset for q in shipping_method_form.fields.values()]
return TemplateResponse(request, 'checkout/shipping_method.html', context={
'shipping_method_form': shipping_method_form, 'checkout': checkout})
##############################################################
{% extends "checkout/details.html" %}
{% load i18n %}
{% load gross from prices_i18n %}
{% load bootstrap_form from bootstrap3 %}
{% block forms %}
<h3>{% trans "Shipping address" context "Checkout shipping address title" %}</h3>
{% include "userprofile/snippets/address-short.html" with address=checkout.shipping_address only %}
<p>{% trans "Select other address" %}</p>
<hr>
<form method="post" novalidate>
{% csrf_token %}
{% bootstrap_form shipping_method_form show_label=True %}
<p class="text-md-right">
<button type="submit" class="btn primary">
{% trans "Continue" context "Checkout shipping method primary action" %}
</button>
</p>
</form>
{% endblock %}
I believe the problem is that you are instantiating the widget in the field definition. This could cause state to be shared between different fields. Try changing it to:
class ShippingCountryChoiceField(forms.ModelChoiceField):
widget = forms.RadioSelect
...