Sorting through request.GET in Django - django

I want people to be able to sort things and not just me sorting things manually. So, for example, I will have a link to sorting, and the link will be something like, /?sort=issues and this would show a list of issues in alphabetical order, etc. Or /?sort=cover and it will show a list of issues with covers only.
Views.py
def issues(request):
issues_list = Issue.objects.order_by('-date_added')
paginator = Paginator(issues_list, 24)
try:
page = int(request.GET.get('page', '1'))
except ValueError:
page = 1
try:
issues = paginator.page(page)
except (EmptyPage, InvalidPage):
issues = paginator.page(paginator.num_pages)
return render_to_response('comics/issues.html', {'issues': issues}, context_instance=RequestContext(request))
So, I'd want anyone to have the option of ordering by something like -date_added, date_addded, pub_date, importance, etc.
I'd imagine I'd have to fix my views and do some request.GET magic, but I am pretty new to Django and don't really know how to go about doing this. I checked Django docs, too.

sort_by = request.GET.get('sort', '-date_added')
if sort_by not in ['-date_added', 'date_addded', 'pub_date', 'importance']:
sort_by = '-date_added'
issues_list = Issue.objects.order_by(sort_by)

Put it at the top of your view:
user_specified = request.GET.get('sort', **''**)
if user_specified:
issues_list = Issue.objects.order_by(user_specified)
else: # default ordering
issues_list = Issue.objects.order_by('-date_added')
NB: Haven't tested this!

Related

How to show users own post along with post from users he follows in Django

