Django queryset with variable value - python

I am writing dynamic filters in django for my database where I am using the below code where I have 2 variables(p_type,s_type):
p_type=[]
s_type=[]
query = request.GET.get("q")
p_type =request.GET.get("p_type")
s_type = request.GET.get("s_type")
#messages.add_message(request, messages.INFO, p_type)
#messages.add_message(request, messages.INFO, s_type)
if query:
queryset_find = queryset_list.filter(
Q(FP_Item__contains=query))
context = {'object_list': queryset_find}
return render(request, 'index.html', context)
elif p_type:
queryset_find = queryset_list.filter(
Q(p_type__contains=s_type))
context = {'object_list': queryset_find}
return render(request, 'index.html', context)
else:
context = {'object_list': queryset}
return render(request, 'index.html', context)
but django returns error at below line
Q(p_type__contains=s_type))
I have dynamic radio button where the value of p_type matches with my database but even though I am receiving the following error:
Exception Type: FieldError
Exception Value:
Cannot resolve keyword 'p_type' into field. Choices are: ... (same choices which I am using with my database).
Isn't it doable with variable query ? Any other methods ?
model:
class RFP(models.Model):
FP_Item = models.TextField(max_length=1500)
P_63 = models.TextField(max_length=1000)
P_64 = models.TextField(max_length=1000)

If p_type holds the name of the field you want to query, then you can do:
elif p_type:
kwargs = {
'{}__contains'.format(p_type): s_type
}
queryset_find = queryset_list.filter(Q(**kwargs))
...

Related

Django form has no attribute 'cleaned_data'

This is the code I have, and when I run it on Django, I am met with this error: 'Title' object has no attribute cleaned_data
def new(request):
form = Title(request.POST)
if request.method == "POST":
if form.is_valid:
title = form.cleaned_data["title"]
text = form.cleaned_data["text"]
util.save_entry(title, text)
else:
return render(request, "encyclopedia/error.html",{
"form":NewForm()
})
return redirect(reverse('page', args = [title]))
return render(request, "encyclopedia/newpage.html",{
"form1":Title(),
"form": NewForm()
})
You are probably getting the exception thrown in your return statement where you are instantiating a new Title object. This object only gets the cleaned_data attribute when is_valid method has been called upon. Hence you haven't called this on the new Title object and that is the reason why you are getting the error.
you use form = Title(request.POST), but the line after you check whether the request.method equals POST or not. i think yo should move that line inside the if statement
is_valid() it's a function
def new(request):
form = Title(request.POST)
if request.method == "POST":
if form.is_valid():
title = form.cleaned_data["title"]
text = form.cleaned_data["text"]
util.save_entry(title, text)
else:
return render(request, "encyclopedia/error.html",{
"form":NewForm()
})
return redirect(reverse('page', args = [title]))
return render(request, "encyclopedia/newpage.html",{
"form1":Title(),
"form": NewForm()
})

I am having an Error in Django Search Views

I am trying to create a search functionality in django. I just wrote the view but i was getting an error with that. I have tried debugging the codes but I don't seems to know where the error is.
Error Log
File "C:\Users\Habib\Documents\django\FIVERR\Alex_SMS\SMS\core\urls.py", line 2, in <module>
from . import views
File "C:\Users\Habib\Documents\django\FIVERR\Alex_SMS\SMS\core\views.py", line 37
return render(request, "core/search.html", context)
^
SyntaxError: invalid syntax
views.py
def search(request):
if request.method == 'GET':
product_name = request.GET.get('search')
try:
product = Product.objects.filter(name__icontains=product_name)
context = {"product": product}
return render(request, "core/search.html", context)
except:
product = "What is this"
context = {"product":product}
else:
result = "Sorry there is no product with that name"
return render(request, "core/search.html", {"result":result})
You're trying to return during a try/except which you can't do.
You need to return after this block;
def search(request):
if request.method == 'GET':
product_name = request.GET.get('search')
try:
product = Product.objects.filter(name__icontains=product_name)
context = {"product": product}
except:
product = "What is this"
context = {"product":product}
return render(request, "core/search.html", context)
else:
result = "Sorry there is no product with that name"
return render(request, "core/search.html", {"result":result})
It's generally considered bad practise to not catch specific exceptions though.
Here's some good information on exception handling; https://wiki.python.org/moin/HandlingExceptions
I was able to solve the issue by adding an except block as well as removing the return statement after the try statement and adding to after the except block.
views.py
def search(request):
if request.method == 'GET':
product_name = request.GET.get('search')
try:
product = Product.objects.filter(name__icontains=product_name)
context = {"product": product}
except product_name.DoesNotExist:
product = None
return render(request, "core/search.html", context)
else:
result = "Sorry there is no product with that name"
return render(request, "core/search.html", {"result":result})

Django : view calls an other template (Pagination )

