Admin can change the web title,keywords or description. But it's not working rn. When i entry website everything is fine i don't have any error but informations didn't come from database.
This is my home/models.py
from django.db import models
class Setting(models.Model):
title = models.CharField(max_length=150)
keywords = models.CharField(max_length=255)
description = models.CharField(max_length=255)
company = models.CharField(max_length=50)
address = models.CharField(blank=True, max_length=150)
phone = models.CharField(blank=True, max_length=15)
fax = models.CharField(blank=True, max_length=15)
email = models.CharField(blank=True, max_length=50)
smptpserver = models.CharField(blank=True, max_length=30)
smptemail = models.CharField(blank=True, max_length=30)
smptpassword = models.CharField(blank=True, max_length=150)
smptport = models.CharField(blank=True, max_length=15)
icon = models.ImageField(blank=True, upload_to='images/')
facebook = models.CharField(blank=True, max_length=50)
instagram = models.CharField(blank=True, max_length=50)
twitter = models.CharField(blank=True, max_length=50)
aboutus = models.CharField(max_length=50)
contact = models.CharField(max_length=50)
contact_map = models.CharField(max_length=50)
references = models.CharField(max_length=50)
status = models.CharField(max_length=10, choices=STATUS)
create_at = models.DateTimeField(auto_now_add=True)
uptade_at = models.DateTimeField(auto_now=True)
def __str__(self):
return self.title
This is my home/views.py
from django.http import HttpResponse
from django.shortcuts import render
from home.models import Setting
def index(request):
setting=Setting.objects.all()
context={'setting':setting}
return render(request,'index.html',context)
This is my home/temp/index.html
{% block title %} {{ setting.title }} {% endblock %}
{% block keywords %} {{ setting.keywords }} {% endblock %}
{% block description %} {{ setting.description }} {% endblock %}
Your issue is that you're returning a whole queryset, not a single item
setting=Setting.objects.all()
context={'setting':setting}
If you only ever have one Setting record, you could use Setting.objects.first() instead. If you have more than one you should use Setting.objects.get(...) to retrieve a single record.
You can see this for yourself if you examine the value of setting in the view before you return it (use a breakpoint() to look at the vars in the shell, or just print it out).
As long as you use all() the template will be expecting an iterable of objects for you to loop over - that iterable will not have properties like keywords, description etc.
Related
I am trying to get a list of all categories and below each category the list of all related articles.
The problem is that I am getting the list of all articles under each category. I read documentation and few answers but nothing seems to be working and I am not sure where I am making the mistake.
models.py
class Category(models.Model):
title = models.CharField(max_length=75, default='', blank=False, null=False)
body = CharField(max_length=2000, default='', null=True, blank=True)
slug = models.SlugField(unique=True, blank=True)
publish = models.DateTimeField('publish', default=timezone.now)
class Meta:
ordering = ('-publish',)
verbose_name = 'category'
verbose_name_plural = 'categories'
get_latest_by = 'publish'
class Article(models.Model):
id = models.AutoField(primary_key=True)
author = models.ForeignKey(User, blank=True, null=True, on_delete=models.SET_NULL) #settings INSTALLED_APPS
title = models.CharField('title', max_length=200)
body = CKEditor5Field('Body', config_name='extends')
last_updated = models.DateTimeField(auto_now=True)
publish = models.DateTimeField('publish', default=timezone.now)
#tags = TagField(required=False, widget=LabelWidget)
category = models.ForeignKey(Category, on_delete = models.CASCADE, blank=True, related_name='category')
slug = models.SlugField(unique=True, blank=True)
views.py
#login_required
def hal_home(request):
top_categories = Category.objects.all()[:3]
category_related_articles = Article.objects.prefetch_related('category').all()
context = {
'category_related_articles': category_related_articles
}
return render(request, 'hal/homepage.html', context)
homepage.html
{% for category in top_categories %}
<div class="btn btn-light text-center text-justify">
<div>{{ category.title }}</div>
</div>
<br>
<p>{% for article in category_related_articles %}
{{article.title}}<br>
{% endfor %}</p>
{% empty %}
<div>No categories</div>
{% endfor %}
You should use prefetch_related with your category queryset.
And also I would suggest you to change the related_name to articles since category.articles.all() seems more readable and easier than category.category.all() .
category = models.ForeignKey(Category, on_delete = models.CASCADE, blank=True, related_name='articles')
Now you can change some code in your views and templates.
top_categories = Category.objects.prefetch_related("articles").all()[:3]
context = {'top_categories': top_categories}
Now you can get articles by category in template with:
{% for category in top_categories %}
{{category.title}}
# articles of each category
{% for article in category.articles.all %}
{{article}}
{% endfor %}
{% endfor %}
I am trying to print all the orders related to a specific customer. I used a for loop to access my orders in the html file, and i used an if statement to make sure that the order is related to the customer.
{% for order in orders %}
{% if customer.name == order %}
{{ order }}
{% endif %}
{% endfor %}
in my views I gave my html file access to these variables.
def orderss(request, pk):
Customer = customer.objects.get(id=pk)
orders = Order.objects.all()
context = {
'customer':Customer,
'orders': orders,
}
return render(request, 'Inventory_Management/orders.html', context)
to reach this page i used a button
View Orders
the url is the one below
path('orders/<str:pk>/', orderss, name="orderss")
related models
class Order(models.Model):
STATUS = (
('Pending', 'Pending'),
('Out for delivery', 'Out for delivery'),
('Delivered', 'Delivered'),
)
order_head = models.ForeignKey(order_header, blank=False, null=True, on_delete=models.SET_NULL)
items = models.ForeignKey(item, blank=False, null=True, on_delete=models.SET_NULL)
Quantity = models.CharField(max_length=100)
date_created = models.DateTimeField(auto_now_add=True, null=True)
total = models.CharField(max_length=100)
status = models.CharField(max_length=200, null=True, choices=STATUS)
def __str__(self):
return '{self.order_head.Buyer}'.format(self=self)
class customer(models.Model):
name = models.CharField(max_length=12, blank=False)
phone = models.CharField(max_length=12, blank=False)
email = models.CharField(max_length=50, blank=False)
date_created = models.DateTimeField(auto_now_add=True, null=True)
def __str__(self):
return self.name
class order_header(models.Model):
date_created = models.DateTimeField(auto_now_add=True, null=True)
User = models.CharField(max_length=100, blank=False, default="Seller")
Type = models.CharField(max_length=100, default="cash")
Buyer = models.ForeignKey(customer, blank=True, null=True, on_delete=models.SET_NULL)
Note = models.CharField(max_length=100, blank=True, default="Discount: ")
Order_Id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)
def __str__(self):
return 'Order Customer: {self.Buyer}, Order Id: {self.Order_Id}'.format(self=self)
now the problem is no orders appear when I press the button, all i get is a blank page even though the specific customer has a number of orders. Help appreciated! Please Help!
In template you're trying to compare name field that is string with object (Order). I guess you thought that name will be compared with order string representation using __str__ method.
Try this:
views.py
Customer = customer.objects.get(id=pk)
orders = order_header.objects.filter(Buyer=Customer)
context = {'orders': orders}
template
{% for order_head in orders %}
{% for order in order_head.order_set.all %}
{{ order }}
{% endfor %}
{% endfor %}
In my Django App, I have 2 models. One called Post and one called Categories. When a user clicks on a category, I want only the posts that are in that category to appear in the category detail view. For example if a user clicks on the medical category, I only want the posts in the medical category to appear.
Models:
class Category(models.Model):
title = models.CharField(max_length=200)
colorcode = models.CharField(max_length=20, blank=True, null=True)
description = models.TextField()
image = models.ImageField(blank=True, null=True)
slug = models.SlugField(unique=True)
class Post(models.Model):
author = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE)
title = models.CharField(max_length=200)
text = models.TextField()
sub_description = models.TextField(blank=True, null=True)
created_date = models.DateTimeField(default=timezone.now)
published_date = models.DateTimeField(blank=True, null=True)
category = models.ForeignKey(Category, on_delete=models.CASCADE, blank=True, null=True)
image = models.ImageField(blank=True, null=True)
live = models.BooleanField(default=False)
slug = models.SlugField(unique=True)
Views:
class CategoryDetailView(DetailView):
model = Category
def get_context_data(self, **kwargs):
context = super(CategoryDetailView, self).get_context_data(**kwargs)
context['category_posts'] = Post.objects.filter(live=True)
return context
Template:
{% for post in category_posts %}
<div class="post">
<div class="post-title">
{{ post.title }}
</div>
<div class="post-author">
{{ post.author }}
</div>
</div>
{% endfor %}
In a DetailView, you have access to the actual object being rendered (in your case the Category instance) through self.object.
So in your get_context_data method you can do:
context['category_posts'] = Post.objects.filter(live=True, category=self.object)
Note that self.object might be None so you may want to deal with that case.
I have two models that feed one view.
models.py
class Item(models.Model):
item_name = models.CharField(max_length=100)
item_type = models.ForeignKey(Item_type, on_delete=models.SET_NULL, null=True)
owned_by = models.ForeignKey(User, on_delete=models.SET_NULL, null=True, blank=True)****
added_at = models.DateTimeField('date item added')
updated_at = models.DateTimeField('last update')
def __str__(self):
return self.item_name
class Item_status(models.Model):
item = models.ForeignKey(Item, on_delete=models.SET_NULL, null=True)
borrower = models.ForeignKey(User, on_delete=models.SET_NULL, null=True, blank=True)
loaned_at = models.DateTimeField(default=None, blank=True, null=True)
due_back = models.DateTimeField(default=None, blank=True, null=True)
def __time__(self):
return self.loaned_at
def itemname(self):
return (self.item.item_name)
I have the following view
views.py
class LoanedItemsByUserListView(LoginRequiredMixin,generic.ListView):
model = Item_status
template_name ='catalog/item_status_list_borrowed_user.html'
paginate_by = 10
def get_queryset(self):
return Item_status.objects.filter(borrower=self.request.user).order_by('due_back')
def get_context_data(self, **kwargs):
context = super(LoanedItemsByUserListView, self).get_context_data(**kwargs)
context['Owned_list'] = Item.objects.filter(owned_by=self.request.user, item_type = 1)
context['Loaned_list'] = Item_status.objects.exclude(borrower=self.request.user).exclude(borrower__isnull=True)
return context
I would like to find the cross section of the 'Owned_list' and the 'Loaned_list' in a single template
Something like
<h2>Loaned Books</h2>
{% if Owned_list %}
<ul>
{% for thing in Owned_list.item_name and in Loned_list.item.item_name %}
<li>
{{thing}}
</li>
{% endfor %}
</ul
{% else %}
<p>There are no books in the library.</p>
{% endif %}
I have take a look at the django documentation here https://docs.djangoproject.com/en/1.11/topics/class-based-views/generic-display/, and around SO but not found exactly what I am looking for.
Thanks!
How might I query through a ManytoMany field to list the shows an Artist has played (along with their genre which has been completed). I am fairly new to Django with just finishing up the Tango with Django tutorial, but what I have so far is below.
models.py
class Genre(models.Model):
name = models.CharField(max_length=20, unique=True, blank=False)
def __unicode__(self):
return self.name
class Artist(models.Model):
name = models.CharField(max_length=50, unique=True, blank=False)
photo = models.ImageField(upload_to='artist_photos', blank=True)
logo = models.ImageField(upload_to='artist_logos', blank=True)
genre = models.ManyToManyField(Genre)
twitter = models.URLField(blank=True)
facebook = models.URLField(blank=True)
instagram = models.URLField(blank=True)
def __unicode__(self):
return self.name
class Venue(models.Model):
name = models.CharField(max_length=50, unique=True, blank=False)
logo = models.ImageField(upload_to='venue_logos', blank=True)
capacity = models.IntegerField(blank=False)
address = models.CharField(max_length=50, blank=True)
city = models.CharField(max_length=50, blank=True)
state = models.CharField(max_length=50, blank=True)
zip_code = models.IntegerField(max_length=50, blank=True, null=True)
website = models.URLField(blank=True)
twitter = models.URLField(blank=True)
facebook = models.URLField(blank=True)
instagram = models.URLField(blank=True)
def __unicode__(self):
return self.name
class Show(models.Model):
venue = models.ForeignKey(Venue)
date_time = models.DateTimeField(blank=False)
attendance = models.IntegerField(blank=False)
bands = models.ManyToManyField(Artist)
views.py
def artists(request):
context = RequestContext(request)
artists = Artist.objects.order_by('name')
shows = Show.objects.order_by('-date_time')
# artist_shows = Show.objects.filter(????????)
context_dic = {'artists': artists, 'shows': shows}
return render_to_response('artistdb/artists.html', context_dic, context)
artist.html
<h2>Artists</h2>
{% if artists %}
<ul>
{% for artist in artists %}
<li>{{ artist.name }}<br />
<ul>
{% for g in artist.genre.all %}
<li>{{ g.name }}</li>
{% endfor %}
</ul>
</li>
<br />
{% endfor %}
</ul>
{% else %}
There are no artist.
{% endif %}
To get the shows that an artist has played, you can do this:
artist = Artist.objects.get(name="johndt6")
artist.show_set.all() # Will return all shows related to the artist
A recommendation is to set a related_name argument on your foreign keys and many to many fields. So, under the Show model, the many to many relation to artist would read:
bands = models.ManyToManyField(Artist, related_name="shows")
Then, you could query an artist's shows as follows:
artist.shows.all() # Will return all of the artists shows
You can also use normal queries if you wish:
shows = Show.objects.filter(bands__in=artist) # Will return all of an artist's shows
However, this isn't quite as nice as using Django's built in relations.
See documentation here