as part of learning django, I am creating a blog website and also a user follow model. Here in the homepage I am able to see only the posts from the people I follow and not the users own posts. I tried using chained filter/ multiple filter but it seems it doesn't work in this case. Please have a look at this view code and let me know what changes I should make here.
#login_required
def myhome(request, tag_slug=None):
current_user = request.user
following_ids = request.user.following.values_list('id',flat=True)
actions = Action.objects.filter(user_id__in=following_ids) #Action notification
posts_list = Post.objects.filter(user_id__in=following_ids).filter(user=current_user).\
order_by('-post_date') #See only followed people post
tag = None
if tag_slug:
tag = get_object_or_404(Tag, slug=tag_slug)
posts_list = posts_list.filter(tags__in=[tag])
paginator = Paginator(posts_list, 5)
page = request.GET.get('page')
try:
posts = paginator.page(page)
except PageNotAnInteger:
posts = paginator.page(1)
except EmptyPage:
posts = paginator.page(paginator.num_pages)
if following_ids:
actions = actions.filter(user_id__in=following_ids)
actions = actions.select_related('user', 'user__profile').prefetch_related('target')[:10]
context = {
'page':page,
'posts':posts,
'tag':tag,
'actions':actions,
}
return render(request, 'posts/users/myhome.html', context)
Thanks in advance!
If I understand you correctly you are trying to get all the posts of the user plus the posts of the users that the user follows. If that is the case I don't think you will be able to do it by chaining filters, because you want an "OR" condition.
So, you can achieve this in several ways, one simple way is to use | operator, like this,
posts_list = Post.objects.filter(user_id__in=following_ids) | Post.objects.filter(user=current_user)
But I think Django construction for this and other complex cases are Q objects. Something like this should work,
from django.db.models import Q
posts_list = Post.objects.filter(Q(user_id__in=following_ids) | Q(user=current_user))
Here you have a nice discussion of this subject (How do I do an OR filter in a Django query?)[How do I do an OR filter in a Django query?.

Flask wtforms AttributeError: 'HTMLString' object has no attribute 'paginate'

Somebody has helped me with some great code here to show the same form multiple times each with a submit button, it works a treat, But as I will have hundreds of forms I need to paginate the page, I have been able to paginate pages in the past but I dont no how to use that code with a form in a for loop.
here is my code:(with lots of help from Greg)
#bp.route('/stock', methods=['GET', 'POST'])
#bp.route('/stock/stock/', methods=['GET', 'POST'])
#login_required
def stock():
stocks = Stock.query.all()
forms = []
for stock in stocks:
form = AddStockForm()
form.id.default = stock.id
form.image.default = stock.image_url
form.date.default = stock.date
form.description.default = stock.description
form.event.default = stock.event
form.achat.default = stock.achat
form.vente.default = stock.vente
form.sold.default = stock.sold
forms.append(form)
for form in forms:
if form.validate_on_submit():
if form.modify.data:
stock = Stock.query.filter_by(id=form.id.data).one()
stock.date = form.date.data
stock.description = form.description.data
stock.event = form.event.data
stock.achat = form.achat.data
stock.vente = form.vente.data
stock.sold = form.sold.data
db.session.add(stock)
db.session.commit()
elif form.delete.data:
stock = Stock.query.filter_by(id=form.id.data).one()
db.session.delete(stock)
db.session.commit()
return redirect(url_for('stock.stock'))
form.process() # Do this after validate_on_submit or breaks CSRF token
page = request.args.get('page', 1, type=int)
forms = forms[1].id().paginate(
page, current_app.config['ITEMS_PER_PAGE'], False)
next_url = url_for('stock.stock', page=forms.next_num) \
if forms.has_next else None
prev_url = url_for('stock.stock', page=forms.prev_num) \
if forms.has_prev else None
return render_template('stock/stock.html',forms=forms.items, title=Stock, stocks=stocks)
I am trying to use the fact "forms" is a list to paginate the results, I obviously dont understand how to do this, I have looked at flask-paginate but I didnt understand that either!
all help is greatly needed
Warm regards, Paul.
EDIT
I have tried to use flask_pagination, here is my code:
#bp.route('/stock/stock/', methods=['GET', 'POST'])
#login_required
def stock():
search = False
q = request.args.get('q')
if q:
search = True
page = request.args.get(get_page_parameter(), type=int, default=1)
stocks = Stock.query.all()
forms = []
#rest of code here#
pagination = Pagination(page=page, total=stocks.count(), search=search, record_name='forms')
form.process() # Do this after validate_on_submit or breaks CSRF token
return render_template('stock/stock.html',forms=forms, title=Stock, pagination=pagination)
This gives a different error "TypeError: count() takes exactly one argument (0 given)" I also tried with "total=forms.count()" and got the same error!
I hate doing this as it shows a lack of patience at the begining but this answer may help others, I solved my problem in two ways the first was the query which decides the order of display (descending or ascending) this then allowed me to use flask-paginate to display the results on several pages, I realised that I was dealing with a list, and the example by one of the developers link showed me the way, here is my code,
from flask_paginate import Pagination, get_page_args
#bp.route('/stock', methods=['GET', 'POST'])
#bp.route('/stock/stock/', methods=['GET', 'POST'])
#login_required
def stock():
stocks = Stock.query.order_by(Stock.id.desc())# this gives order of results
forms = []
def get_forms(offset=0, per_page=25): #this function sets up the number of
return forms[offset: offset + per_page] #results per page
for stock in stocks:
form = AddStockForm()
form.id.default = stock.id
form.image.default = stock.image_url
form.date.default = stock.date
form.description.default = stock.description
form.event.default = stock.event
form.achat.default = stock.achat
form.vente.default = stock.vente
form.sold.default = stock.sold
forms.append(form)
for form in forms:
if form.validate_on_submit():
if form.modify.data:
stock = Stock.query.filter_by(id=form.id.data).one()
stock.date = form.date.data
stock.description = form.description.data
stock.event = form.event.data
stock.achat = form.achat.data
stock.vente = form.vente.data
stock.sold = form.sold.data
db.session.add(stock)
db.session.commit()
elif form.delete.data:
stock = Stock.query.filter_by(id=form.id.data).one()
db.session.delete(stock)
db.session.commit()
return redirect(url_for('stock.stock'))
#this is the code from the link that I used to paginate
page, per_page, offset = get_page_args(page_parameter='page',
per_page_parameter='per_page')
total = len(forms) # this code counts the resulting list to be displayed
pagination_forms = get_forms(offset=offset, per_page=per_page)
pagination = Pagination(page=page, per_page=per_page, total=total)
form.process() # Do this after validate_on_submit or breaks CSRF token
return render_template('stock/stock.html', title=Stock, stocks=stocks
page=page, forms=pagination_forms, per_page=per_page, pagination=pagination)
#And finally this is the pagination passed to the html
so this for all those numptys like me who struggle with everything but still love it.

Django inline-formset ordering issue when editing

I am trying to use the Django inline-formset.
Forms should be displayed sorted by their order value which is correctly done when I request the form.
But if I change the order values and save, the first view is with the previous order (refresh does the trick)
forms:
class SlidesForm(forms.ModelForm):
order = forms.IntegerField(widget=forms.NumberInput())
background_image = forms.ImageField(widget=forms.FileInput(attrs={'class': 'custom-file-input'}), required=False)
text = forms.CharField(max_length=256, widget=forms.Textarea(attrs={'rows': 2, 'class': 'form-control'}), required=False)
class Meta:
model = SlideCarousel
fields = ['order', 'background_image', 'text']
views:
def management_form_general(request, city_slug):
city = City.objects.get(slug=city_slug)
SlideCarouselInlineFormSet = inlineformset_factory(City, SlideCarousel, form=SlidesForm, extra=0)
if request.method == 'POST':
carousel_formset = SlideCarouselInlineFormSet(request.POST, request.FILES, instance=city, queryset=city.slidecarousel_set.order_by("order"))
if carousel_formset.is_valid():
carousel_formset.save()
else:
carousel_formset = SlideCarouselInlineFormSet(instance=city, queryset=city.slidecarousel_set.order_by("order"))
return render(request, 'management/form/city_general.html', {'city': city, 'carousel_formset': carousel_formset})
Any idea what I am doing wrong ? Tried to reinstance the carousel_formset after the save but it seems nasty and it actually didn't work
Right now you're still returning the same queryset (already evaluated and ordered) in the formset. What you need is to get the data that you just saved and update the formset with it. I think that you have two options that should work.
Recreate the carousel_formset like you said. This might not be exactly what you want but it seems more likely than my second suggestion. You said you tried this and it didn't work. If your code looks the same as mine then you might want to skip this approach.
carousel_formset.save()
carousel_formset = SlideCarouselInlineFormSet(
instance=city,
queryset=city.slidecarousel_set.order_by("order"),
)
Usually, after I save a form(set) I would redirect to a success URL. In this case that would be the same path again.
carousel_formset.save()
return redirect(request.path)
A third option that I have no idea if it will work, but you could try for very little effort, would be to re-set the carousel_formset.queryset attribute.
carousel_formset.save()
carousel_formset.queryset = city.slidecarousel_set.order_by("order")

Can you use a Django form multiple times in one view?

I have a Django view that uses one form multiple times. The form is just a boolean field form that is supposed to initialize to True but then the user can decide to uncheck the boxes or not.
The problem I'm having is that the all of the fields evaluate to True no matter what the user leaves checked. Is this a problem with using the same form multiple times, or did I mess something else up?
The form looks like this:
class DataTypeForm(forms.Form):
def __init__(self,*args,**kwargs):
section_label = kwargs.pop('section_label')
initial_value = kwargs.pop('initial_value')
super(DataTypeForm,self).__init__(*args,**kwargs)
self.fields['dataType'].label=mark_safe(section_label)
self.fields['dataType'].initial=initial_value
self.fields['dataType'].required=False
dataType = forms.BooleanField(required=False)
This is the view:
def Manual_Request(request):
form_dict = {}
arg_dict = {}
message = message = {'last_url':'Nominal_Request'}
if request.method == 'POST':
logger.info("Query submitted, beginning query results render for:")
form_NOM = DataTypeForm(request.POST or None,section_label="ENG_NOM",initial_value=True)
form_DDM = DataTypeForm(request.POST or None,section_label="SCI_DDM",initial_value=True)
form_RAW = DataTypeForm(request.POST or None,section_label="SCI_RAW",initial_value=False)
if form_NOM.is_valid():
NOM = form_NOM.cleaned_data['dataType']
arg_dict.update({'ENG_NOM':str(NOM)})
logger.info("ENG_NOM: {val}".format(val=NOM))
if form_DDM.is_valid():
DDM = form_DDM.cleaned_data['dataType']
arg_dict.update({'SCI_DDM':str(DDM)})
logger.info("SCI_DDM: {val}".format(val=DDM))
if form_RAW.is_valid():
RAW = form_RAW.cleaned_data['dataType']
arg_dict.update({'SCI_RAW':str(RAW)})
logger.info("SCI_RAW: {val}".format(val=RAW))
return Request_Results(request,args_dict)
else:
logger.info("Rendering query page")
form_NOM = DataTypeForm(section_label="ENG_NOM",initial_value=True)
form_DDM = DataTypeForm(section_label="SCI_DDM",initial_value=True)
form_RAW = DataTypeForm(section_label="SCI_RAW",initial_value=True)
form_dict.update({'form_NOM':...etc})
return render(request,'InterfaceApp/COMMRequest_Manual.html',form_dict)
Help much appreciated!
I haven't run your code, but my best guess is that yes, it's a problem with using the same form multiple times in the same view. The reason? All of your <input type="checkbox" name="..." ... /> tags will have the same name, 'dataType'. The user's browser knows nothing of your back-end, and will just send, for example, dataType=on&dataType=on as POST data for the three fields if two are checked and one is not.
Seeing the problem here? How is django supposed to know which of those dataType fields are for your NOM, DDM, or RAW forms? It can't know.
You should be able to solve this using form prefixes. In short, there's a kwarg that you can pass to a form's __init__() that will cause a prefix to be added to all of the form items in the rendered HTML. So, for example:
form_NOM = DataTypeForm(request.POST or None, section_label="ENG_NOM",
initial_value=True, prefix="NOM")
form_DDM = DataTypeForm(request.POST or None, section_label="SCI_DDM",
initial_value=True, prefix="DDM")
form_RAW = DataTypeForm(request.POST or None, section_label="SCI_RAW",
initial_value=False, prefix="RAW")
Hope that helps!
This is exactly what Django formsets are for. They allows you to create a set of the same type of form. It handles prefixes, and adds a management form so that Django doesn't get confused as to what data comes from what form.
https://docs.djangoproject.com/en/1.8/topics/forms/formsets/

Django 1.4 - Keeping track of multiple queries and pagination

I have a site that basically displays a huge table of people that I want to be able to filter, sort and page through. I want to do this in multiple views that have different preexisting constraints on which people are displayed in the table. My problem is that, if I filter by state for example, sort by name, and then try to go to the next page, it resets. For example, if I filter once I get my/url/2013/?sort=n&state=MN&page=1; then if I go to page 2, I just get my/url/2013/?page=2. It doesn't remember what I've already asked it.
def all(request, year=default_year):
#sorting (sort_options was defined prior to this view)
if 'sort' in request.GET:
sort = request.GET.get('sort')
order = sort_options[sort]
else:
order = '-score'
players = Player.objects.filter(year=year).order_by(order)
url_query = ''
#filtering
if 'position' in request.GET:
filterData = _filter(request,players)
players = filterData['players']
url_query += filterData['url_query']
# pagination
paginator = Paginator(players,25)
page = request.GET.get('page')
try:
players = paginator.page(page)
except PageNotAnInteger:
players = paginator.page(1)
except EmptyPage:
players = paginator.page(paginator.num_pages)
data = {'players':players,'url_query':url_query}
return render_to_response('all.html', data, context_instance = RequestContext(request))
In my template I reference url_query like so:
<th class="name">Name</th>
and like so:
Next
This is pretty obviously wrong to me but I don't know how to do it right.
When you render your template you can pass to the context (in your case data) object sort parameter and then in the template you can use url to show which view to invoke.