get_object_or_404() got an unexpected keyword argument 'slug' - python

TypeError at /vegetables/
get_object_or_404() got an unexpected keyword argument 'slug'
views.py
from django.shortcuts import render, get_object_or_404
from .models import Category
from .models import Product
def allProductCategory(request,c_slug=None):
c_page=None
products=None
if c_slug!=None:
c_page=get_object_or_404(Category,slug=c_slug)
products=Product.objects.all().filter(category=c_page,available=True)
else:
products=Product.objects.all().filter(available=True)
return render(request, 'category.html',{'category':c_page,'products':products})
urls.py
app_name='ecommerceapp'
urlpatterns = [
path('',views.allProductCategory,name='allProductCategory'),
path('<slug:c_slug>/',views.allProductCategory,name='products_by_category'),
]
Includes 2 tables-Category, Product
models.py
class Category(models.Model):
name = models.CharField(max_length=250,unique=True)
slug = models.SlugField(max_length=250,unique=True)
desc = models.TextField(blank=True)
image = models.ImageField(upload_to='category',blank=True)
class Meta:
ordering = ('name',)
verbose_name = 'category'
verbose_name_plural = 'categories'
def __str__(self):
return '{}'.format(self.name)
def get_url(self):
return reverse('ecommerceapp:products_by_category',args=[self.slug,])
class Product(models.Model):
name = models.CharField(max_length=250, unique=True)
slug = models.SlugField(max_length=250, unique=True)
desc = models.TextField(blank=True)
price = models.DecimalField(max_digits=10, decimal_places=2)
category = models.ForeignKey(Category, on_delete=models.CASCADE)
image = models.ImageField(upload_to='product', blank=True)
stock = models.IntegerField()
available = models.BooleanField(default=True)
created = models.DateTimeField(auto_now_add=True)
updated = models.DateTimeField(auto_now=True)
class Meta:
ordering = ('name',)
verbose_name = 'product'
verbose_name_plural = 'products'
def __str__(self):
return '{}'.format(self.name)
Base.html is created. header.html, navbar.html, footer.html are included in base.html. Other than these three a block content and endblock is created in base.html.
category.html
{% extends 'base.html' %}
{% load static %}
{% block metadescription %}
{% if category %}
{{ category.description|truncatewords:155 }}
{% else %}
Welcome to ZUPER Store where you can get everything.
{% endif %}
{% endblock %}
{% block title %}
{% if category %}
{{ category.name }} - ZUPER store
{% else %}
All that you need - ZUPER store
{% endif %}
{% endblock %}
{% block content %}
{% if category %}
<div>
<div>
<p>Our products</p>
</div>
</div>
{% endif %}
<div>
{% if category %}
<img src="{{category.image.url}}" alt="{{category.name}}">
</div>
<br>
<div>
<h1>{{category.name}}</h1>
<p>{{category.description}}</p>
</div>
{% else %}
<div>
<img src="{% static 'images/Banner.png' %}" alt="Our products" width="500px;" height="100px;">
</div>
<br>
<div>
<h1>Our product collections</h1>
<p>Dummy content</p>
</div>
{% endif %}
<div>
<div>
{% for product in products %}
<div>
<div>
<img src="{{product.image.url}}" alt="{{product.name}}">
<div>
<h4>{{product.name}}</h4>
<p>{{product.price}}</p>
</div>
</div>
</div>
{% endfor %}
</div>
</div>
{% endblock %}
I'am getting this error when I click on the hyperlink vegetables and cotton dress.

Related

How to filter queryset based on boolean value, then count in template