I have this view:
class DamageListCriteria(TemplateView):
template_name = "damage/damagelist_criteria.html"
def get(self, request):
form = DamageListCriteriaForm()
general = General.objects.get(pk=1)
args = {
'form': form,
'general': general
}
return render(request, self.template_name, args)
def post(self, request):
general = General.objects.get(pk=1)
form = DamageListCriteriaForm(request.POST)
form.non_field_errors()
if form.is_valid():
fromdate = request.POST.get('fromdate')
fdate = datetime.strptime(fromdate, '%d/%m/%Y')
fdate = datetime.combine(fdate, datetime.min.time(), tzinfo=pytz.UTC)
print('fdate ', fdate)
todate = form.cleaned_data['todate']
#tdate = datetime.strptime(todate, '%d/%m/%Y') + timedelta(days=1)
tdate = datetime.strptime(todate, '%d/%m/%Y')
tdate = datetime.combine(tdate, datetime.max.time(), tzinfo=pytz.UTC)
print('tdate ', tdate)
d_list = Damage.objects.filter(entry_date__range=(fdate, tdate))
paginator = Paginator(d_list, 1)
page = request.GET.get('page')
try:
damage_list = paginator.page(page)
except PageNotAnInteger:
# If page is not an integer, deliver first page.
damage_list = paginator.page(1)
except EmptyPage:
# If page is out of range (e.g. 9999), deliver last page of results.
damage_list = paginator.page(paginator.num_pages)
template = "damage/damagelist_table.html"
form = DamageListForm()
general = General.objects.get(pk=1)
fromdatetext = fdate.strftime('%d/%m/%Y')
todatetext = tdate.strftime('%d/%m/%Y')
args = {
'form': form,
'damage_list': damage_list,
'general': general,
'fromdate': fromdatetext,
'todate': todatetext
}
return render(request, template, args)
else:
print('form is not valid')
print(form.errors)
# form = DamageEntryForm()
args = {'form': form,
'general': general
}
return render(request, self.template_name, args)
I want to get some criteria to make a filtering listing of my database.
It worked this way , until the moment i tried to add pagination.
The url is http://127.0.0.1:8000/damage/damage/list/criteria/
url(r'damage/list/criteria/$', views.DamageListCriteria.as_view(), name="damage-list-criteria"),
Next and Previous don't work because I am still at this url after the
return render(request, template, args)
Can i redirect somehow?
I understand that this might be the wrong way to do the listing. Can you help me , how to do it?
Thanks a lot
Kostas
The easiest thing to do would be not use Django itself but use Django REST framework and reuse its serializer classes along with APIView (or one of its subclasses). Are you in a position to use it or are you constrained?

Filtering data with __range

I'm using django_tables2 thus part of the code is dependent on this package, but this should be irrelevant to the overall problem.
forms.py
class PersonForm(forms.Form):
date_from = forms.DateField(input_formats='%d/%m/%Y')
date_to = forms.DateField(input_formats='%d/%m/%Y')
views.py
def filter(request):
table = PersonTable(Person.objects.all())
if request.method == 'POST':
form = PersonForm(request.POST)
if form.is_valid():
date_from = form.cleaned_data['date_from']
date_to = form.cleaned_data['date_to']
result_filtered = table.filter(date__range=(date_from, date_to))
RequestConfig(request, paginate={"per_page": 100}).configure(table)
return render(request, "people.html", {
"table": result_filtered })
args = {}
args.update(csrf(request))
args['form'] = PersonForm()
args['table'] = table
RequestConfig(request, paginate={"per_page": 100}).configure(table)
return render(request, 'people.html', args)
Simply, filtering is not working. I can see the entire table, but when I try to filter nothing happens. Can you see what's wrong?
I'm pretty sure you need to call .filter() on the query set rather than the table. For example:
result_filtered = PersonTable(Person.objects.filter(date__range=(date_from, date_to))
Also, on this line:
RequestConfig(request, paginate={"per_page": 100}).configure(table)
You are passing in table. You should pass in result_filtered instead.
This is one way I'd do it, assuming your Person model has a date field:
def filter(request):
if 'date_from' in request.GET and 'date_to' in request.GET:
# form data has been submitted
form = PersonForm(request.GET)
if form.is_valid():
date_from = form.cleaned_data['date_from']
date_to = form.cleaned_data['date_to']
people = Person.objects.filter(date__range=(date_from, date_to))
table = PersonTable(people)
else:
table = PersonTable(Person.objects.all())
else:
form = PersonForm()
table = PersonTable(Person.objects.all())
RequestConfig(request, paginate={"per_page": 100}).configure(table)
args = {}
args.update(csrf(request))
args['form'] = form
args['table'] = table
return render(request, 'people.html', args)
This binds the form if both its expected fields are present, and limits the queryset according to the results if valid. If not valid, it renders the bound form and the table built from the unlimited table. If form data was not submitted, it renders the table built from the unlimited table.
Your form tag's method attribute should be GET rather than POST, if using this design.
This doesn't quite follow the usual Django patterns for form handling because the form isn't actually making any changes, so you can use GET and don't need to return a redirect on success.

Put forms.DateField parameter into view method

I have following problem:
In my forms.py i have following line in class:
date_from = forms.DateField(label='date', initial='1800-01-01', widget=SelectDateWidget(years=[y for y in range(1450,2050)]))
In template i can choose the concrete date.
This parameter i want to transfer to my view.py file to the following method:
def adv_search(request):
objects = None
if request.GET.get('key'):
form = AdvancedSearchForm(request.GET)
if form.is_valid():
repo = Repository()
objects = list(repo.find_objects(date__gt='2012-07-23'))
return render(request, 'templates/adv_search.html', {'form': form, 'objects': objects })
return render(request, 'templates/adv_search.html', {'form': AdvancedSearchForm(), 'objects': objects})
In objects = list(repo.find_objects(date__gt='2012-07-23')) i have date__gt with concrete Date.
How i can add to date__gt the date from form.Date.Field ?
Thanks.
It's in form.cleaned_data:
You can access it by doing:
objects = list(repo.find_objects(date__gt=form.cleaned_data['date_from']))
This is assuming that your form declaration looks something like:
class AdvancedSearchForm(forms.Form):
date_from = forms.DateField(...)
...

Categories

Resources