I have a bootstrap search bar and I want it to search for a list of all tags in a database. Tags are a like a facebook page or reddit tags.
When I submit my search, I want the URL to include the GET-parameter: ?q="typeword", but the application has to redirect to a new page, and preserve the tags parameter while doing so. How can I do this?
I have tried using GET-request and paginator, but nothing seems to work. I can add a new URL "search/" and then it works, but only with this url.
models.py
class Tag(models.Model):
path = models.CharField(max_length=37,default=0)
name = models.CharField(max_length=35)
description = models.CharField(max_length=200)
image = models.ImageField(default='default.jpg',upload_to='tag_pics')
def __str__(self):
return self.path
views.py
def search(request):
q = request.GET.get('q',None)
items=''
if q is None or q is "":
items = Tag.objects.all()
elif q is not None:
items = Tag.objects.filter(name__contains=q)
paginator = Paginator(items, 3)
page = request.GET.get('page')
items = paginator.get_page(page)
title = "Search"
return render(request, 'blog/search.html',{'items': items, 'title': title})
search.html
{% extends 'blog/base.html' %}
{% block content %}
{% if items %}
<p class="search-count">Found
<strong>
{{ items.paginator.count }}
</strong>
result{{ items.paginator.count|pluralize }}
</p>
{% for i in items %}
{{ i.path }}
{% endfor %}
{% elif request.GET.q %}
<p class="search-no-results">
No results for "<strong>{{ request.GET.q }}"
</p>
{% endif %}
{% endblock %}
Related
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.
How to add parameters in a url in render method - Django?
I'm having difficulty adding pagination to a search result.
On the first page the result is shown perfectly, but from the second page onwards, the search parameter no longer exists.
thank you.
def get(self, request):
clientes = Cliente.objects.filter(
Q(nome__icontains=request.GET['nome']))
formPesquisa = FormPesquisaCliente()
paginator = Paginator(clientes, 40)
page = request.GET.get('page')
clientes = paginator.get_page(page)
response = render(request, 'cliente/index.html', {
'clientes': clientes,
'formPesquisa': formPesquisa})
response['Location'] += '?nome=' +request.GET['nome']
return response
What are you missing is that when you have filtered data from the queryset and its paginated so obviously to view the next page you need to maintain the state by passing the same filter object nome. So the url should look something like this.
http://localhost:8000/clients/?page=2&nome=something
def get(self, request):
abc = request.GET.get['nome']) #<--- variable to get value from view
clientes = Cliente.objects.filter(
Q(nome__icontains=abc)) #<-- pass the abc variable here
formPesquisa = FormPesquisaCliente()
paginator = Paginator(clientes, 40)
page = request.GET.get('page')
clientes = paginator.get_page(page)
response = render(request, 'cliente/index.html', {
'clientes': clientes,
'formPesquisa': formPesquisa,
'abc':abc}) #<-- passing the abc variable to view to maintain the state of your filter.
response['Location'] += '?nome=' +request.GET['nome']
return response
Example Pagination Code:
<div class="pagination">
<span class="step-links">
{% if clients.has_previous %}
« first
{% if nome %}
previous
{% else %}
previous
{% endif %}
{% endif %}
<span class="current">
Page {{ clientes.number }} of {{ clientes.paginator.num_pages }}.
</span>
{% if clientes.has_next %}
{% if nome %}
next
last »
{% else %}
next
last »
{% endif %}
{% endif %}
</span>
</div>
I don't want to display a link if the list returns empty.
template.html
{% for item in cart %}
<h1>{{ item.product.product_title }}</h1>
Remove item
{% empty %}
<p>No items in cart</p>
{% endfor %}
{% if item is not None %}
<p>
Checkout
</p>
{% endif %}
views.py
def cartview(request):
if request.user.is_authenticated():
cart = Cart.objects.filter(user=request.user.id, active=True)
orders = ProductOrder.objects.filter(cart=cart)
#total = 0
count = 0
for order in orders:)
count += order.quantity
context = {
'cart': orders,
'count': count,
}
return render(request, 'store/cart.html', context)
else:
return redirect('index:index')
I want to hide checkout link if the cart list is empty. putting it in the for loop would make the link appear many times. I want to display checkout button only once.
Instead of 'item' check for 'cart' in the template.
{% if cart %}
<p>
Checkout
</p>
{% endif %}
First my model contains questions and answers pages to manage issues.
My models.py
class Question(models.Model):
label = models.CharField(max_length=30)
def __str__(self):
return self.label
class Page(models.Model):
title = models.CharField(max_length=30)
def __str__(self):
return self.title
class Reply(models.Model):
page = models.ManyToManyField(Page)
question = models.ForeignKey(Question)
user = models.ForeignKey(Personne)
answer = models.CharField(max_length=30)
creationDate = models.DateTimeField(default=datetime.now())
def __str__(self):
return str(self.answer)
So I managed to retrieve the answers for each page
One page is equal to: 1 visit
another: Visit 2 etc ... (i go share a screenshot)
I managed to retrieve the answers for each pages but I fail to see the questions corresponding to each response for a page!
This is my views.py
def reply(request):
replies = Reply.objects.all()
questions = Question.objects.all()
logged_user = get_logged_user_from_request(request)
pages = Page.objects.all()
form = ReplyForm(request.GET)
personnes = Personne.objects.all()
if logged_user:
if len(request.GET) > 0:
form = ReplyForm(request.GET)
if form.is_valid():
form.save(commit=True)
return HttpResponseRedirect('/reply')
else:
return render_to_response('polls/reply.html', {'personnes': personnes, 'replies': replies, 'questions': questions,'pages':pages, 'form': form})
else:
form = ReplyForm()
return render_to_response('polls/reply.html', {'personnes':personnes, 'replies': replies, 'questions': questions, 'pages':pages, 'form': form})
else:
return HttpResponseRedirect('/login')
and for finish this is my template reply.html
{% for reply in replies %}<br>
<br>{{ reply.user }}
{{ reply.question }} -
{{ reply.answer }}
(dans la :{% for page in reply.page.all %} {{ page }}) {% endfor %}
{% endfor %}
<hr>
{% for page in pages %}<hr>
{{ page }}:
<br>{{ page.reply.question_set all }} : {{ page.reply_set.all }}
{% endfor %}
How is it possible to retrieve the issues here?
I manage to recover the questions for every answer but not for every visit
The questions will not be displayed for each response in each visit
What is the call in the template to do this?
or then add a line in views.py ?
** Edit :**
edit : add a screen shot of the result right now
template :
{% for reply in replies %}<br>
<br>{{ reply.user }}
{{ reply.question }} -
{{ reply.answer }}
(dans la :{% for page in reply.page.all %} {{ page }}) {% endfor %}
{% endfor %}
<hr>
{% for page in pages %}<hr>
{{ page }}:
<br>{% for reply in page.reply_set.all %}<br> {{ reply.question }} (Author : {{ reply.user }}) {% endfor %}
{% endfor %}
{% for reply in replies %}<br>
<br>{{ reply.user }}
{{ reply.question }} -
{{ reply.answer }}
(dans la :{% for page in reply.page.all %} {{ page }}) {% endfor %}
{% endfor %}
<hr>
{% for page in pages %}<hr>
{{ page }}:
<br>{% for each_reply in page.reply_set.all }}{{each_reply.question}}{% endfor %} : {{ page.reply_set.all }}
{% endfor %}
Ok I know a thousand people asked this but I have looked all over this site to no success(also google) here is my models.py
VENUE_IMAGE_PATH = os.path.join('images', 'venue_profiles/%Y/%m/%d')
class Venue(models.Model):
.....................
name = models.CharField(max_length=100)
address = models.CharField(max_length=100)
city = models.CharField(max_length=100)
...........................
class VenueImage(models.Model):
venue = models.ForeignKey(Venue, related_name="venue")
image = models.ImageField(upload_to=VENUE_IMAGE_PATH, max_length=255)
Here is my views.py
def list(request):
venues = Venue.objects.all()
images=VenueImage.objects.all()
return render_to_response('venues/list.html', {'venues':venues,'images':images},
context_instance = RequestContext(request))
here is my template
{% for v in venues %}
<a href='#'>{{v.name}}</a>
edit
{% if images %}
<img class='venue_image' src='images/venue_profiles/2012/10/25/{{images.url}}'
alt=''>
{% endif %}
{% endfor %}
Now I have tried {{images.images.url}} and {{images.url}}. {{MEDIA_URL}}images/venue_profiles/%Y/%m/%d/{{image.url}}.
I also tried {%for i in images %} {{i.url}} {% endfor %}.
I also tried without that 'images/venue_profiles/2012/10/25' prefix and nothing seems to work.
Can someone please help me see what I am doing wrong.
# In models.py
class Venue(models.Model):
....
name = models.CharField(max_length=100)
....
images = models.ForeignKey(VenueImage)
class VenueImage(models.Model):
image = models.ImageField(upload_to=VENUE_IMAGE_PATH)
# In views.py
def list(request):
venues = Venue.objects.all()
return render(request,
'venues/list.html',
{'venues': venues'})
# In template
{% for venue in venues %}
<a href '#'>{{ venue.name }}</a>
...
{% for image in venue.images %}
<img class='venue_image' src=''{{ image.url }}' alt=''>
{% endfor %}
{% endfor %}