I want to count how many jobs are open vs closed. I don't understand why this isn't working, I have added {{ap.id}} and {{open.line_num_id }} after the forloop just to see what was rendered. I would think that if they match, then it's added to the count. This is not the case, all jobs are counted regardless of the "if" statement. Totally lost as to what is happening and why. Any help would be very appreciated .
I have two models:
class Airplane(models.Model):
line_num = models.CharField(max_length=10, unique=True)
vh_num = models.CharField(max_length=4, unique=True)
vz_num = models.CharField(max_length=4, unique=True)
stall = models.CharField(max_length=30, choices=stall_picker)
status = models.CharField(max_length=30, choices=status_picker)
owner = models.ForeignKey(User, on_delete=models.CASCADE)
is_completed = models.BooleanField(default=False)
pm = models.BooleanField(default=False)
created_at = models.DateTimeField(auto_now_add=True)
updated_at = models.DateTimeField(auto_now=True)
def __str__(self):
return self.line_num
class Job(models.Model):
line_num = models.ForeignKey(
Airplane, on_delete=models.CASCADE, related_name="ap_jobs")
job_num = models.CharField(max_length=10, unique=True)
description = models.CharField(max_length=200)
status = models.CharField(max_length=30, choices=status_picker)
category = models.CharField(max_length=30, choices=categories_picker)
is_completed = models.BooleanField(default=False)
created_at = models.DateTimeField(auto_now_add=True)
updated_at = models.DateTimeField(auto_now=True)
class meta:
ordering = ["category", "status"]
def __str__(self):
return (f"{self.job_num}: {self.description} : {self.is_completed}")
My View:
def Ap_linup(request):
context = {
'ap_list': Airplane.objects.all().filter(is_completed=False),
'open_jobs': Job.objects.all().filter(is_completed=False),
'closed_jobs': Job.objects.all().filter(is_completed=True)
}
return render(request, 'airplane/airplane_list.html', context)
Template:
{% extends 'base.html' %}
{% block title %} Airplane List {% endblock %}
{% block content %}
<center>
<div class="row">
{% for ap in ap_list %}
<div class="col-md-3">
<div class="card">
<div class="card-body">
<h1>
<center>
{{ ap.line_num }}
</center>
</h1>
<h3>
<p>Stall:<strong>{{ ap.stall }}</strong> VH:<strong>{{ ap.vh_num }}</strong> VZ:<strong>{{ap.vz_num }}</strong>
<p>MGR: {{ ap.owner }}</p>
<hr class="lead border border-dark">
<h1>{{ ap.id }}</h1>
{% if ap.status == "Avalible" %}
<p>Current Status:</p>
<p class="text-success">{{ ap.status }}</p>
{% else %}
<p>Current Status:</p>
<p class="text-danger">{{ ap.status }}</p>
{% endif %}
</h3>
<!-- Open Job counts -->
<div>
{% for open in open_jobs %}
{% if open.line_num_id == ap.id %}</p>
<p>Open Jobs: {{ open_jobs.count }}</p>
{% endif %}
{% endfor %}
{% for closed in closed_jobs %}
{% if closed.line_num_id == ap.id %}</p>
<p>Closed Jobs: {{ closed_jobs.count }}</p>
{% endif %}
{% endfor %}
</div>
<div class="lead border border-dark">
{% for comment in ap.ap_comments.all %}
{% if comment_id == ap_id %}
<p><strong>{{ comment.author }}</strong>: {{ comment.text }} # {{comment.created_at }}</p>
{% endif %}
{% endfor %}
</div>
</div>
</div>
</div>
{% endfor %}
</div>
</center>
{% endblock %}
OUTPUT:
Output
I want to display open and closed job counts per Airplane. Count of Open and Close jobs just add up to total jobs on Airplane.
As Kusko mentions in the comments, you can pass that number to the context. You can do so by doing something like this:
def Ap_linup(request):
context = {
'ap_list': Airplane.objects.all().filter(is_completed=False),
'open_jobs': Job.objects.all().filter(is_completed=False),
'closed_jobs': Job.objects.all().filter(is_completed=True),
'open_jobs_count': Job.objects.filter(is_completed=False).count(),
'closed_jobs_count': Job.objects.filter(is_completed=True).count()
}
Also, you don't need .all() because filter is sufficient.
and if you wanted to clean up the code a bit more so you don't have a lot of queries, you can declare variables before the context.
def Ap_linup(request):
open_jobs = Job.objects.filter(is_completed=False)
closed_jobs = Job.objects.filter(is_completed=True)
context = {
'ap_list': Airplane.objects.all().filter(is_completed=False),
'open_jobs': open_jobs,
'closed_jobs': closed_jobs,
'open_jobs_count': open_jobs.count(),
'closed_jobs_count': closed_jobs.count()
}
return render(request, 'airplane/airplane_list.html', context)
UPDATE:
To display this information in the template you can do this outside of your loop:
For closed jobs you just call the variable you set in the context
<p>Closed Jobs: {{ closed_jobs_count }}</p>
and for open jobs you would do the same thing
<p>Open Jobs: {{ open_jobs_count }}</p>
You no longer need to add a .count to the template because you already did the work on the backend. The value stored in closed_jobs_count and open_jobs_count is an number.

Cant get data from view to django template

Problem as in title. I can not get data from view to template I get <QuerySet [{'id': 3, 'order_user_id': 13, 'ordered': False, 'total': Decimal('0.00')}]> when i use
{{ order }}. I would like to get OrderedItems and Total from Order Model. How can i do that?
views.py
def cart(request):
order = Order.objects.values()
context = {'order': order}
return render(request, 'shop/cart.html', context)
models.py:
class OrderItem(models.Model):
order_item = models.ForeignKey(Item, on_delete=CASCADE, null=True)
quantity = models.IntegerField(default=1)
class Order(models.Model):
order_user = models.OneToOneField(User, on_delete=CASCADE)
order_items = models.ManyToManyField(OrderItem)
ordered = models.BooleanField(default=False)
total = models.DecimalField(default=0.00, decimal_places=2, max_digits=11)
class Item(models.Model):
title = models.CharField(max_length=150)
price = MoneyField(
decimal_places=2,
default=0,
default_currency='USD',
max_digits=11,
)
cart.html
{% extends 'shop/base.html' %}
{% block content %}
<div class="container">
{% include 'shop/navbar.html' %}
<div>
{{order}}
{{order.total}}
</div>
</div>
{% endblock content %}
You have to use for loop to iterate over the items.
First, change this: order = Order.objects.values() to order = Order.objects.all()
{% extends 'shop/base.html' %}
{% block content %}
<div class="container">
{% include 'shop/navbar.html' %}
<div>
{% for item in order %}
<li>User: {{ item.order_user }}<li/>
<li>Items:
{% for order_item in item.order_items %}
<li>{{ order_item }}</li>
{% endfor %}
<li/>
<li>Is ordered: {{ item.ordered }}<li/>
<li>Total: {{ item.total }}<li/>
{% endfor %}
</div>
</div>
{% endblock content %}

How to retrieve object that a different object is linked to in template? Django

I have a quiz model and a view where I'm displaying a list of all the quizzes on the page. I want to show the score that they got alongside each quiz. Currently, I have a score model that links to each quiz and the user that took the quiz. I want to do something like Score.objects.filter(user=request.user, quiz=quiz).get_percentage() to show the percentage achieved for that quiz. However this doesn't work in template code of course.
def learn(request): #Quiz Screen
context = {
'quizzes':Quiz.objects.all(),
'scores':Score.objects.filter(user=request.user),
}
return render(request, 'quiz_app/learn.html', context)
{% extends "quiz_app/base.html" %}
{% block content %}
New Quiz
{% for quiz in quizzes %}
<a id="quiz-anchor" href="{% url 'quiz-detail' quiz.id %}">
<article class="quiz-thumbnail-large">
<h2>{{ quiz.title }}</h2>
<h3>{{ quiz.question_amount }} Questions</h3>
<p>Score: {{ quiz.score.get_percentage }}%</p>
</article>
</a>
{% endfor %}
{% endblock content %}
class Quiz(models.Model):
title = models.CharField(max_length=100) #or TextField
#slug = models.SlugField(max_length=200, default=1) #url
video_link = models.CharField(max_length=100, default="https://www.youtube.com/watch?v=p60rN9JEapg")
question_amount = models.IntegerField()
author = models.ForeignKey(User, default=1, on_delete=models.CASCADE, related_name='quiz_author',)
date_created = models.DateTimeField(default=timezone.now)
class Meta:
verbose_name_plural = "Quizzes"
def __str__(self):
return self.title
def get_absolute_url(self):
return reverse('quiz-detail', kwargs={'pk': self.pk})
class Score(models.Model):
quiz = models.ForeignKey(Quiz, default=1, on_delete=models.CASCADE)
user = models.ForeignKey(User, default=1, on_delete=models.CASCADE)
correct_answers = models.IntegerField()
incorrect_answers = models.IntegerField()
def __str__(self): #returns as a/b
score = str(self.correct_answers) + '/' + str(self.get_total_answers())
return score
def add_correct_answers(self, amount):
self.correct_answers = self.correct_answers + amount
self.save()
def add_incorrect_answers(self, amount):
self.incorrect_answers = self.incorrect_answers + amount
self.save()
def get_total_answers(self):
total = self.correct_answers + self.incorrect_answers
return total
def get_percentage(self):
percentage = round(self.correct_answers / self.get_total_answers() * 100)
return percentage
So as you have a foreign Key relationship between Quiz and Score, you can have multiple scores for a single quiz, therefore you should use the backward relationship from the quiz model like this:
{% extends "quiz_app/base.html" %}
{% block content %}
New Quiz
{% for quiz in quizzes %}
<a id="quiz-anchor" href="{% url 'quiz-detail' quiz.id %}">
<article class="quiz-thumbnail-large">
<h2>{{ quiz.title }}</h2>
<h3>{{ quiz.question_amount }} Questions</h3>
{% for score in quiz.score_set.all %}
<p>Score: {{ score.get_percentage }}%</p>
{% endfor %}
</article>
</a>
{% endfor %}
{% endblock content %}
Edit
This is a quick fix:
{% extends "quiz_app/base.html" %}
{% block content %}
New Quiz
{% for quiz in quizzes %}
<a id="quiz-anchor" href="{% url 'quiz-detail' quiz.id %}">
<article class="quiz-thumbnail-large">
<h2>{{ quiz.title }}</h2>
<h3>{{ quiz.question_amount }} Questions</h3>
{% for score in quiz.score_set.all %}
{% if score.user == request.user %}
<p>Score: {{ score.get_percentage }}%</p>
{% endif %}
{% endfor %}
</article>
</a>
{% endfor %}
{% endblock content %}
But it's not a good idea, because you will retrieve all the score for all the users first, and then filter them; It's better to use a template tag here.

Django post not showing on homepage, but in category list

I made a blog with Django. The blog lets the admin create posts and categories. I used bootstrap to align the posts next to each other.
This is the homepage of my blog. When the posts reach the other end of the screen. look at the Example below:
post1 post2 post3 post4 post5 post6 post7 post8 post9 post10 post11 post12 post 13 post14 post16.
post 16 wouldnot show on the homepage, but if you go to its category it will show on the category list but not on the homepage which is index.html
index.html
{% extends 'base.html' %}
{% block content %}
{% if categories %}
<div class="tab">
{% for category in categories %}
<button class="tablinks"><a href="{{ category.get_absolute_url }}">{{
category.title }}</a></button>
{% endfor %}
{% else %}
<p>There are no posts.</p>
{% endif %}
</div>
<br><br><br>
<div class="container ">
{% if posts %}
<div class="row ">
{% for post in posts %}
<div class="poosts col-md-2">
<p class="past"><a class="link" href="{{ post.get_absolute_url }}"><span
class="tda"> {{post.title}}</span><br><br><span class="postbody">
{{post.body|truncatewords:13}}</a></span></p>
</div>
{% endfor %}
{% else %}
<p class="text-center">There are no posts.</p>
</div>
{% endif %}
</div>
{% endblock %}
categories page
{% extends 'base.html' %}
{% block head_title %}Viewing category {{ category.title }}{% endblock %}
{% block title %}{{ category.title }}{% endblock %}
{% block content %}
<br><br><br>
<div class="container">
{% if posts %}
<div class="row">
{% for post in posts %}
<div class="poosts col-md-4">
<p class="past"><a class="link" href="{{ post.get_absolute_url }}"><span
class="tda"> {{post.title}}</span><br><br>{{post.body|truncatewords:13}}</a>
</p>
</div>
{% endfor %}
{% else %}
<p class="text-center">There are no posts.</p>
</div>
{% endif %}
</div>
{% endblock %}
views.py
from django.shortcuts import render, redirect, get_list_or_404
from django.http import HttpResponse
from .models import Blog, Category
from django.shortcuts import render_to_response, get_object_or_404
def index(request):
return render_to_response('account/index.html', {
'categories': Category.objects.all(),
'posts': Blog.objects.all()[:5]
})
def view_post(request, slug):
return render_to_response('account/view_post.html', {
'post': get_object_or_404(Blog, slug=slug)
})
def view_category(request, slug):
category = get_object_or_404(Category, slug=slug)
return render_to_response('account/view_category.html', {
'category': category,
'posts': Blog.objects.filter(category=category)[:5]
})
models.py
from django.db import models
from django.db.models import permalink
class Blog(models.Model):
title = models.CharField(max_length=100, unique=True)
slug = models.SlugField(max_length=100, unique=True)
body = models.TextField()
posted = models.DateTimeField(db_index=True, auto_now_add=True)
category = models.ForeignKey('Category')
def __unicode__(self):
return '%s' % self.title
#permalink
def get_absolute_url(self):
return ('view_blog_post', None, { 'slug': self.slug })
def __str__(self):
return self.title
class Category(models.Model):
title = models.CharField(max_length=100, db_index=True)
slug = models.SlugField(max_length=100, db_index=True)
def __unicode__(self):
return '%s' % self.title
#permalink
def get_absolute_url(self):
return ('view_blog_category', None, { 'slug': self.slug })
def __str__(self):
return self.title
you should try this 'posts': Blog.objects.all() in views.py

Django NoReverMatch at /

I read the book 'Django By Example' and run the examples in chaper7.
But the results call "NoReverMatch at /"
NoReverseMatch at /
Reverse for 'product_detail' with arguments '(5, u'ironman')' and keyword arguments '{}' not found. 1 pattern(s) tried: ['(?P<id>[\\d+])/(?P<slug>[-\\w+])/$']
This is the models.py
class Category(models.Model):
name = models.CharField(max_length=200, db_index=True)
slug = models.SlugField(max_length=200, db_index=True, unique=True)
class Meta:
ordering = ('name',)
verbose_name = 'category'
verbose_name_plural = 'categories'
def __str__(self):
return self.name
def get_absolute_url(self):
return reverse('shop:product_list_by_category', args=[self.slug])
class Product(models.Model):
category = models.ForeignKey(Category, related_name='products')
name = models.CharField(max_length=200, db_index=True)
slug = models.SlugField(max_length=200, db_index=True)
image = models.ImageField(upload_to='products/%Y/%m/%d', blank=True)
description = models.TextField(blank=True)
price = models.DecimalField(max_digits=10, decimal_places=2)
stock = models.PositiveIntegerField()
available = models.BooleanField(default=True)
created = models.DateTimeField(auto_now_add=True)
updated = models.DateTimeField(auto_now=True)
class Meta:
ordering = ('-created',)
index_together = (('id', 'slug'),)
def __str__(self):
return self.name
def get_absolute_url(self):
return reverse('shop:product_detail', args=[self.id, self.slug])
This is the urls.py
urlpatterns = [
url(r'^$', views.product_list, name='product_list'),
url(r'^(?P<category_slug>[-\w]+)/$', views.product_list, name='product_list_by_category'),
url(r'^(?P<id>\d+)/(?P<slug>[-\w+])/$', views.product_detail, name='product_detail'),
]
This is the views.py
def product_list(request, category_slug=None):
category = None
categories = Category.objects.all()
products = Product.objects.filter(available=True)
if category_slug:
category = get_object_or_404(Category, slug=category_slug)
products = products.filter(category=category)
return render(request, 'shop/product/list.html', {'category': category, 'categories': categories, 'products': products})
def product_detail(request, id, slug):
product = get_object_or_404(Product,
id=id,
slug=slug,
available=True)
return render(request, 'shop/product/detail.html', {'product': product})
And this is the 'list.html' template where the error occurs.
{% extends "shop/base.html" %}
{% load static %}
{% block title %}
{% if category %}{{ category.name }}{% else %}Products{% endif %}
{% endblock %}
{% block content %}
<div id="sidebar">
<h3>Categories</h3>
<ul>
<li {% if not category %} class="selected" {% endif %}>
All
</li>
{% for c in categories %}
<li {% if category.slug == c.slug %} class="selected" {% endif %}>
{{ c.name }}
</li>
{% endfor %}
</ul>
</div>
<div id="main" class="product-list">
<h1>{% if category %}{{ category.name }}{% else %}Products{% endif %} </h1>
{% for product in products %}
<div class="item">
<a href="{{ product.get_absolute_url }}">
<img src="{% if product.image %}{{ product.image.url }}{% else %}{% static 'img/no_image.png' %}{% endif %}">
</a>
{{ product.name }}<br>
${{ product.price }}
</div>
{% endfor %}
</div>
{% endblock %}
The error occurs at
<a href="{{ product.get_absolute_url }}">
-- line.
Why this error occurs??
It looks to me like you need to change your url pattern:
url(r'^(?P<id>\d+)/(?P<slug>[-\w+])/$', views.product_detail, name='product_detail')
perhaps to:
url(r'^(?P<id>\d+)/(?P<slug>[-\w]+)/$', views.product_detail, name='product_detail')
'+' does not function as a special character inside square brackets, so [-\w+] will only match a single character (alphanumeric or '-'), but you are passing multi-character strings like 'ironman'. [-\w]+ will match one or more characters.

Categories